Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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...

share|improve this question
FreeBSD did not accept password-less keys. Don't be tempted. However some Linux servers accepted it. I believe the Linux server was misconfigured. –  Eonil Mar 1 '11 at 14:32
This is a valid question. For example, I want to allow a user to enter a password, then login in to another machine using it. I can't assume that there will be ssh keys distributed across all our machines. The answers below so far do not help this situation. –  dfrankow Apr 12 '12 at 16:30

8 Answers 8

up vote 85 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/id_rsa.pub.

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.

share|improve this answer
I finally decided using key pairs. Because I realized that's the most simple way. –  Eonil Mar 1 '11 at 12:49
@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
ssh-copy-id doesn't accept password from STDIN either. When you have to log in just once for the sake of creation of a new su, uploading its pubkey and restricting logins to keys and restricting root login, it doesn't make sense to upload pubkey for root. –  phil pirozhkov Nov 3 '12 at 14:05
If your computer doesn’t have ssh-copy-id, this snippet (which sends the key by piping to ssh) is equivalent. Alternatively, on Mac OS X, install ssh-copy-id using Homebrew: brew install ssh-copy-id. –  Rory O'Kane Mar 25 '13 at 17:44
This is a good answer, but not the correct answer to the question. –  John Hunt Jul 4 '13 at 13:34
$ sudo apt-get install sshpass
$ sshpass -p your_password ssh user@hostname
share|improve this answer
Thanks for actually answering the question :) –  diedthreetimes Jun 9 '13 at 19:54
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
+1! As a side note, you need to run plain ssh once before using sshpass, in order to confirm the RSA fingerprint –  Pumbaa80 Aug 2 '13 at 8:08
-1 for having to use the password in the command. This logs the password at .bash_history in plain text on your machine. –  Mister Dood Apr 11 '14 at 21:18
Not sure, but usefull for your own virtual local servers management. Thank you! –  ssoto Jan 13 at 17:49

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 hostname

you can now log in without password:

# ssh hostname
share|improve this answer

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

share|improve this answer
I realized using key-pairs is most simple way. Thanks for care :) –  Eonil Mar 1 '11 at 12:50

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:  https://help.ubuntu.com/
Last login: Tue Mar  1 12:41:12 2011 from localhost
share|improve this answer
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
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

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 = 'remote.serv.er';
my $user = 'root';
my $pass = 'hunter2';
my $ssh = Net::SSH::Perl->new('$host');
$ssh->login('$user', '$pass') or die "Oh noes! $!";

share|improve this answer
@JLo - have a +1 for good measure (and to cancel out the -1!). –  Coops Mar 1 '11 at 14:32
Thanks Coops :) I did think it was a bit harsh! I'm keeping an eye on your reputation, I see you're sneaking back up on me. –  James L Mar 1 '11 at 14:42
Likewise, you can do with this Ruby github.com/net-ssh/net-ssh –  EnabrenTane Jun 17 '13 at 23:47
+1 for coming up with correct/alternative answer :) –  John Hunt Jul 4 '13 at 13:57

Hi I hope this guide helps you:


It goes over keyless entry and aliases which might be helpful to you.

share|improve this answer
This guide helped me partially. Thanks :) –  Eonil Mar 1 '11 at 12:51
straight to the point –  cobie Jul 8 '13 at 18:13

When using encrypted home directory you have to use password login. When using public key login your home directory is not automatically encrypted.

share|improve this answer
What does this have to do with automated ssh login? –  Drew Khoury Oct 28 '13 at 12:32

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 answers, posting an answer now requires 10 reputation on this site.

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.