How to Create an SSH Key Pair on Raspberry Pi with Ubuntu
Ohidur Rahman Bappy
MAR 22, 2025
Introduction
Setting up SSH key authentication is a security best practice and can save you from having to input your password every time you access your Raspberry Pi running Ubuntu. This post will guide you through creating an SSH key pair and configuring your Raspberry Pi to use it.
Step 1: Ensure Authorized Keys Are Writable
First, ensure that the authorized_keys
file on your Raspberry Pi is writable:
chmod 600 /home/ubuntu/.ssh/authorized_keys
Step 2: Generate the SSH Key Pair on Your Client
To create a new SSH key pair, open your terminal and execute:
ssh-keygen
Accept the defaults by pressing Enter.
By default, your new key pair will be saved in the ~/.ssh
directory.
Step 3: Copy SSH Public Key to Raspberry Pi
With the key pair generated, copy the public key to your Raspberry Pi using:
ssh-copy-id -i ~/.ssh/id_rsa pi@<YOUR_RASPBERRY_IP>
id_rsa
is the default SSH key name.pi
refers to the Raspberry Pi user.<YOUR_RASPBERRY_IP>
should be replaced with your Raspberry Pi's IP address.
Step 4: Test the SSH Connection
With the public key in place, test your SSH connection:
ssh -i ~/.ssh/id_rsa pi@<YOUR_RASPBERRY_IP>
If successful, you'll connect without needing a password.
Bonus: Scripted Login with rpi.sh
Create a script file rpi.sh
for easier access:
ssh ubuntu@<REMOTE_IP>
By default, this will use the key
~/.ssh/id_rsa
.
Run this script to log into your Raspberry Pi effortlessly.
Conclusion
Using SSH keys enhances security while providing convenient access to your devices. Ensure you keep the private key secure on your local machine.
By following these steps, you've set up your Raspberry Pi for secure, password-less SSH access!