sshpass: Login To SSH Server / Provide SSH Password Using A Shell Script

Q. How do I login over ssh without using password less RSA / DSA public keys? How do I use ssh in a shell script? How do I login non-interactivly performing password authentication with SSH and shell scripts?

A. You can use sshpass command to provide password for ssh based login. From the man page:

sshpass is a utility designed for running ssh using the mode referred to as "keyboard-interactive" password authentication, but in non-interactive mode.

ssh uses direct TTY access to make sure that the password is indeed issued by an interactive keyboard user. Sshpass runs ssh in a dedicated tty, fooling it into thinking it is getting the password from an interactive user.

The command to run is specified after sshpass' own options. Typically it will be "ssh" with arguments, but it can just as well be any other command. The password prompt used by ssh is, however, currently hardcoded into sshpass.

WARNING! These examples considered the least secure as simple ps command can expose password to all users on the same host. I highly recommend using ssh's public key authentication.

Install sshpass under Debian / Ubuntu Linux

Type the following command:
$ sudo apt-get install sshpass

How do I use sshpass?

Login to ssh server called server.example.com with password called t@uyM59bQ:
$ sshpass -p 't@uyM59bQ' ssh username@server.example.com
Under shell script you may need to disable host key checking:
$ sshpass -p 't@uyM59bQ' ssh -o StrictHostKeyChecking=no username@server.example.com

How do I backup /var/www/html using rsync?

Run rsync over SSH using password authentication, passing the password on the command line:
$ rsync --rsh="sshpass -p myPassword ssh -l username" server.example.com:/var/www/html/ /backup/

Further readings:

  • sshpass man page
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 10 comments… read them below or add one }

1 Robert de Bock 09.12.08 at 11:54 am

I don’t agree to this trick, better use an ssh-agent and ssh-add. Check out this howto: http://meinit.nl/ssh-agent-trick
Regards, Robert de Bock.

2 Dave 09.12.08 at 3:00 pm

Wow… this is scary. I would never recommend this method to anyone! If you want to ssh using command line with no password prompt, just create your ssh key without a password.

Not only would ps reveal the password for your ssh key, but also it is stored in your history on the filesystem!

3 Miker 09.12.08 at 4:30 pm

I’ve always done it using ssh-copy-id or the good ol manual way.

$ mkdir -p ~/.ssh If it doesn’t already exist
$ chmod 700 ~/.ssh
$ cd ~/.ssh
$ ssh-keygen -t rsa
$ cat ~/.ssh/id_rsa.pub | ssh ‘mkdir .ssh; chmod 700 .ssh; cat>>.ssh/authorized_keys’

4 Miker 09.12.08 at 9:19 pm

Web input filter changed the last line of my comment.

$ cat ~/.ssh/id_rsa.pub | ssh hostname ‘mkdir .ssh; chmod 700 .ssh; cat>>.ssh/authorized_keys’

5 ram 09.13.08 at 7:54 am

it is recommended for sererrs it may used in scripts,that is not comes under system history and you may given password for that script.

6 Raj 09.13.08 at 8:47 am

I use this tool and it is safe. Here is my scenario

I’ve a central backup server and I’m the only person who logs in. Server is connected to internet and no services are running except ssh on vpn interface. I need to login to over 20 boxes collocated or leased in 5 data centers. With this tool I don’t have to upload ssh keys to those boxes. So if any one of the production box got hacked, my backup server remain intact.

7 KwangErn 10.09.08 at 9:41 pm

Personally, I find keychain to be the best alternative. At least I know I’m save from any possible cracking!

http://www.gentoo.org/proj/en/keychain/

8 KwangErn 10.09.08 at 9:43 pm

On an extra note, one can clear the keychain (keychain –clear) on every login using .bash_profile just in case. ;)

9 MPerera 05.06.09 at 11:51 pm

This is what exactly I looking for.
All the servers I used ssh-copy-id. But recently one server replaced by 3rd party and they manage it and I do not have any write access login (no home directory). I run a script to rsync just two directories and last two weeks I had to do this manually since cron job filling.
My problem fixed by this solution.

10 Arete Vestige 07.01.09 at 3:41 pm

Sometimes it is not possible to add the keys or advisable to do so. sshpass is an excellent solution for large deployments of secure systems that prevents the innate issues of unauthenticated access.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , ,

Previous post: Linux: Force Users To Change Their Passwords Upon First Login

Next post: ip6tables: IPv6 Firewall For Linux