Communicating between 2 Raspberry Pis using sshpass

Communicating between 2 Raspberry Pi's using sshpass
Published: by
Last updated:
Categories
Raspbian

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.

Introduction

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 pi@192.168.1.xx '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.

Sponsored links

Please support us by using these links they come at no additional cost to you, but we get a little commission each time they are used. These and others are available from Amazon.com and BangGood.

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.)

Catda Raspberry Pi 4B Starter Kit
Catda Raspberry Pi 4B Starter Kit
2019 Model B, 4GB RAM
1.5GHz 64-bit quad-core CPU & 4GB RAM, Gigabit Ethernet, Wi-Fi, Bluetooth, USB 2.0/3.0, headphone jack and duel Micro HDMI (4K). Kit includes power supply, MicroSD, USB MicroSD card reader, a case with heatsink and fan, mini-HDMI cable and USB-C PiSwitch.
Available from BangGood
iUniker Raspberry Pi 4B 8GB Starter Kit
iUniker Raspberry Pi 4B 8GB Starter Kit
2019 Model B, 8GB RAM
1.5GHz 64-bit quad-core CPU & 8GB RAM, Gigabit Ethernet, Wi-Fi, Bluetooth, USB 2.0/3.0, headphone jack and duel Micro HDMI (4K). Kit includes power supply, MicroSD, USB MicroSD card reader, a case with heatsink and fan, mini-HDMI cable and USB-C PiSwitch.
Available from Amazon.com
iUniker Raspberry Pi 4B 4GB Starter Kit
iUniker Raspberry Pi 4B 4GB Starter Kit
2019 Model B, 4GB RAM
1.5GHz 64-bit quad-core CPU & 4GB RAM, Gigabit Ethernet, Wi-Fi, Bluetooth, USB 2.0/3.0, headphone jack and duel Micro HDMI (4K). Kit includes power supply, MicroSD, USB MicroSD card reader, a case with heatsink and fan, mini-HDMI cable and USB-C PiSwitch.
Available from Amazon.com

Before starting

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 /home/pi directory:

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 /home/pi/.ssh to /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:

  1. The system’s IP address (use hostname -I)
  2. The host name (use hostname)

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 /home/pi directory

nano /home/pi/hostname.txt

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 pi@192.168.1.16:/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 pi@192.168.1.16:/home/pi/hostname.txt /home/pi/hostname.txt

where pi and 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)?

Type in yes.

Warning: Permanently added '192.168.1.16' (ECDSA) to the list of known hosts.
username@192.168.1.16'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 /.ssh directory).

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 pi@192.168.1.17 'your command here'

where raspberry, pi and 192.168.1.17 are the password, username and IP address of the ‘local’ Raspberry Pi.

Conclusion

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.

About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of our content, please consider making a donation to our cause, or becoming a Patron for exclusive content.
Donate via PayPal Become a Patron

Save, share & discuss

Your comment is important, but don't be a knob. Keep it constructive and polite.

Leave a Reply

Your email address will not be published. Required fields are marked *

More Raspbian posts