Latest Windows 10 and 11 have a built-in OpenSSH Client for us to connect to SSH Servers e.g. Raspberry Pi and Ubuntu. By default, the SSH access required us to key in a password which is quite troublesome if always need remote ssh to them. Actually, we can enable passwordless SSH access using SSH Key (which contains public and private keys), so this post will write about this.
Install / enable Windows SSH client
If you haven't enabled the SSH Client, you can enable it in Settings > Apps > Optional features.
Enable SSH on Raspberry Pi
On your Raspberry Pi, if you haven't enabled SSH, run sudo raspi-config
and follow the steps below:
1. Interface Options
2. P2 SSH, select Yes to enable the SSH server.
Enable SSH on Ubuntu
On Ubuntu, run the commands below to install the SSH server if you haven't installed the SSH server.
sudo apt update sudo apt install openssh-server # Use the command below to double-check the SSH installed correctly and started. sudo systemctl status ssh
Checking for existing SSH Key
Open Windows Powershell and key in the commands below to check whether your Windows already have an existing SSH Key. If yes, you can skip the Generate new SSH Key.
ls ~/.ssh
If you are able to see something like id_xxx
(private key) and id_xxx.pub
(public key), then your system already has the SSH Key. If the folder does not exist or the folder is empty, then you need to Generate new SSH Key in the next section.
Generate new SSH Key
Enter the following command to generate a new SSH key. You will be asked to enter a password for the file which is optional, enter it to encrypt the private SSH key.
ssh-keygen
After the step above is done, enter the ls ~/.ssh
again and now you should be able to see new files inside the folder already.
Copy SSH Key to Raspberry Pi / Ubuntu
Run the following command to copy the SSH public key to Raspberry Pi or Ubuntu. Replace the [Pi-Ubuntu-UserId]
with the Pi/Ubuntu user id and [Pi-Ubuntu-Ip-Address]
with the Pi/Ubuntu IP address.
cat ~/.ssh/id_rsa.pub | ssh [Pi-Ubuntu-UserId]@[Pi-Ubuntu-Ip-Address] 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys' # e.g. # cat ~/.ssh/id_rsa.pub | ssh pi@192.168.0.2 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'
Remote to Raspberry Pi / Ubuntu
After all the above, the installation and configurations were completed. You can try to connect to the SSH server using the command below. Remember to replace the IP address 192.168.0.2 with your Pi / Ubuntu IP address. It should be now connected without the need to enter a password and user id.
ssh 192.168.0.2
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.