基于mstsc远程:修改远程端口号

windows mstsc 远程

1 首先被远程端需要运行允许远程、修改路径:Control->系统和安全->系统属性(稍后通过修改注册表可以修改)

 

2 mstsc 常用3389端口,而3389 通常会被防火墙 禁用,因此需要修改端口。本例修改成55480

 需要进行三步:

第一: 修改rdp 端口 

第二:重启Remote Desktop Services

第三:修改防火墙监听端口

复制代码
复制代码
 /// <summary>
        /// 修改远程桌面端口
        /// </summary>
        private void ChangedTeleport()
        {
            var localMachine = Registry.LocalMachine;
            int changedPort = 55800;
            // 修改端口:将默认3389 修改成55800
            localMachine
                .CreateSubKey(@"SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Tds\tcp", true)?
                .SetValue("PortNumber", changedPort, RegistryValueKind.DWord);

            localMachine
              .CreateSubKey(@"SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp", true)?
              .SetValue("PortNumber", changedPort, RegistryValueKind.DWord);

            //重启Remote Desktop Services,即可生效。由于Remote Desktop Services重启很耗时,而且不一定成功。故重启电脑代替

            //修改防火墙监听端口
            var defaultsFirewallRulesKey = localMachine
                .CreateSubKey(@"SYSTEM\CurrentControlSet\Services\SharedAccess\Defaults\FirewallPolicy\FirewallRules", true);
            var defaultsRemoteDesktopUserModeInTcp = defaultsFirewallRulesKey?.GetValue("RemoteDesktop-UserMode-In-TCP").ToString();
            // v2.31|Action=Allow|Active=FALSE|Dir=In|Protocol=6|LPort=3389|App=%SystemRoot%\system32\svchost.exe|Svc=termservice|Name=@FirewallAPI.dll,-28775|Desc=@FirewallAPI.dll,-28756|EmbedCtxt=@FirewallAPI.dll,-28752|
            defaultsRemoteDesktopUserModeInTcp = defaultsRemoteDesktopUserModeInTcp?.Replace("3389", $"{changedPort}");
            defaultsFirewallRulesKey?.SetValue("RemoteDesktop-UserMode-In-TCP", defaultsRemoteDesktopUserModeInTcp, RegistryValueKind.String);

            var parametersFirewallRulesKey = localMachine
          .CreateSubKey(@"SYSTEM\CurrentControlSet\services\SharedAccess\Parameters\FirewallPolicy\FirewallRules", true);
            var parametersRemoteDesktopUserModeInTcp = parametersFirewallRulesKey?.GetValue("RemoteDesktop-UserMode-In-TCP").ToString();
            // v2.31|Action=Allow|Active=TRUE|Dir=In|Protocol=6|LPort=3389|App=%SystemRoot%\system32\svchost.exe|Svc=termservice|Name=@FirewallAPI.dll,-28775|Desc=@FirewallAPI.dll,-28756|EmbedCtxt=@FirewallAPI.dll,-28752|
            parametersRemoteDesktopUserModeInTcp = parametersRemoteDesktopUserModeInTcp?.Replace("3389", $"{changedPort}");
            parametersFirewallRulesKey?.SetValue("RemoteDesktop-UserMode-In-TCP", parametersRemoteDesktopUserModeInTcp, RegistryValueKind.String);
        }
复制代码

 

复制代码

posted on   TanZhiWei  阅读(1330)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示