sshpass is a Linux package that can establish a direct, secure connection between two Linux-operated computers. It uses ssh that allows remote access from a local computer.
Table of contents
In the case where the Raspberry Pi OS (formerly known as Raspbian) is installed, sshpass can be used to gain terminal access from one Raspberry Pi to another Raspberry Pi.
Case study 1
A Raspberry Pi that is connected to a keyboard and screen is used to connect to a Raspberry Pi that is not.
Case study 2
In a home automation system, one Raspberry Pi is used to compute certain functions and, depending on the outcome, it uses sshpass in the background to trigger a command on a different Raspberry Pi.
There are other ways to connect between two Linux computers, e.g. using PuTTY, but might need manual execution. sshpass commands can be used with Bash scripts and Python scripts to automate commands. Commands can also be executed in a screen session.
An example command using sshpass would be:
sudo sshpass -p 'raspberry' ssh email@example.com 'local command here'
Requirements / assumptions
Both Raspberry Pis are running Raspberry Pi OS (Raspbian) and are connected to the same local area network (LAN).
If no keyboard and screen are available, a connection using PuTTY can be used.
For sshpass to work, SSH needs to be enabled on both Raspberry Pis.
Although not a must, a static IP address to at least the remote Raspberry Pi is recommended.
We have tested most of these products ourselves. Other selection criteria include affordability, quality, availability and average user rating and popularity by other buyers.
Links will open in a new window. (Dead or old links can be reported in the comments section below.)
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 this case, this directory is already available and there are files present, there are already connections that have 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 contents 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) have 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 the following terminal command:
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
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.
sshpass is a Linux software package that uses SSH that can establish a direct, secure connection between two Raspberry Pis. It allows remote access from a local Raspberry Pi that can run commands manually or in the background.