Windows Install OpenSSH

2022-09-19 15:26:45 浏览数 (1)

生成密钥

代码语言:javascript复制
ssh-keygen 
    -m PEM 
    -t rsa 
    -b 4096 
    -C "laowang@qq.com" 
    -N my_ssh_passwd

Install OpenSSH

代码语言:javascript复制
# >>>
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
代码语言:javascript复制
<<<
Name  : OpenSSH.Client~~~~0.0.1.0
State : NotPresent

Name  : OpenSSH.Server~~~~0.0.1.0
State : NotPresent
代码语言:javascript复制
# >>>
# 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
代码语言:javascript复制
<<<
Path          :
Online        : True
RestartNeeded : False
代码语言:javascript复制
# >>>
# Start the sshd service
Start-Service sshd

# OPTIONAL but recommended:
Set-Service -Name sshd -StartupType 'Automatic'

# Confirm the Firewall rule is configured. It should be created automatically by setup. Run the following to verify
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."
}

Uninstall OpenSSH

代码语言:javascript复制
# Uninstall the OpenSSH Client
Remove-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

# Uninstall the OpenSSH Server
Remove-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

Configuration OpenSSH

Default Terminal

代码语言:javascript复制
New-ItemProperty -Path "HKLM:SOFTWAREOpenSSH" -Name DefaultShell -Value "C:WindowsSystem32WindowsPowerShellv1.0powershell.exe" -PropertyType String -Force

File

  • C:ProgramDatasshadministrators_authorized_keys
  • C:ProgramDatasshsshd_config
  • C:Users<UserName>.sshauthorized_keys

Config

  • StrictModes no
  • PubkeyAuthentication yes
  • !!! PasswordAuthentication no !!!
  • !!! PermiEmptyPasswords no !!!
  • AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys

Debug Model

代码语言:javascript复制
Stop-Service sshd
C:WindowsSystem32OpenSSHsshd.exe -d

Other

代码语言:javascript复制
Restart-Service sshd 	# 重启服务
Stop-Service sshd 		# 停止服务
Start-Service sshd		# 开启服务

XRSec has the right to modify and interpret this article. If you want to reprint or disseminate this article, you must ensure the integrity of this article, including all contents such as copyright notice. Without the permission of the author, the content of this article shall not be modified or increased or decreased arbitrarily, and it shall not be used for commercial purposes in any way

0 人点赞