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.
Run the following command to open the sshd_config file using nano editor to add some necessary configurations.
Add the following lines in the file to enable the root login and password-based authentication.
PermitRootLogin yes
Run the following command to restart the SSH service.
Syntax
The syntax of this command is given below. It supports different options which have been described later.
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.
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.
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.
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.
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.
Run the following command to set the permission bits for the sshfile file which will make the file secure.
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.
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.
Run the following command from the terminal.
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.