一、win10及以上
1.使用 PowerShell 安装 OpenSSH
若要使用 PowerShell 安装 OpenSSH,请先以管理员身份运行 PowerShell(win+x快捷键进行打开)。 为了确保 OpenSSH 可用,请运行以下 cmdlet:
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
如果两者均尚未安装,则此操作应返回以下输出:
Name : OpenSSH.Client~~~~0.0.1.0
State : NotPresent
Name : OpenSSH.Server~~~~0.0.1.0
State : NotPresent
然后,根据需要安装服务器或客户端组件:
#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
这两者应该都会返回以下输出:
Path :
Online : True
RestartNeeded : False
2.启动并配置 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."
}
设置完成后,回到“应用”>“应用和功能”和“可选功能”,你应会看到已列出 OpenSSH 。
3.开启服务
1、开启服务命令
net start sshd
2、关闭服务
net stop sshd
备注:默认端口为22,连接密码为系统登录密码,连接名为系统当前账号
4.连接到 OpenSSH 服务器
安装后,可从使用 PowerShell 安装了 OpenSSH 客户端的 Windows 10 或 Windows Server 2019 设备连接到 OpenSSH 服务器,如下所示。 请务必以管理员身份运行 PowerShell:
ssh username@servername
#例如
ssh Administrator@127.0.0.
微软参考地址:https://learn.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_install_firstuse
二、windowsServer
1、下载微软开源的openssh
官方地址:
https://github.com/PowerShell/Win32-OpenSSH/releases
下载最新的二进制版本:
https://github.com/PowerShell/Win32-OpenSSH/releases/download/v9.2.2.0p1-Beta/OpenSSH-Win64.zip
2、解压到C:\Program Files并重命名为OpenSSH
3、启动Window PowerShell 并进入C:\Program Files\OpenSSH目录
4、输入命令安装sshd和ssh-agent服务
powershell -ExecutionPolicy Bypass -File install-sshd.ps1
5、打开防火墙,开启22端口
netsh advfirewall firewall add rule name=sshd dir=in action=allow protocol=TCP localport=22
6、设置开机启动
Set-Service sshd -StartupType Automatic
Set-Service ssh-agent -StartupType Automatic
7、启动服务
Start-Service sshd
Start-Service ssh-agent
8、连接测试
使用xshell输入ip,用户名密码为Win 2008 r2登录的用户名和密码