Skip to content

The Pi Network

Luis Rivera edited this page Mar 7, 2020 · 43 revisions

The Pi Network

The so called Local Area Network (LAN). In order for the cluster to communicate, we need to set up a LAN switch so that each Pi board knows where to send and receive data, commands, and output results.

Network Lookup Table

Let's enumerate each Pi and outline the network configuration for reference.

Pi Number Interface IP Address Hostname
0 eth0 192.168.0.100 master
1 eth0 192.168.0.101 worker1
2 eth0 192.168.0.102 worker2
3 eth0 192.168.0.103 worker3
4 eth0 192.168.0.104 worker4

You guessed right! There is a "master" node that will oversee and control the cluster while each "worker" executes whatever command the master issued.

Assign a static IP address

To facilitate communication between the cluster, let's assign a static local IP address to each Raspberry Pi board in the network switch.

Edit file /etc/dhcpcd.conf and modify the following lines:

interface eth0
static ip_address=192.168.0.10X/24

Where X represents the Pi Number in the Network Lookup Table.

Reminder: You have to edit this file manually on each Raspberry Pi.

Change Hostname

Raspbian by default, sets the hostname of each Pi to raspberrypi. Such hostname is not mapped to the IP address given it can change via DHCP but since we assigned a static IP address we can map it. Mapping the hostname to the static IP will ease communication between the network.

Rename the hostname

Edit file /etc/hostname and replace raspberrypi hostname with the new one for each Pi as specfied in the Network Lookup Table

sudo nano /etc/hostname

Reminder: You have to change the hostname manually on each Raspberry Pi.

By changing the hostname to master and workerX you will be able to distinguish between the nodes.

Mapping the hostname

To map each Pi's IP address to it's new respective hostname, edit file /etc/hosts and add the following:

192.168.0.100    master
192.168.0.101    worker1
192.168.0.102    worker2
192.168.0.103    worker3
192.168.0.104    worker4

Reminder: You have to do this manually on each Raspberry Pi.

All of your Raspberry Pi's must have each of their IP addresses mapped to their hostnames in the /etc/hosts file so that each Pi can fully communicate with each other.

Reboot the Pi

Reboot system for changes to take effect

shutdown -r now

Enable Secure Shell (SSH)

SSH is a network protocol for operating network services securely such as log into a remote computer, execute commands, tunneling, forwarding TCP ports and transfer files (via secure copy or scp).

We need SSH for each Pi to communicate with each other and be able to execute commands coming from the Master node.

Enable SSH

SSH is disabled by default as of latest Raspbian versions. Thus, we need to enable it but first, you must change your default password.

To change your default password, simply open a terminal window and type in passwd then hit Enter. Type in your new password and confirm it. You can use the same password for all Pi's or a custom one per Pi, up to you.

Enable via Terminal

Open a terminal window and type in

sudo systemctl enable ssh

Enable via Desktop

  1. Go to Preferences menu and open Raspberry Pi Configuration
  2. Head over to the Interfaces tab
  3. Select Enabled right next to SSH
  4. Click OK
  5. Start the service

Enable via raspi-config

  1. Open a terminal window and type in sudo raspi-config
  2. Select Interfacing Options
  3. Navigate to SSH and select it
  4. Choose Yes
  5. Select Ok
  6. Choose Finish

Start SSH

To start up the SSH service, run the following command

sudo systemctl start ssh

How to Login Remotely

With the hostname mapped to each IP address on each Pi and SSH enabled, remote login becomes easier

ssh pi@worker3

If that is your first time remotely logging in to worker3, it will ask you if you trust the destination, type in yes and hit Enter.

The problem here is that everytime you log in remotely to any other Pi, it will ask you for that Pi's password. Not cool!

Generate SSH Keys

To avoid typing in the password of the computer we're trying to remotely log in to, we create what is called private and public key-pairs.

The private key is solely for the computer in which the key is being generated (not to be shared) while the public key can be shared among other computers for these to be able to securely login remotely without entering a password.

Generating Keys

To generate private/public keys, run this command

ssh-keygen -t ed25519

Press Enter twice.

That will generate a private and public key-pair within the ~/.ssh hidden directory. The private key is stored in file ~/.ssh/id_ed25519 and the public key is in file ~/.ssh/id_ed25519.pub. DO NOT TOUCH THE PRIVATE KEY!

Reminder: You have to generate keys manually on each Raspberry Pi.

Append to Authorized Keys File

Now, each public key will have to be appended to file ~/.ssh/authorized_keys on every Pi.

The simplest way to accomplish this is to generate all keys on each Pi (including the master) then append each public key to the master node. Once the master has access to all public keys, including it's own, you can copy (overwrite) this file to all workers.

First, on the master, we need to copy the master's public key to it's own authorized_keys file

cat ~/.ssh/id_ed22519.pub >> ~/.ssh/authorized_keys

Next, run the following command on each worker

cat ~/.ssh/id_ed22519.pub | ssh pi@master 'cat >> ~/.ssh/authorized_keys'

Enter the password when prompted.

Then, to replicate this work on all workers, simply copy over the master's authorized_keys file to all workers

scp ~/.ssh/authorized_keys pi@workerX:~/.ssh/authorized_keys

Where X represents the Pi Number in the Network Lookup Table.

You should now be able to log in remotely to any Pi on the network from any other Pi.