Windows – passwordless ssh from linux to windows

cygwin;sshwindows

enter image description hereenter image description here

I am trying to login to Windows without password from a Linux server. I have already installed OpenSSH from GitHub and I'm able to do scp and ssh. I tried copying the authorized_keys to the Windows location. But it's still not working.
The functionality should be no password prompt for running ssh or scp from linux environment to login/show windows directory.

I tried below commands:

cat .ssh/id_rsa.pub | ssh edh_win@xxx.xxx.xxx.xxx 'cat >> .ssh/authorized_keys'

ssh edh_win@xxx.xxx.xxx.xxx "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"

But getting error not able to understand cat and chmod.

Updating the errors

'cat' is not recognized as an internal or external command,
operable program or batch file.

'chmod' is not recognized as an internal or external command,
operable program or batch file.

Do I need to install cygwin? If yes, please help with the implementation.

Any assistance is appreciated. Below is an image of the error.

image of the error

Best Answer

Steps to establish passwordless SSH between Linux ⬌ Windows:

Note:

  • Open a PowerShell console with Administrator privileges and execute all the commands mentioned below in that console only
  • Depending on install path, add C:\Windows\System32\OpenSSH or C:\Program Files\OpenSSH to the System Path

Windows Server 2019:

  • Ensure the system is up to date via Windows Update
  • Ensure OpenSSH features are installed:
    • Apps & Features > Manage Optional Features
    • OpenSSH Server and OpenSSH Client should be listed, if they are not: Add a Feature

Windows Server 2012 and 2016:

  1. Download OpenSSH (OpenSSH-Win64.zip)
  2. Extract the contents to C:\Program Files\OpenSSH and enter directory
  3. Follow steps 4 - 6 mentioned in the Install Wiki:

    # In an elevated Powershell console, run the following:
      powershell -ExecutionPolicy Bypass -File install-sshd.ps1
    
    # Open the firewall for sshd.exe to allow inbound SSH connections
      New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
    
    # Start sshd (this will automatically generate host keys under %programdata%\ssh if they don't already exist)
      net start sshd ; net start ssh-agent
    

Common Steps for Windows Server 2012/2016/2019:

  1. Execute the following, which should show the status as Running for both services:

    Set-Service ssh-agent -StartupType Automatic
    
    Set-Service sshd -StartupType Automatic
    
    Get-Service -Name ssh-agent,sshd
    

    If not running: open Services and start OpenSSH Server and OpenSSH Authentication Agent

  2. For public-private key pair generation, issue ssh-keygen and follow the prompts
  3. Create C:\ProgramData\ssh\administrators_authorized_keys:
    New-Item -ItemType file "C:\ProgramData\ssh\administrators_authorized_keys"
    
  4. Append /root/.ssh/id_rsa.pub to C:\ProgramData\ssh\administrators_authorized_keys
    • If id_rsa.pub does not exist on Linux, generate via: ssh-keygen
  5. Append C:\Users\Administrator\.ssh\id_rsa.pub to /root/.ssh/authorized_keys
    • If authorized_keys does not exist:
      touch "/root/.ssh/authorized_keys"
      
  6. For permission settings:

    icacls "C:\ProgramData\ssh\administrators_authorized_keys" /remove "NT AUTHORITY\Authenticated Users"
    
    icacls "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r
    
    Restart-Service -Name sshd, ssh-agent -Force
    

Relevant locations on Windows host:

  • C:\Windows\Sytem32\OpenSSH\
  • C:\Program Files\OpenSSH\
  • C:\Users\Administrator\.ssh\
  • C:\ProgramData\ssh\

References:

Related Question