Linux Commands

How to Use sshpass for Non-Interactive SSH Login

The Linux user can use password-based or password-less authentication to log into the remote server using SSH. Password-less authentication is more secure but the easiest and most popular way of authentication is password-based authentication. The user has to provide the password whenever required to authenticate and the SSH also requires to access a shell script manually.

The sshpass utility is used to implement automated password-based authentication. It runs the SSH in dedicated TTY (TeleTYpewriter) to confirm that the password is provided by an interactive keyboard user for non-interactive authentication. How the sshpass can be used for authentication has shown in this tutorial.

Prerequisites

Before starting the steps of this tutorial, the following steps will be required to complete.

Enable the SSH service on Ubuntu if it is not enabled before.

Generate the SSH Key pairs to execute the commands in the remote server. Run the following command to create the public key and the private key. The private key will be stored in the remote server and the public keys will be stored in the client securely.

$ ssh-keygen -t rsa

Run the following command to open the sshd_config file using nano editor to add some necessary configurations.

$ sudo nano /etc/ssh/sshd_config

Add the following lines in the file to enable the root login and password-based authentication.

PasswordAuthentication yes
PermitRootLogin yes

Run the following command to restart the SSH service.

$ sudo service ssh restart

Syntax

The syntax of this command is given below. It supports different options which have been described later.

$ sshpass [-f filename|-d number|-p password|-e] [options] command arguments

Different Options of the sshpass Command

The purpose of different sshpass options has been described here. sshpass reads the password from the standard input if no option is given.

Option Purpose
-p password It is used to provide the password on the command line.
 -f filename It is used to give the alternative source of the password.
 -d number It is used to provide the file descriptor inherited by sshpass.
-e It is used to take the password from the environment variable “SSHPASS”.

Install sshpass

sshpass is not installed on Ubuntu by default. Run the following command to install sshpass on Ubuntu.

$ sudo apt-get install sshpass

The following output will appear if the sshpass is installed properly.

Connect with the Server by Providing a Password

The way to connect with the remote machine by using the sshpass with -p option is shown in this part of the tutorial. Here, the username is ‘fahmida’ and the password is ‘12345’ of the sever machine. The IP address of the server machine is 10.0.2.15. The username of the client machine is ‘yesmin’. Run the following command to connect with the server by providing the password.

$ sshpass -p '12345' ssh <a href="mailto:[email protected]">[email protected]</a>

The following output will appear if the connection is established properly with the server machine. The output shows that the username is changed to ‘fahmida’ from ‘yesmin’ in the command prompt after establishing the connection. Now, if the user runs any command, then it will be executed from the server machine.

Connect with the Server Without Providing a Password

It is not secure to provide the password in the command line. This problem can be solved in two ways. One way is to save the password in the environment variable using the export command and another way is to save the password in a file.

Run the following command to save the password in the SSHPASS environment variable using the export command.

$ export SSHPASS='12345'

Now, you can run the sshpass command with the -e option to make the connection with the server machine without providing a password in the command line. Run the following command to connect with the server without giving the password.

$ sshpass -e ssh <a href="mailto:[email protected]">[email protected]</a>

The following output will appear if the connection is established properly with the server machine. The output shows that the username is changed to ‘fahmida’ from ‘yesmin’ in the command prompt after establishing the connection like the previous command.

The way to connect with the server using sshpass by saving the password in a file has been shown in this part of the tutorial. Run the following command to create the sshfile file and store the password to connect with the server machine.

$ echo '12345'> sshfile

Run the following command to set the permission bits for the sshfile file which will make the file secure.

$ chmod 0400 sshfile

Now, run the following command to connect with the server without providing the password in the command line. Here, the -f option has been used with sshpass command to read the password from the file.

$ sshpass -f sshfile ssh [email protected]

The following output will appear if the connection is established properly with the server machine. The output shows that the username is changed to ‘fahmida’ from ‘yesmin’ in the command prompt after establishing the connection like the previous command.

Using the sshpass Command in the Script

The sshpass command is used to make the connection with the server from the terminal in the previous part of this tutorial. But you can execute any command in the server machine without login into the server by writing a bash script with sshpass command. Create a bash file named ssh.sh in the client machine with the following content. The script will execute the `pwd` command in the server machine and store the output in a variable. The value of the variable will be printed later.

ssh.sh
#!/bin/bash

value=$(sshpass -f sshfile ssh [email protected] 'pwd')
echo $value

Run the following command from the terminal.

$ bash ssh.sh

The following output will appear if the pwd command is executed properly in the server machine. The username of the server machine is ‘fahmida’. So, the output shows that ‘/home/fahmida/’ is the output of the pwd command.

Conclusion

The uses of the sshpass command for non-interactive login have been shown in this tutorial by two different user accounts of the localhost. You can do the same task for the remote host by following the steps shown in this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.