Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

How to Setup Passwordless SSH Login on Windows

Since I upgraded my laptop to Windows 10, I started using inbuilt tool SSH.exe to connect openssh servers (linux) instead of external tools like putty.exe. Here whenever I want to connect to linux through ssh, it prompts for password each time for new login. To make linux passwordless connection over ssh, you will need to copy/add rsa key certificate on linux from windows server. Below break down of the script will show how to use the script.

Below part of code requires parameter Computer (Linux) and User (root) and set folder location to user path.

configure remote ssh extension on visual studio code
Powershell execution policy setting is overridden by a policy defined at a more specific scope

[CmdletBinding(
    SupportsShouldProcess=$True,
    ConfirmImpact='Medium',
    HelpURI='http://vcloud-lab.com',
    DefaultParameterSetName='Manual'
)]

param 
(
    [string]$Computer = '192.168.34.13',
    [string]$User = 'root'
)
"`n"
$oldLocation = Get-Location
Set-Location -Path $env:USERPROFILE

It will check if under folder path <your profile folder>\.ssh files id_rsa and id_rsa.pub are exist, if files do not exist they are generated using ssh-keygen.exe (inbuilt tool) with 4096 bit.

Write-Host 'INFO: Checking <yourprofile>/.ssh/id_rsa exists' -ForegroundColor Cyan

if (-not(Test-Path -Path "./.ssh/id_rsa"))
{
     if (-not(Test-Path -Path "./.ssh"))
     {
          [void](New-Item -Path "./" -Name .ssh -ItemType Directory -Force)
          Write-Host 'INFO: Created <yourprofile>/.ssh directory' -ForegroundColor Cyan
     }
     ssh-keygen.exe -t rsa -b 4096 -N '""' -f "./.ssh/id_rsa"
     Write-Host 'INFO: Generated  <yourprofile>/.ssh/id_rsa file' -ForegroundColor Cyan
} 
else 
{
    Write-Host 'INFO: <yourprofile>/.ssh/id_rsa already exist, skipping...' -ForegroundColor Cyan
} 

In the next lines of command id_rsa.pub file will be copied over SCP protocol on linux server at as ~/tmp.pub location. Files contents will be appended to on the linux server to logged in users path ~/.ssh/authorized_keys, modified files permissions to 600 and ~/tmp.pub is deleted. 

It will prompt linux user password twice while using ssh.exe and scp.exe.

$id_rsa_Location = "$env:USERPROFILE/.ssh/id_rsa" 
$remoteSSHServerLogin = "$User@$Computer"

Write-Host "INFO: Copying /.ssh/id_rsa.pub to $Computer, Type password`n" -ForegroundColor Cyan
scp.exe -o 'StrictHostKeyChecking no' "$id_rsa_Location.pub" "${remoteSSHServerLogin}:~/tmp.pub"
Write-Host "INFO: Updating authorized_keys on $Computer, Type password`n" -ForegroundColor Cyan
ssh.exe "$remoteSSHServerLogin" "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat ~/tmp.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && rm -f ~/tmp.pub"

Set-Location -Path $oldLocation
Write-Host "`nINFO: Try SSH to $Computer Now it will not prompt for password" -ForegroundColor Green

This is the screenshot looks like when I will execute the ps1 script with parameters.

Windows 10 to Linux SSH passwordLess configuration id_rsa .ssh id_rsa.pub randomart ecdsa known_hosts ssh scp authorized_keys ssh.exe scp.exe passwordless configuration ssh private public keys.png

You can download this script from here Windows 10 to Linux SSH PasswordLess configuration.ps1 or it is available on gitbub.com. Below screenshot shows the location of id_rsa files and authorized_keys. While login to linux it will not prompt for password and authentication will be done using certificate keys automatically.

user profile .ssh authorized_keys  id_rsa id_rsa.pub known_hosts linux passwordless ssh keys configuration from windows with ssh.exe and scp.exe linux no password.png

Useful Articles
Docker Error response from daemon i\o timeout internet proxy
Cannot connect to the Docker daemon at unix:var run docker.sock. Is the docker daemon running
How to install Docker on Linux
How to install Ansible on Linux for vSphere configuration

Go Back

Comment

Blog Search

Page Views

3238623

Follow me on Blogarama