howtouselinux

3 ways to install sshpass

Table of Contents

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.

wget https://sourceforge.net/projects/sshpass/files/latest/download/sshpass/1.08/sshpass-1.08.tar.gz
tar zxvf sshpass-1.08.tar.gz
cd sshpass-1.08
./configure
make
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 https://sourceforge.net/projects/sshpass/.

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@172.20.21.201

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@172.20.21.201

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
10.20.21.200
10.20.21.201
10.20.21.202
10.20.21.203
10.20.21.204

The use while loop to copy SSH keys:

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

Example:

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

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.