点击展开更新日志

2025

04-12

如何删除 Windows 远程桌面连接历史记录

04-20

新增:
更改 RDP 默认端口
禁用用户登录

10-06

【新增】OpenSSH 章节

nexttime

会有些什么呢(❁´◡`❁)

删除 Windows 远程桌面连接历史记录

源起

⚠️: 我在删除之后,遇到了一些bug:

  • 任务栏错位
  • 资源管理器存在黑块
    不确定是否是由删除这玩意导致的,建议先备份,真有问题了也能还原(你问我备份呢?这不是忘记了嘛😿)

因为在局域网会远程连接笔记本,原本是 192.168.2.0/24 的网段,最近(4月7日-9日)心血来潮,将局域网网段更换为了新的网段,不想每次远程时还留着旧的连接记录,因此想要删掉。

注册表删除

  1. 打开注册表编辑器
    Win + R 打开运行,输入 regedit 打开注册表编辑器。

  2. 备份注册表
    左上角选择【文件】-【导出】备份注册表(ps: 建议不定期备份注册表)。

  3. 删除历史记录
    进入到 HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client, 展开

    • Default:展开可以看到远程连接记录,右键【名称】删除不需要的记录
    • Servers:展开可以看到包含远程连接记录的目录,删除不需要的目录

更改远程桌面端口号

Windows RDP 默认端口号为 3389 ,如果觉得没必要的话不需要修改。

  1. 开启远程桌面

    • 方式一:进入【设置】-【系统】-【远程桌面】,开启功能。
    • 方式二:【控制面板】-【系统和安全】-【系统】下方【允许远程访问】,勾选远程桌面标签框中的允许远程连接到此计算机
  2. 修改默认端口

    打开注册表编辑器:Win+R 输入 regedit 回车,

    1. 修改路径 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Tds\tcpPortNumber 值,选择十进制,修改为新的端口号,比如 22333;
    2. 修改路径 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-TcpPortNumber 值,选择十进制,修改为新的端口号,比如 22333;
    3. 退出注册表编辑器;
    4. 重启系统生效。

    第一条路径 \Wds\rdpwd\Tds\tcp

    PortNumber键值负责​​底层网络协议栈的端口监听​​,属于远程桌面服务的基础网络层配置。它定义了RDP服务通过TCP协议监听的具体端口号,确保客户端能通过该端口发起连接请求

    第二条路径 \WinStations\RDP-Tcp:

    PortNumber键值控制​​会话管理层的端口配置​​,属于应用层设置。它定义了RDP会话建立后数据传输的端口规则,包括用户认证、会话分配等功能。

    只修改第二项也可以使用,但建议还是都修改。

  3. 配置入站规则

    打开【高级安全Windows Defender防火墙】,选择到左侧【入站规则】,创建两条 22333/TCP22333/UDP 的规则,限专用网络。

  4. (可选)禁用默认端口规则

    在防火墙入站规则配置页面,找到"远程桌面-用户模式(TCP-In)",禁用。

  5. 验证。

禁用用户登录

此需求的起因是因为将 WS2022 作为文件服务器使用需要共享给其他设备使用,但是又不想直接使用 Admin 用户,因此用此方法,如果觉得只是自己用用管理员账号倒也没什么问题,只是我又强迫症。。。

  1. Win+R 输入 gpedit.msc 打开组策略编辑器,进入【计算机配置】-【Windows设置】-【安全设置】-【本地策略】-【用户权限分配】
  2. 拒绝本地登录拒绝通过远程桌面登录服务器两项添加共享用户;
  3. 重启服务器生效。

自定义添加服务

如果要将自定义程序添加成 Windows 服务,可以用自带的 sc 命令。

sc 是用来与服务控制管理器和服务进行通信的命令行程序,具体可以在 cmd 直接输入 sc 查看详细信息。如果要查看子命令信息,比如 create,可以使用 sc create 查看语法和参数信息。

添加服务

1
sc <server> create [service name] [binPath= ] <option1> <option2>...
  • server:服务器地址,本地添加不需要
  • service name:服务名,命令行中使用的名称
  • displayname:服务管理台显示的名称
  • binPath:应用程序路径

注意参数和等号之间没有空格,但是等号和值之间有一个空格。
选项大小写不敏感,binPathbinpath 是一样的。

以将 C:\Users\admin\Documents\configs\windows exporter 目录下的 windows_exporter-0.31.3-amd64.exe 为例,正常启动命令为 windows_exporter-0.31.3-amd64.exe --config.file=config.yml,因此:

1
2
3
4
5
6
sc create windows_exporter binPath= "\"C:\Users\admin\Documents\configs\windows exporter\windows_exporter-0.31.3-amd64.exe\" --config.file=\"C:\Users\admin\Documents\configs\windows exporter\config.yml\"" start= auto displayname= windows_exporter

# 启动
sc start windows_exporter
# 或
net start windows_exporter

因为绝对路径中存在空格,因此需要使用引号,并做了转义。

如果添加之后发现启动不了,可以先把完整命令在cmd执行一遍看看报错

添加成功之后就可以在 Windows 服务管理台看到了。

删除服务

1
sc delete windows_exporter

OpenSSH

源起

这个也是需求驱动,正如之前在文章 MCP 镜像构建 提到,主机A上并没有安装 Docker Desktop,因此需要将代码拷贝到有 Docker 环境的 Mini 主机B,目前已经实现的基本流程是:

  1. 主机A:PyCharm开发调试 ✅
  2. 主机A:PyCharm 中又写了一个脚本同步代码到主机B——smb ✅
  3. 主机B:到项目目录下构建最新镜像,并重新创建容器 ❎

第三步目前还需要手动登录到主机B进行操作,因此想着借助 n8n 进行半自动化。要实现的效果,手动点击流程自动完成新镜像构建和部署,那要怎么操作呢,那就是这一节的主要内容了。

方式

根据元宝的回答,远程 Windows 执行命令有以下方式:

  • 基于内置远程协议
    • WinRM(Windows Remote Management):内置远程管理服务,支持 PowerShell 命令执行
    • WMI(Windows Management Instrunebtation)
  • 基于SSH 选择这种

为什么选择 SSH 的方式,因为:

  1. 类似 Linux 上的方式,操作起来有一定熟练度;
  2. 手动在主机B上打开的命令行工具是 Git bash,因此偏向于 Linux 命令行方式;
  3. Win 11内置支持(不过不包含服务端,还是得自己安装)

另外两种方式暂不做深究,因为目前实现的效果已经满足预期,不做扩展,因此如果有需要,请自行探索。

概要

OpenSSH 是 Linux 管理员和其他非 Windows 用于跨平台管理远程系统的安全外壳(SSH)工具的开源版本。 从 Windows 10 内部版本 1809 和 Windows Server 2019 开始,OpenSSH 作为按需功能提供。资料来源-Microsoft Learn

使用前提:

  • 系统:支持 Win10(1809+),Windows Server 2019 及以上版本,如果是其它版本,本文仅作参考,下面提及的命令可能会有报错,请善于使用搜索引擎及AI。
  • PowerShell >= 5.1:执行 $PSVersionTable.PSVersion 检查主要和次要版本号(Win11默认的版本就是5.1,7版本需要自己安装)
  • 账户:内置管理员组。我这里用的是本地管理员,不确定微软账户是否可用,知悉。

安装 OpenSSH 服务器和客户端

参考资料:

GUI

官方介绍说【可选功能】里有,但是我没找到。。

PowerShell

以管理员身份运行 PowerShell

  1. 检查 OpenSSH 状态:

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

    输出类似:

    1
    2
    3
    4
    5
    Name  : OpenSSH.Client~~~~0.0.1.0
    State : NotPresent

    Name : OpenSSH.Server~~~~0.0.1.0
    State : NotPresent
  2. 安装服务端或客户端:Win11 默认已安装客户端,没有服务端

    1
    2
    3
    4
    5
    # 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
  3. 启动服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # 启动 sshd 服务,通过【服务】进入也是一样的,名称是 OpenSSH SSH Server
    Start-Service sshd

    # 设置服务为开机自启:推荐
    Set-Service -Name sshd -StartupType 'Automatic'

    # 配置防火墙规则,应该会自动配置,可以执行下面的命令检查一下
    if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) {
    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."
    }

连接

连接方式就和 Linux 类似了,比如:

1
ssh [email protected]

如果是加域服务器,在用户名前加上域名:

1
ssh domain\username@servername

此时应该是可以从另一台机器远程连接到服务器,使用账号密码的方式。

卸载服务器和客户端

以管理员身份运行 PowerShell

  1. 删除 OpenSSH:

    1
    2
    3
    4
    5
    # 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
  2. 删除防火墙规则:

    1
    2
    3
    4
    5
    6
    if ((Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) {
    Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' is being removed."
    Remove-NetFirewallRule -Name 'OpenSSH-Server-In-TCP'
    } else {
    Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, removal failed..."
    }

如果卸载服务时正在使用该服务,则应重启 Windows。

注意事项

密钥认证

~~关于密钥认证还没弄明白,就不卖弄了,算是一个遗留问题,后续补充吧。~~刚说完又试了下就弄好了,不愧是我。

  1. 生成密钥对:随便找台机器,打开命令行,以 Git bash 为例:

    1
    ssh-keygen -t rsa -b 4096 -m pem

    具体可以参考 密钥对生成, 建议是使用 ed25519 算法,但是因为我需要在 n8n 中使用,试过 ed25519 会报错不支持,且不支持添加密码,只能使用 pem 编码的 rsa 算法。

    如果一路回车会在 ~/.ssh/id_rsa 下生成密钥对。

  2. 部署公钥

    • 标准用户:将 id_rsa.pub 公钥中的内容拷贝到用户目录 %USERPROFILE%/.ssh/authorized_keys
      1
      2
      3
      4
      5
      6
      7
      8
      # Get the public key file generated previously on your client.
      $authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ecdsa.pub

      # Generate the PowerShell command to run remotely that copies the public key file generated previously on your client to the authorized_keys file on your server.
      $remotePowershell = "powershell New-Item -Force -ItemType Directory -Path $env:USERPROFILE\.ssh; Add-Content -Force -Path $env:USERPROFILE\.ssh\authorized_keys -Value '$authorizedKey'"

      # Connect to your server and run the PowerShell command by using the $remotePowerShell variable.
      ssh username@[email protected] $remotePowershell
    • 管理用户:将 id_rsa.pub 公钥中的内容复制到 %ProgramDATA%/ssh/administrators_authorized_keys
      1
      2
      3
      4
      5
      6
      7
      8
      # Get the public key file generated previously on your client.
      $authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ecdsa.pub

      # Generate the PowerShell command to run remotely that copies the public key file generated previously on your client to the authorized_keys file on your server.
      $remotePowershell = "powershell Add-Content -Force -Path $env:ProgramData\ssh\administrators_authorized_keys -Value '''$authorizedKey''';icacls.exe ""$env:ProgramData\ssh\administrators_authorized_keys"" /inheritance:r /grant ""Administrators:F"" /grant ""SYSTEM:F"""

      # Connect to your server and run the PowerShell command by using the $remotePowerShell variable.
      ssh username@[email protected] $remotePowershell

    有疑问参考:OpenSSH for Windows 中基于密钥的身份验证

  3. 部署私钥

    id_rsa 中的内容拷贝到另一台 Windows 的 ~/.ssh/id_rsa_pem 文件(文件名自定),修改 ~/.ssh/config 文件,新增一条配置,参考:SSH 客户端配置

    我这里就是直接把私钥添加到 n8n 的凭据中做一下验证了。

  4. 验证

    打开一个终端直接 ssh hostname 看是否可以免密登录,如果还是需要密码说明密钥对没有配置正确。