Sshpass is a password authentication tool for Linux which allows you to automatically login to an SSH shell non-interactively. You can use the command to login to an SSH server in several different ways. A more common use for sshpass is inside a bourne again shell script. This allows you to login automatically if using a shortcut.
If you see an error message that states “sshpass: command not found”, it means that you don’t have the command in your system installed, or the path to the installation directory of sshpass is not properly set. In this article, you’ll learn how to fix this issue, and we’ll talk a little about a bourne again shell-only alternative to the sshpass command.
Install the sshpass command
Most linux distributions don’t have the sshpass preinstalled. Simply installing the program should be enough to get rid of the error message you see. To install sshpass, you need to use your distribution’s package manager. Now the thing here is that the command will differ from distro to distro. Some of the commands required to install the sshpass command in certain Linux Distributions are listed below:
$ sudo pacman -Syu $ sudo pacman -S sshpass
$ sudo apt-get update $ sudo apt-get install sshpass
$ sudo yum makecache $ sudo yum install sshpass
$ sudo dnf makecache $ sudo dnf install sshpass
The first line updates your system, which is very important. Sometimes, if you don’t update your system’s repositories, then you won’t be able to install the command and the manager may just get stuck. The next line is used to install the sshpass package. You’d be prompted to type y or n to decide whether or not you want to install the package. Type “y” to continue the installation.
If you’ve successfully been able to install the command, check whether it works by simply executing sshpass in your terminal:
If you see an output similar to the above, congratulations. You’ve successfully installed sshpass in your system and surpassed the error.
Install sshpass from source code
This is yet another method by which you can install the sshpass command in your Linux Machine. In case that your distribution’s repository does not have the sshpass package, or if you simply want more control over how your package is being installed, you can follow this method of installation. In this method, you’ll be building the package from its source code, by means of using a few other tools that will be required.
Before you start out, make sure that the following commands/programs are installed in your system:
The git command will be used to fetch the source code from a repository hosted in GitHub. If you don’t want to install this command in your machine, then you can directly go to the website of the repository and download a zipped version of the source code and then unzip it later on using the unzip command.
The make command is the more important one here. This is the program that you’ll be using to compile the program from its source code. Once you’ve made sure that you have these programs installed, you’re ready to proceed to the installation.
At first, fetch the source code from the repository using the git command:
$ git clone https://github.com/kevinburke/sshpass.git
After that, enter the sshpass directory that just got fetched by using the cd command:
$ cd sshpass
Now run the configure script found in this directory to prepare the files for compiling:
Afterwards, run the make command to compile the program from the source code:
Finally, install the sshpass command to make it available across the whole system using:
$ sudo make install
You should now successfully be able to run the sshpass command in your Linux Machine, try it out by executing sshpass as seen on the previous section.
If you used a different directory to install sshpass other than the default one, which is /usr/bin, than you need to add that directory to global PATH variable, so that bourne again shell can trace it when the terminal starts up. To do this, you need to add one line of code to the .bashrc file located in your home directory. You can also use the command directly from the terminal, but this would have a temporary effect. Which means, the next time you start the terminal, you’ll no longer be able to run sshpass, unless you run the command once again.
So to avoid this, add this like to the .bashrc file:
Make sure to replace the /path/to/installation/directory part with the actual path where you have installed sshpass from its source code. And wala! You can now execute the sshpass command from anywhere in your machine.
The sshpass.sh bourne again shell script
If you don’t want to install a whole program for this task, then there is a better way for you to achieve a similar result, without needing to use a whole program. If you’re using the bourne again shell or the zsh shell for your terminal, then there’s a small shell script called sshpass.sh which is purely written in the bourne again shell Script. This will not require you to install any programs, and you can directly execute the shell. As compared to the sshpass command, this is probably a lot lighter and a lot more hassle free. You can find the project by visiting this link:
You can also fetch the repository using git by using:
$ git clone https://github.com/huan/sshpass.sh
Then navigate to the directory:
$ cd sshpass.sh
Now you can simply execute the script by running it:
Learn more about how to use it by visiting their GitHub page.
I’m rest assured that you should now properly be able to run the sshpass command in your Linux Machine. Using the sshpass can save you a bunch of time when you constantly need to login to different ssh shells. You can simply bind the command inside a bourne again shell script to further automate the process, and be able to non-interactively login to SSH Shells, without shedding any sweat.