How to automate SSH login with password? I'm configuring my test VM, so heavy security is not considered. SSH chosen for acceptable security with minimal configuration.


echo password | ssh id@server

This doesn't work.

I remember I did this with some tricks somebody guided me, but I can't remember now the trick I used...

up vote 282 down vote accepted

Don't use a password. Generate a passphraseless SSH key and push it to your VM.

If you already have an SSH key, you can skip this step… Just hit Enter for the key and both passphrases:

$ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/

Copy your keys to the target server:

$ ssh-copy-id id@server
id@server's password: 

Now try logging into the machine, with ssh 'id@server', and check in:


to make sure we haven’t added extra keys that you weren’t expecting.

Finally check logging in…

$ ssh id@server


You may also want to look into using ssh-agent if you want to try keeping your keys protected with a passphrase.

  • 9
    I finally decided using key pairs. Because I realized that's the most simple way. – Eonil Mar 1 '11 at 12:49
  • 7
    @Eonil: Don't be tempted to use keys without a pass phrase. Learn how to use ssh-agent or pageant. – Iain Mar 1 '11 at 13:20
  • 73
    This is a good answer, but not the correct answer to the question. – John Hunt Jul 4 '13 at 13:34
  • 36
    These kinds of answers really, really annoy me. That wasn't the question. Nobody asked how to use key pairs. – Matt Fletcher Jun 16 '16 at 12:41
  • 5
    This does not answer the question. It is a good answer for a completely different question, but it is terrible for the one asked. – srchulo Jan 4 '17 at 20:42
$ sudo apt-get install sshpass
$ sshpass -p your_password ssh user@hostname
  • 36
    Yup, sometimes you can't use key based auth for various reasons.. for example right now I can't use keyauth on a plesk server because out the box it's not enabled and I don't have root. – John Hunt Jul 4 '13 at 13:33
  • 13
    +1! As a side note, you need to run plain ssh once before using sshpass, in order to confirm the RSA fingerprint – user123444555621 Aug 2 '13 at 8:08
  • 12
    -1 for having to use the password in the command. This logs the password at .bash_history in plain text on your machine. – user164495 Apr 11 '14 at 21:18
  • 11
    @MisterDood You could run history -r after the command to erase your history. Good point though. – NuclearPeon May 14 '15 at 18:37
  • 10
    Pro tip: If you don't want to have a specific command show up in .bash_history, prefix the command with a space. It just works. However, users of this command should be more concerned that non-privileged users on the system can see the full command-line with ps, which, of course, includes the password. Since ssh sessions tend to be long-lived, this is a security issue. – CubicleSoft Jul 16 '16 at 15:47

Easy answer in three easy steps

Generate a rsa keypair:

# ssh-keygen

then copy it on the server with one simple command:

# ssh-copy-id userid@hostname

you can now log in without password:

# ssh userid@hostname
  • Works fine with the default values. Using ~/ didn't work for me when attempting ssh-copy-id. – Cees Timmerman Jun 4 '15 at 10:08
  • If you want this steps to work for different user, 1. ssh-keygen 2. ssh-copy-id nazir@hostname 3. ssh nazir@hostname – Venfah Nazir Jun 17 '16 at 10:23
  • 1
    For me it was ssh-copy-id -i ~/.ssh/tatu-key-ecdsa user@host – Gabriel Fair Sep 8 '17 at 21:47
  • 5
    This isn't an answer to the question. – Steve Bennett Nov 27 '17 at 23:10
  • And? The answer is above, sshpass... – lzap Nov 30 '17 at 16:21

Use expect:

#!/usr/bin/expect -f
#  ./ssh.exp password id
set pass [lrange $argv 0 0]
set server [lrange $argv 1 1]
set name [lrange $argv 2 2]

spawn ssh $name@$server
match_max 100000
expect "*?assword:*"
send -- "$pass\r"
send -- "\r"


# ./1.ex password localhost ooshro
spawn ssh ooshro@localhost
ooshro@localhost's password: 
Linux ubuntu-1010-server-01 2.6.35-25-generic-pae #44-Ubuntu SMP Fri Jan 21 19:01:46 UTC 2011 i686 GNU/Linux
Ubuntu 10.10

Welcome to Ubuntu!
 * Documentation:
Last login: Tue Mar  1 12:41:12 2011 from localhost
  • 1
    It worked but it can't print stdout of remote machine. – Eonil Mar 1 '11 at 12:41
  • it works well for some machine can't put the key in advance since IP address is changed everytime. – larrycai Dec 19 '12 at 6:14
  • 4
    it will be good to add -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null for ssh command as well to avoid accept the machine into known_hosts – larrycai Dec 19 '12 at 6:15
  • B.. b but muh ssh keys... – Damien Ó Ceallaigh Jun 1 '17 at 5:50
  • More detailed example of this script you can find at:… This examples here should work with remote commands too – Radon8472 Jul 11 at 13:39

This might not be any use to you, but you can do it with Perl:

use warnings;  
use strict;  

use Net::SSH::Perl;  
my $host = '';  
my $user = 'root';  
my $pass = 'hunter2';  
my $ssh = Net::SSH::Perl->new('$host');  
$ssh->login('$user', '$pass') or die "Oh noes! $!";

Sure you don't want to use SSH keys rather than passwords? That way it's both secure and automatic.

  • 3
    Using SSH keys without password is only slightly more secure than using passwords in a file. – yunzen Apr 14 '15 at 7:34
  • 8
    @yunzen Incorrect. Key authentication protects you from mitm-attacks even if you don't know the host key. An attacker could impersonate the server, but never connect to the real server. With password authentication any server you connect to (legitimate or not) will see the password. For those reasons an ssh key without password is a lot more secure than just storing the password in a file. – kasperd May 17 '16 at 7:15
  • 5
    This is not an answer. – Steve Bennett Nov 27 '17 at 23:10
  • @SteveBennett It's the same as the accepted answer and was posted before it, though it has less detail. – Michael Hampton Aug 4 at 14:34
  • Well, I'd say the accepted answer is a decent answer to a question that wasn't asked. This isn't anything. – Steve Bennett Aug 6 at 6:24

I am surprised nobody mentioned plink from the putty-tools package in Ubuntu:

plink user@domain -pw mypass  [cmd]

It also available on Windows and the syntax is mostly compatible with the openssh client.

  • For windows your answer is good, unfurtunally linux / unix user usually dont have plink – Radon8472 Jul 11 at 13:48

protected by Tom O'Connor Jan 2 '14 at 13:27

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.