Communicating between 2 Raspberry Pis using sshpass

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

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

This is an ongoing post. Please suggest corrections, explanations, etc. in the comment section at the bottom of this page.

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

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

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 /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 contensts 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) 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:

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

where pi and 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 ' (' 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 '' (ECDSA) to the list of known hosts.
username@'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@ 'your command here'

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

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.
Donate via PayPal

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

Using the Raspberry Pi as a home web server
Using the Raspberry Pi as a home web server
18 January 2016 | Updated 19 November 2017
A web server is a computer that can send and receive data via a web browser. It uses, the very popular, Hypertext Transfer Protocol (HTTP), the basic network protocol used to distribute information on the internet. More…