Last active
October 24, 2024 02:43
-
-
Save wsxq2/7ca5f4c4e0e64c04abc31f4775c6a298 to your computer and use it in GitHub Desktop.
setup win11 openssh server. refer to https://learn.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_install_firstuse for detail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 1. install pwsh in Microsoft Store | |
| ## 2. execute command on remote pc(server) | |
| Set-Service -Name sshd -StartupType 'Automatic' # Enable the sshd service | |
| Start-Service sshd # Start the sshd service | |
| #Check firewall | |
| if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) { | |
| Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..." | |
| New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 | |
| } else { | |
| Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists." | |
| } | |
| #set default shell to powershell | |
| New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force | |
| ## 3. execute command on local pc(client) | |
| # Get the public key file generated previously on your client | |
| $authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ecdsa.pub | |
| # Generate the PowerShell to be run remote that will copy the public key file generated previously on your client to the authorized_keys file on your server | |
| $remotePowershell = "powershell 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""" | |
| # Connect to your server and run the PowerShell using the $remotePowerShell variable | |
| ssh username@[email protected] $remotePowershell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment