Skip to content

Instantly share code, notes, and snippets.

@kradalby
Last active May 2, 2020 19:46
Show Gist options
  • Save kradalby/5dfe3b761ad5c3f75afcebf8f51094fa to your computer and use it in GitHub Desktop.
Save kradalby/5dfe3b761ad5c3f75afcebf8f51094fa to your computer and use it in GitHub Desktop.
Install SSH on Windows
# Run with:
# iex ((New-Object System.Net.WebClient).DownloadString('URL'))
# Install the OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
# Install the OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
# Confirm the Firewall rule is configured. It should be created automatically by setup.
Get-NetFirewallRule -Name *ssh*
# There should be a firewall rule named "OpenSSH-Server-In-TCP", which should be enabled
# If the firewall does not exist, create one
# New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
# Allow PubKey auth
Write-Output "PubkeyAuthentication yes" | Add-Content $env:PROGRAMDATA\ssh\sshd_config -encoding ASCII
# Remove broken lines
(Get-Content $env:PROGRAMDATA\ssh\sshd_config) -notmatch "Match Group administrators" | Out-File -FilePath $env:PROGRAMDATA\ssh\sshd_config -Encoding ASCII
(Get-Content $env:PROGRAMDATA\ssh\sshd_config) -notmatch "AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys" | Out-File -FilePath $env:PROGRAMDATA\ssh\sshd_config -Encoding ASCII
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
Restart-Service sshd
New-Item -ItemType Directory -Force -Path ~/.ssh
Invoke-WebRequest https://github.com/kradalby.keys -OutFile ~/.ssh/authorized_keys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment