基于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 2023-02-06 17:47  TanZhiWei  阅读(1281)  评论(0编辑  收藏  举报