4月和5月份的时候,客户的医药供应链数据库,接连两次被恶意加密,没有办法,只好把服务器从windows server 2008升级到了 2022, 我并且把sqlserver 补丁也打了最新了,为了防止再次被加密数据,我购买了令外一台 windows server 2022 服务器专门用来备份数据。 我的两台 windows server 2022 一台安装了Openssh server 一台是默认自带的ssh client。openssh server那台有sqlserver 的备份,我通过powershell脚本定时把server上的备份复制到另外一台机器上,实现远程文件备份的目的。有一次因为打了累计补丁,服务器重启了下,结果客户端连接openssh server时候报: Unable to negotiate with 10.0.0.3 port 22: no matching host key type found. Their offer: ssh-rsa,ssh-dss,试过各种方法都没有找到问题,一个多月后的今天再次看这个问题,把sshd_config配置文件看了一遍又一遍,都应该没有问题才对,我本想卸载服务端重新安装,结果看到一个opensshd服务,这个是我之前安装的版本,版本很低,这个服务也是启动22端口,启动优先级高于通过powershell命令安装的 OpenSSH server,我排查问题的时候 已经通过telnet 10.0.0.3 22 回显的sshd 版本号,总感觉不对,都没有引起注意。卸载这个软件后,然后通过restart-services sshd 再通过客户端连接,一下载正常了,哎,折腾了这么久。。。。。
若要使用 PowerShell 启用 SSHD,请执行以下操作:
以管理员身份打开 PowerShell,并运行以下 cmdlet 以启动 SSHD 服务:
# Start the sshd service
Start-Service sshd
还可以运行以下可选但建议的 cmdlet 以自动启动 SSHD,以确保其保持启用状态:
Set-Service -Name sshd -StartupType 'Automatic'
最后,运行以下命令以验证 SSHD 安装过程是否自动配置了防火墙规则:
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 兼容的客户端可用于连接到 Windows Server 和 Windows 客户端设备。
在开始之前,计算机必须满足以下要求:
至少运行 Windows Server 2019 或 Windows 10(内部版本 1809)的设备。
PowerShell 5.1 或更高版本。
作为内置管理员组成员的帐户。
若要验证环境,请打开提升的 PowerShell 会话并执行以下操作:
Enter winver.exe and press enter to see the version details for your Windows device.
运行 $PSVersionTable.PSVersion。 验证主要版本至少为 5,次要版本至少为 1。 详细了解如何在 Windows 上安装 PowerShell。
运行以下命令。 当你是内置 Administrator 组的成员时,输出将显示 True。
(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
若要使用 PowerShell 安装 OpenSSH,请执行以下操作:
以管理员身份运行 PowerShell。
运行以下 cmdlet 以确保 OpenSSH 可用:
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
之后,运行以下 cmdlet 以根据需要安装服务器或客户端组件:
# 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
若要启动并配置 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 客户端的 Windows 或 Windows Server 设备连接到 OpenSSH 服务器。 在 PowerShell 提示符下,运行以下命令。
ssh domain\username@servername
连接后,会收到类似如以下输出的消息。
The authenticity of host 'servername (10.00.00.001)' can't be established.
ECDSA key fingerprint is SHA256:(<a large string>).
Are you sure you want to continue connecting (yes/no)?
Entering yes adds that server to the list of known SSH hosts on your Windows client.
此时,服务会提示你输入密码。 作为安全预防措施,输入密码时不会显示密码字符。
连接后,你将看到以下 Windows 命令行界面提示符:
domain\username@SERVERNAME C:\Users\username>
若要使用 PowerShell 卸载 OpenSSH 组件,请使用以下命令:
# 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
如果卸载服务时正在使用该服务,则应重启 Windows。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!