Steps to Set Up SSH Key Authentication Between Windows and Raspberry Pi (So You Don’t Have to Enter the Password)
- Generate SSH Keys on Windows (If Not Done Already)
You should have generated the SSH key pair on your Windows machine (the one you’re using for VSCode).
Open PowerShell (or Command Prompt) on your Windows machine.
Run the following command to generate an SSH key pair:
ssh-keygen -t rsa -b 4096
Follow the prompts:
Press Enter to accept the default location for the key (C:\Users\<YourUserName>\.ssh\id_rsa).
Optionally, enter a passphrase, or leave it blank for no passphrase (easier but less secure).
Now you should have two files in your C:\Users\.ssh directory:
id_rsa (your private key)
id_rsa.pub (your public key)
- Copy the Public Key to the Raspberry Pi
You need to copy the public key (id_rsa.pub) to the Raspberry Pi so that it can authenticate you.
Option 1: Using ssh-copy-id (from Windows with Git Bash)
If you have Git Bash installed on your Windows machine, you can use the ssh-copy-id tool to copy the public key to the Raspberry Pi:
Open Git Bash on Windows.
Run the following command:
ssh-copy-id pi@<RPI_IP_ADDRESS>
Replace <RPI_IP_ADDRESS> with the actual IP address of your Raspberry Pi.
It will prompt you for the Raspberry Pi password. After entering it, your key will be copied.
Option 2: Manually Copy the Key
If ssh-copy-id is not available, follow these manual steps:
Copy the public key:
Open id_rsa.pub (located in C:\Users\<YourUserName>\.ssh\id_rsa.pub) with a text editor, and copy its entire content.
Log into the Raspberry Pi:
Open PowerShell or Command Prompt on Windows, and run:
ssh pi@<RPI_IP_ADDRESS>
Enter the Raspberry Pi password when prompted.
Paste the public key into the authorized_keys file:
On the Raspberry Pi, run:
mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys
Paste the contents of id_rsa.pub into the authorized_keys file.
Save and exit (Ctrl + X, then press Y to confirm, and Enter).
Set the correct permissions:
Run the following commands on the Raspberry Pi to ensure the permissions are correct:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
- Configure SSH to Use Key Authentication (on the Raspberry Pi)
Now, ensure that the SSH server on the Raspberry Pi is configured to allow key-based authentication and disable password authentication (if you want to fully rely on SSH keys).
Edit the SSH configuration file on the Raspberry Pi:
sudo nano /etc/ssh/sshd_config
Ensure the following lines are present and not commented out (# means commented out):
PubkeyAuthentication yes
PasswordAuthentication no (This line will disable password authentication, forcing key-based login)
AuthorizedKeysFile .ssh/authorized_keys
Restart the SSH service to apply changes:
sudo systemctl restart ssh
- Test the SSH Connection
Test that SSH key authentication is working by attempting to log in again from your Windows machine:
Open PowerShell or Command Prompt on your Windows machine.
Run the following command:
ssh pi@<RPI_IP_ADDRESS>
You should now be able to log in without a password prompt. If everything is set up correctly, SSH should use the key authentication, and you'll be logged in automatically.
- Set Up VSCode to Use SSH
Now that SSH key authentication is working, you can connect from VSCode:
Install the Remote - SSH extension in VSCode if you haven't already.
Configure the SSH connection in VSCode:
Open Command Palette (Ctrl + Shift + P) in VSCode.
Type Remote-SSH: Open SSH Configuration File.
Add an entry for your Raspberry Pi:
Host raspberrypi
HostName <RPI_IP_ADDRESS>
User pi
IdentityFile C:\Users\<YourUserName>\.ssh\id_rsa
Connect to the Raspberry Pi:
Open Command Palette (Ctrl + Shift + P) in VSCode again.
Select Remote-SSH: Connect to Host... and choose your Raspberry Pi entry.
Now, VSCode should connect to your Raspberry Pi without asking for a password each time you reboot your Raspberry Pi.
- Troubleshooting If you’re still being asked for a password, check that:
The private key (id_rsa) is being correctly used.
The Raspberry Pi’s SSH server is configured to allow key-based authentication (PubkeyAuthentication yes).
The authorized_keys file on the Raspberry Pi has the correct permissions (chmod 600 ~/.ssh/authorized_keys).
With this setup, you should no longer be prompted for a password after rebooting the Raspberry Pi!