How to establish a passwordless SSH Connection between Windows and Linux
In this article you will learn how to create a passwordless SSH connection between a Windows based and a Linux based machine. This is especially useful if you have to ssh multiple times between devices and to use the scp command without having to enter the passwords over and over.
Windows to Linux
Open up a Powershell Terminal and enter the following command:
> Get-WindowsCapability -Online | ? Name -like ‘OpenSSH*’
If either OpenSSH.Client or OpenSSH.Server have State set to NotPresent then enter:
//for Client
> Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0//for Server
> Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
After adding both capabilities enter the following commands in the same Powershell Terminal:
> Start-Service sshd
> Set-Service -Name sshd -StartupType ‘Automatic’
> Get-Service ssh-agent | Set-Service -StartupType Manual
Now generate a public/private key pair if you don’t have one.
Head over to C:Users/You/.ssh
- if two files (id_rsa and id_rsa.pub) are present you don’t need to generate a new key pair.
- if the files are not present, then in the same Powershell Terminal as before enter:
> ssh-keygen.exe
Press Enter when asked for a passphrase as we don’t want it to be passphrase protected.
After the key pair has been created, copy the public key to the remote Linux device using the scp command:
>scp C:/Users/You/.ssh/id_rsa_1.pub remoteDeviceName@remoteDeviceIP:/path/to/.ssh/
//make sure the path to the .ssh folder exists in the remote device. //If not just created it using mkdir
It will look something like this:
Now open a command prompt and SSH into the remote Linux device and navigate to the location of the .ssh folder.
Once there, ensure that the id_rsa.pub file from the windows device is present there. Ensure the authorized_keys file exists in the same directory, if not create it using the command:
~/.ssh# touch authorized_keys
Copy the id_rsa.pub file into the authorized_keys file with the following command:
~/.ssh# cp id_rsa.pub authorized_keys
To see if the passwordless connection between Windows machine and the remote Linux machine is working, open a new command prompt and SSH into the remote Linux machine again. This time it should SSH directly without asking for a password.
Linux to Windows
SSH into the Linux device, navigate to the .ssh directory (create it if not present). If no key pair is present, then enter the following command:
~# ssh-keygen
After the key pair is created, copy the id_rsa.pub file onto the Windows machine with:
~/.ssh# scp id_rsa.pub WindowsMachineUsername@WindowsMachineIP:C:\ProgramData\ssh\administrators_authorized_keys
Back in the Windows device, open a Powershell terminal and execute the following command:
icacls.exe "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"
to ensure that the ACL is correct.
Now you should be able to SSH and SCP between the devices without having to enter the respective passwords.