Establish SSH Connections Using sshpass

Dmitri Popov

Productivity Sauce

Jan 31, 2014 GMT
Dmitri Popov

sshpass enables non-interactive ssh password authentication. In other words, it allows you to establish an SSH connection by specifying a password as part of the command. So instead of using a command like ssh user@remotehost, and then manually entering the password when prompted, you can use sshpass to pass the password non-interactively. This lets you automate scripts that establish an SSH connection as part of their routine (e. g., rsync-based backup scripts). In other words, sshpass provides an alternative to the popular password-less SSH login technique.

On Debian and Ubuntu-based Linux distributions, sshpass can be installed by running apt-get install sshpass command as root. On openSUSE, an unstable version of sshpass can be installed via software.opensuse.org.

sshpass is dead-easy to use. Instead of using the ssh user@remotehost command to establish an SSH connection, use the sshpass -p 'password' ssh user@remotehost command which automatically passes the specified password. Specifying the password as part of a command is not good security practice. A better approach is to export the password as the SSHPASS environment variable, and then use sshpass with the -e parameter:

export SSHPASS=password
sshpass -e ssh user@remotehost

That's all there is to it.

comments powered by Disqus