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


    [string]$Computer = '',
    [string]$User = 'root'
$oldLocation = Get-Location
Set-Location -Path $env:USERPROFILE

It will check if under folder path <your profile folder>\.ssh files id_rsa and 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
    Write-Host 'INFO: <yourprofile>/.ssh/id_rsa already exist, skipping...' -ForegroundColor Cyan

In the next lines of command file will be copied over SCP protocol on linux server at as ~/ 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 ~/ 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/ to $Computer, Type password`n" -ForegroundColor Cyan
scp.exe -o 'StrictHostKeyChecking no' "$" "${remoteSSHServerLogin}:~/"
Write-Host "INFO: Updating authorized_keys on $Computer, Type password`n" -ForegroundColor Cyan
ssh.exe "$remoteSSHServerLogin" "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat ~/ >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && rm -f ~/"

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 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 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 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


Blog Search

Page Views


Follow me on Blogarama