Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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.

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 \.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 /.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 /.ssh directory' -ForegroundColor Cyan
     }
     ssh-keygen.exe -t rsa -b 4096 -N '""' -f "./.ssh/id_rsa"
     Write-Host 'INFO: Generated  /.ssh/id_rsa file' -ForegroundColor Cyan
} 
else 
{
    Write-Host 'INFO: /.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.

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.

\

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



This post first appeared on Tales From Real IT System Administrators World And Non-production Environment, please read the originial post: here

Share the post

How to Setup Passwordless SSH Login on Windows

×

Subscribe to Tales From Real It System Administrators World And Non-production Environment

Get updates delivered right to your inbox!

Thank you for your subscription

×