通过SSH远程连接Windows系统并进行管理

1. 在Windows系统中安装OpenSSH服务端

在Windows11系统下,按 Windows+i 打开设置,点击应用,点击可选功能,点击查看功能,勾选OpenSSH服务器,点击下一步,点击安装。

或者使用Windows Terminal安装OpenSSH:按Windows + x,点击Windows终端(管理员),输入以下命令分别安装OpenSSH客户端和服务端。

# 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

安装完毕OpenSSH服务端后,还需要启动SSH服务,设置开机启动,设置防火墙。在Windows终端(管理员)中执行以下命令:

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

操作完毕后,即可使用SSH登录到Windows系统终端了。登录后的操作,和在目标机器上打开其Windows终端进行操作是等同的。

ssh username@servername

修改OpenSSH配置文件,使支持使用密钥远程登录Windows系统。修改C:\ProgramData\ssh\sshd_config文件的内容,修改如下几项:

PubkeyAuthentication yes
PermitRootLogin no
ClientAliveInterval 60
ClientAliveCountMax 10
UseDNS no
GatewayPorts yes

修改完配置文件后,在Windows终端中重启SSH服务:

Stop-Service sshd; Start-Service sshd
或
Restart-Service -Name sshd

2. 实现在Linux服务器上执行一个命令后,能播放我个人笔记本电脑上的一段音乐

我经常远程登录Linux服务器进行比较耗时的计算,却又想在程序结束后能及时提醒我。于是,我在输入一个耗时较长的命令后,额外加一个命令finishing_reminder_by_ringingSong.pl。该命令在远程Linux系统上运行后,能播放我当前笔记本电脑Windows系统中的一段响铃音乐。具体的操作流程如下:

2.1 在Windows系统设置无密码登录的密钥和授权文件

将在Linux系统上的密钥文件id_rsa拷贝到Windows系统用户的.ssh文件夹下,将授权文件authorized_keys拷贝到C:\ProgramData\ssh文件夹下并重命名为administrators_authorized_keys。在Windows终端(管理员)中对这两个文件再进行权限设置,修改拥有者权限:

icacls.exe "C:\Users\chenlianfu\.ssh\id_rsa" /inheritance:r /grant "chenlianfu:F"
icacls.exe "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"

2.2 在Windows系统设置反向隧道连接到我的一台公网服务器

在Windows系统上执行命令,构建到公网服务器的反向隧道:

ssh -f -N -R 52228:localhost:22 chenlianfu@chenlianfu.com

考虑设置开机时在联网状态下自动运行以上命令:右击我的电脑,显示更多选项,管理,任务计划程序,创建任务,在弹出的页面中进行设置。触发器:启动后1分钟;条件:只有在有网络连接可用时才启动;操作:以上ssh命令。

在远程Linux计算服务器上检测能否使用密钥登录到Windows笔记本电脑上:

ssh -p 52228 chenlianfu@chenlianfu.com

2.3 在Windows下使用命令播放音乐

设置Windows系统Path路径:按Windows+q搜索“高级系统设置”,点击“查看高级系统设置”,在弹出的窗口中点击“环境变量”,鼠标左键选中“Path”所在行,再点击编辑,浏览到文件夹C:\Program Files (x86)\Windows Media Player和C:\Program Files (x86)\Tencent\QQMusic\QQMusic1917.16.31.32,从而将该路径添加进去,再点击确定。此外,我将音频文件放置于D:\ringing_sound_01.mp3路径。关闭所有Windows终端,再重新打开,输入命令即可播放音乐:

wmplayer.exe D:\ringing_sound_01.mp3
QQMusic.exe D:\ChenLianfu_Data\05.设置数据\ringing_sound_01.mp3

2.4 在Linux服务器中远程播放本地笔记本电脑上的音乐

在远程Linux服务器中执行命令,即可播放本地笔记本电脑上的音频。

ssh -p 52228 chenlianfu@chenlianfu.com "wmplayer.exe D:\ChenLianfu_Data\05.设置数据\ringing_sound_01.mp3"