Skip to content

Instantly share code, notes, and snippets.

@aruaam
Last active October 28, 2023 10:37
Show Gist options
  • Save aruaam/942073b358ab74ad9668380f068652c0 to your computer and use it in GitHub Desktop.
Save aruaam/942073b358ab74ad9668380f068652c0 to your computer and use it in GitHub Desktop.
How to configure passwordless SSH access to a Windows host
# Run commands in PowerShell as admin
# Remove default ssh components (restart is needed afterwards)
# Do not run this on a production server if OpenSSH is already in use there
Remove-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Remove-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
# Install fresh openssh server with Chocolatey
choco install openssh -params '"/SSHServerFeature /KeyBasedAuthenticationFeature /SSHAgentFeature"' -y
# Add client's public key to administrators_authorized_keys and fix permissions
# Replace 'your_admin' with the admin username used on your server
$authorizedKey = Read-Host "Enter SSH public key from the client"
Add-Content -Force -Path $env:ProgramData\ssh\administrators_authorized_keys -Value $authorizedKey
icacls.exe ""$env:ProgramData\ssh\administrators_authorized_keys"" /inheritance:r /grant ""Administrators:F"" /grant ""SYSTEM:F""
icacls.exe ""$env:ProgramData\ssh\administrators_authorized_keys"" /setowner ""your_admin""
# Uninstall command if it's necessary to repeat the procedure
choco uninstall openssh -params '"/SSHServerFeature /KeyBasedAuthenticationFeature /SSHAgentFeature"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment