使用 powershell 安装 openssh

发布时间 2023-11-14 12:27:47作者: 昆山皮皮虾

1. 若要使用 PowerShell 安装 OpenSSH,请先以管理员身份运行 PowerShell(win+x快捷键进行打开)。 为了确保 OpenSSH 可用,请运行以下 cmdlet:

Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'

 

 

2. 如果两者均尚未安装,则此操作应返回以下输出:

Name  : OpenSSH.Client~~~~0.0.1.0
State : NotPresent

Name  : OpenSSH.Server~~~~0.0.1.0
State : NotPresent

 

 

3. 然后,根据需要安装服务器或客户端组件:

# 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

 

 

4. 这两者应该都会返回以下输出:

 

Path          :
Online        : True
RestartNeeded : False

 

 

5. 启动并配置 OpenSSH 服务器

若要启动并配置 OpenSSH 服务器来开启使用,请以管理员身份打开 PowerShell,然后运行以下命令来启动 sshd service

 

# 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."}