更改计算机上的远程桌面的侦听端口
适用于: ✅ Windows Server 2025, ✅ Windows Server 2022, ✅ Windows Server 2019, ✅ Windows Server 2016, ✅ Windows 11, ✅ Windows 10
远程桌面允许通过远程桌面协议 (RDP) 远程连接到运行 Windows 或 Windows Server 的计算机,并且默认情况下侦听端口 3389。 出于安全或配置目的,你可能需要更改此侦听端口。 本文提供使用 PowerShell 或注册表编辑器修改侦听端口的分步说明。
Prerequisites
在开始之前,请确保具有以下项:
要连接到的计算机的管理员访问权限或等效权限。
启用了远程桌面的计算机。 有关详细信息,请参阅启用远程桌面。
A client to test the changes from, such as Remote Desktop Connect (
mstsc.exe
) or Windows App.
配置远程桌面侦听端口
远程桌面的侦听端口在注册表中指定。 若要更改注册表值,请按照以下方法使用 PowerShell 或注册表编辑器进行更改。 选择喜欢的方法的相关选项卡。
PowerShell
Registry Editor
若要使用注册表编辑器更改侦听端口,请执行以下步骤:
Select the Start button, type Registry Editor, open Registry Editor from the best match list.
使用导航窗格,展开
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp
**` 键在主窗格中,选择
PortNumber
。从菜单栏中选择编辑 > 修改,然后选择十进制。
Type the new port number, and then select OK.
关闭注册表编辑器,然后重新启动计算机。
将新端口添加到 Windows 防火墙中
如果使用 Windows 防火墙,则需要添加新的入站规则以允许新端口上的流量。 有关可用于配置 Windows 防火墙的其他方法的详细信息,请参阅 Windows 防火墙工具。
重要
如果使用任何其他防火墙,请确保你或管理员允许连接到新端口号。
若要创建新的 Windows 防火墙规则以允许新端口,请以管理员身份运行以下 PowerShell 命令。 请务必将 <Port Number>
替换为新的端口号。
$portValue = '<Port Number>'New-NetFirewallRule -DisplayName 'RDPPORTLatest-TCP-In' -Profile Public -Direction Inbound -Action Allow -Protocol TCP -LocalPort $portValueNew-NetFirewallRule -DisplayName 'RDPPORTLatest-UDP-In' -Profile Public -Direction Inbound -Action Allow -Protocol UDP -LocalPort $portValue
测试新的远程桌面侦听端口
下次使用远程桌面连接或其他客户端连接到此计算机时,请输入主机名和新端口。 例如,如果将端口更改为在计算机 pc1.contoso.com
上使用 3390,则地址为 pc1.contoso.com:3390
。