How do I create and copy the SSH key from the Controller Pi to the Pi Zeros to allow passwordless logins?

~ 0 min

Assuming you haven't already created a public/private key on the Controller Pi run the following.

ssh-keygen -t rsa

Newer Raspberry Pi OS installations allow you to use your own username so please replace the "pi" user in the following commands with your username.

Press enter to save the key at the standard location (/home/pi/.ssh/id_rsa), you can optionally enter a passphrase but you will need to enter this every time you access the key.

Copy your public SSH key to the Pi Zeros 

for I in 1 2 3 4; do echo "Copying to p$I.local";ssh-copy-id [email protected]$I.local;done

For each Pi you will need to type "yes" to connect if you haven't already previously logged into the Zero from the Controller using SSH and then enter your password.

You will now be able to log into the Pi Zeros using SSH without any key

ssh [email protected]

To run a command on all Pi Zeros sequentially you can reuse a portion of the above command.

for I in 1 2 3 4; do echo -n "p$I:";ssh [email protected]$I.local uptime;done

You should see something similar to the output below which shows how long each Pi Zero has been booted, number of users, etc.

p1: 21:43:32 up 24 min,  3 users,  load average: 0.01, 0.02, 0.00
p2: 21:43:33 up 6 min,  2 users,  load average: 0.07, 0.08, 0.05
p3: 21:43:33 up 5 min,  2 users,  load average: 0.00, 0.07, 0.04
p4: 21:43:34 up 4 min,  2 users,  load average: 0.02, 0.11, 0.06
Average rating 3.5 (6 Votes)

You cannot comment on this entry