Skip to Content

3 ways to install sshpass

To fix “to use the the ‘ssh’ connection type with passwords, you must install the sshpass program”, we need to install sshpass package in our ansible control machine.

What is sshpass?

Sshpass is a utility that allows you to store and use your SSH password in a secure way. Sshpass is typically used in conjunction with the ssh command. When you use sshpass, your SSH password is encrypted and stored in a file. 

When you connect to a remote system, the sshpass utility decrypts your password and sends it to the server. The server then uses the password to authenticate your connection.

Install sshpass In Mac OS X

We can install sshpass through source code.

tar zxvf sshpass-1.08.tar.gz
cd sshpass-1.08
sudo make install

% sshpass
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
-f filename Take password to use from file
-d number Use number as file descriptor for getting password
-p password Provide password as argument (security unwise)
-e Password is passed as env-var "SSHPASS"
With no parameters - password will be taken from stdin
-P prompt Which string should sshpass search for to detect a password prompt
-v Be verbose about what you're doing
-h Show help (this screen)
-V Print version information

At most one of -f, -d, -p or -e should be used

We can download the latest version sshpass from here

Install sshpass In CentOS

The yum install command is used to install software packages on Linux systems. It can be used to install both RPM packages.

The yum install command requires two parameters: the name of the package you want to install or the location of the package’s installation file. The installation file can be a local file or a remote file.

yum install sshpass

Here’s an example of how the yum install command might be used: sudo yum install httpd. This command will install the Apache web server on your system.

sudo yum install /path/to/package.rpm. This command will install the package located at /path/to/package.rpm on your system.

Install sshpass In Ubuntu

The apt install command is used to install software packages on Debian and Ubuntu systems. It can be used to install both RPM and DEB packages.

apt install sshpass

How does sshpass work?

Sshpass works by encrypting your password and storing it in a file. When you use sshpass to connect to a remote system, the encrypted password is sent to the server. The server then decrypts the password and uses it to authenticate your connection.

What are some common uses for sshpass?

Sshpass is commonly used to automate the login process for remote servers. It can also be used to store passwords in a secure location. Additionally, sshpass can be used to connect to systems that require two-factor authentication.

Are there any risks associated with using sshpass?

Yes, there are some risks associated with using sshpass. One risk is that sshpass can be used to steal passwords. Additionally, if your sshpass file is compromised, an attacker could gain access to your systems.

sshpass Example

$ sshpass -p ‘MyStrongSSHP@ssword’ ssh username@

we can also put the ssh password to one file like this.

$ echo ‘MyStrongSSHP@ssword’ >ssh_pass_file
$ chmod 0400 ssh_pass_file
$ sshpass -f ssh_pass_file ssh username@

Automate copying of SSH keys to multiple servers

If we have multiple servers and would like to automate copying of SSH public keys to the servers, first create a file with all remote servers.

$ vim /tmp/servers

The use while loop to copy SSH keys:

cat /tmp/servers | while read line; do
sshpass -p ‘SSH_USER_PASSWORD’ ssh-copy-id @$line;


cat /tmp/servers | while read line; do
sshpass -p ‘MySSHP@ssword’ ssh-copy-id username@$line;

Samuel Meri

Monday 6th of November 2023

Cool. I used yum command to install it. Looks great. Thanks.