sshpass is a Linux software package that can be used to establish a direct (secure) connection between two Linux operated computers. It uses the ssh protocol. This connection allows ‘remote’ access to a ‘local’ computer to run commands ‘locally’.
Using sshpass is a great way to run (background) terminal commands from one Raspberry Pi to another. This method can be used for various functions, but is especially helpful running commands on a Raspberry Pi web server.
Of course there are other ways to connect between two Linux computers. One such way is for example using PuTTY, but that needed manual execution. sshpass comands can be used with Bash scripts to automate commands.
An example command using sshpass would be:
sudo sshpass -p 'raspberry' ssh email@example.com 'local command here'
sshpass is great for triggering fast, one-liner commands, and for example Bash and Python scripts. Alternatively, longer or continual commands, like for example streaming internet radio, can be executed in a screen session.
Requirements / assumptions
Both Raspberry Pis are running Raspbian and is connected to a local area network (LAN). If no keyboard and screen is available, a connection through a PuTTY terminal can be used. A static IP to at least the ‘remote’ Raspberry Pi is recommended.
Remote vs. local
In this post ‘remote’ and ‘local’ will refer to what the Raspberry Pis are to be used for after the setup is complete – even though technically both will be accessed remotely if for example PuTTY is used. The remote Raspberry Pi will be the device where the commands are executed on, whereas the local Raspberry Pi will be the device where the commands are executed from.
SSH system files
In order for sshpass to work, and Raspbian to allow and to remember which remote user connection can be trusted or not, authorisation will be required during the first connection attempt.
When authenticity has been successfully established between two systems, sshpass commands can be triggered without having to supply a/the password every time.
The authorised settings will be saved in the form of 4 files in the
/home/pi/.ssh directory. If no remote hosts are authorised yet, (e.g. on a freshly installed Raspbian) this directory will be absent:
cd /home/pi/.ssh dir
In the case of a “
-bash: cd: /home/pi/.ssh: No such file or directory” error, no connections are authorised yet. In the case this directory is already available and there are files present, there are already connections that has been authorised.
If absent, the
/.ssh directory can be created by running the following from the
cd /home/pi install -d -m 700 ~/.ssh
To make a backup of the
/.ssh directory use:
sudo cp -rp /home/pi/.ssh /sshbackup-directory
This will copy the contensts of
/sshbackup-directory. A different backup directory can be used.
Setting up Raspbian to use sshpass
To establish authenticity between the ‘remote’ and ‘local’ Raspberry Pis, I recommend creating a ‘local’ file which is then copied to the ‘remote’ Raspberry Pi. To make things easier, this file needs to be in the same directory on both systems.
This will enable the initial process to create the correct SSH system file(s) to allow future connections without the need of a password – making the process more automated. By using a text file, it can be also be used in future to see what systems (hostnames) has been authorised on the ‘local’ Raspberry Pi.
The ‘remote’ Raspberry Pi
From the Raspberry Pi where the commands are to be executed on, apart from the username and password and a text file that will be copied, the following will also be required:
- The system’s IP address (use
- The host name (use
For explanation purposes, the username will be
pi, the password will be
raspberry,the IP address will be
192.168.1.16 and the host name will be
hostname. Make sure to replace them with your own settings.
After logging into the ‘remote’ Raspberry Pi’s terminal, create a text file in the
and copy the following text (with your own details) into the file before exiting and saving (Ctrl + X, then Y):
To get this file and enable SSH key sudo scp firstname.lastname@example.org:/home/pi/hostname.txt /home/pi/hostname.txt If this file is on another Pi, the SSH key is probably already enabled.
The ‘local’ Raspberry Pi
From the Raspberry Pi where the commands are executed from, apart from sshpass the username, password and IP address of the ‘local’ Raspberry Pi will also be required.
sshpass can be installed with:
sudo apt-get install sshpass
Secure copy (SCP) can be used to copy the previously created text file from the ‘remote’ Raspberry Pi:
sudo scp email@example.com:/home/pi/hostname.txt /home/pi/hostname.txt
192.168.1.16 are the username and IP address of the ‘remote’ Raspberry Pi.
Authenticity might already be established, but in the likely event that it is not, the following message will show:
The authenticity of host '192.168.1.16 (192.168.1.16)' can't be established. ECDSA key fingerprint is 1f:cd:15:39:... . Are you sure you want to continue connecting (yes/no)?
Warning: Permanently added '192.168.1.16' (ECDSA) to the list of known hosts. firstname.lastname@example.org's password:
Type the password for the ‘remote’ Raspberry Pi (in this case it’s
raspberry). The file will be copied to the ‘local’ Raspberry Pi and the authentication of the ‘remote’ Raspberry Pi will be permanently established (and saved in one of the files in the
Using sshpass between two Raspberry Pis
As previously discussed, sshpass will run commands on a local Raspberry Pi after being triggered remotely. To use sshpass from the ‘remote’ Raspberry Pi:
sudo sshpass -p 'raspberry' ssh email@example.com 'your command here'
192.168.1.17 are the password, username and IP address of the ‘local’ Raspberry Pi.