How to Use sshpass for Non-Interactive SSH Login?

Non-interactive SSH login refers to the process of logging into a remote server using SSH (Secure Shell) without the need for user interaction. It is done using an SSH key pair comprising a private and public key. The purpose of non-interactive SSH login is to automate tasks such as backups, file transfers, and software deployment and to allow easy remote access to a server without needing a password.

This guide will demonstrate step-by-step procedures to use the sshpass for non-interactive ssh login in Linux. 

Prerequisites: Install sshpass and Configure SSH on Linux

The sshpass allows users to specify the password for non-interactive. To install the “sshpass” in Linux distributions, follow the below scripts:

$ sudo apt install sshpass # Ubuntu, Debian, LinuxMint
$ sudo dnf install sshpass # Fedora, RHEL, CentOS
$ sudo pacman -S sshpass  # Arch Linux

The output shows that all the required packages of “sshpass” have been installed in the current system.

Check the SSH Services 

Before using the SSH services; it is recommended to check the ssh services status via the command:

$ systemctl status ssh

The output confirms that “ssh.services” are active.

Note: If the “ssh.services” are not active, configure the SSH services by following the link.

How to Use sshpass for Non-Interactive SSH Login? 

Before getting into the examples, let’s understand the syntax of the sshpass:

$ sshpass -p [password] ssh [options] [user]@[host]

The description of arguments is provided below:

  • p [password]: specifies the password to use for authentication.
  • ssh [options]: refers to the user’s options to pass to the ssh.
  • [user]@[host]: the username and hostname of the remote server.

Example 1: Using sshpass for Non-Interactive SSH Login

In this example, the “sshpass” command is utilized to connect to a remote host using a non-interactive SSH login. In our case, username “roger” and hostname “ubuntu” is utilized, having password “1212” with the “p” option to log in to the remote server:

$ sshpass -p '1212' ssh roger@ubuntu

The output shows that the remote host has been logged in via non-interactive ssh login.

Example 2: Using sshpass to Read the Password From a Particular File

We can also use sshpass with the “f” option for reading the password from a specified file. It is helpful if the user doesn’t mention the password in the terminal window. For this, specify the file name that stores the password:

$ sshpass -f file.txt ssh roger@ubuntu

The output confirms that the remote host has been successfully login using non-Interactive ssh login.

Conclusion

Linux offers the “sshpass -p [password] ssh [user]@[host]” command to connect to the remote host using a non-interactive SSH login. Users can also establish a connection by reading passwords from the file. These two methods are useful to provide an easy and secure way of automating the management of remote servers and network devices. This guide has described all possible methods to use the “sshpass” command for non-interactive ssh login.