passwordless ssh from linux to windows

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 [email protected] 'cat >> .ssh/authorized_keys'

ssh [email protected] "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


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:

  • Microsoft Docs: OpenSSH Key Management
  • Win32-OpenSSH Github: Install Wiki
  • Win32-OpenSSH Github: Logging Facilities

The errors say it all.

More or less, your ssh server provides... well, an ssh server. It dosen't have the 'unix' style or linux coreutils you're trying to run by default.

While swapping this ssh server for cygwin might help - what you literally need to do is understand what you're doing and not presume linux commands will work.

You can probably get cat on windows - through various native packages of it like the ones bundled with git or GOW

The permissions model probably works differently so you need to do it with native tools.

It needs some reading but this suggests "only System, Administrators and owner can have access"- and this post suggests you can use ICACLS to set the appropriate permissions.

The takeaway is - well you got to understand your tools and realise that you're not going to find the same environment everywhere.