Skip to content

Instantly share code, notes, and snippets.

@GiovanniGrieco
Last active December 6, 2022 17:14
Show Gist options
  • Save GiovanniGrieco/5b1f8488290e9528bd9e6ebdad9e09ea to your computer and use it in GitHub Desktop.
Save GiovanniGrieco/5b1f8488290e9528bd9e6ebdad9e09ea to your computer and use it in GitHub Desktop.
Install OpenSSH Server on Windows
# credits: https://www.medo64.com/2019/12/authorized-keys-in-windows-10-openssh/
# Start as admin
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
# OpenSSH: Install
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Set-Service -Name sshd -StartupType Automatic
Start-Service sshd
# OpenSSH: Authorized Keys Setup
New-Item -Path $Env:ALLUSERSPROFILE\ssh -Name "administrators_authorized_keys" -ItemType "file" `
-Value "<INSERT_PUBLIC_KEY_HERE>"
# Set-up file ACL/permissions
icacls "$Env:ALLUSERSPROFILE\ssh\administrators_authorized_keys" /inheritance:d
icacls "$Env:ALLUSERSPROFILE\ssh\administrators_authorized_keys" /remove `
"NT AUTHORITY\Authenticated Users"
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Program Files\PowerShell\7\pwsh.exe" -PropertyType String -Force
Restart-Service -Name sshd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment