設定 Windows 防火牆以進行遠端偵錯Configure Windows Firewall for remote debugging

在受 Windows 防火牆保護的網路上,必須將防火牆設定為允許遠端偵錯程式。On a network protected by Windows Firewall, the firewall must be configured to permit remote debugging. Visual Studio 和遠端偵錯程式會嘗試在安裝或啟動期間開啟正確的防火牆埠,但您可能也需要手動開啟埠或允許應用程式。Visual Studio and the remote debugging tools try to open the correct firewall ports during installation or startup, but you may also need to open ports or allow apps manually.

本主題說明如何設定 Windows 防火牆,以便在 Windows 10、8/8.1 和7上啟用遠端偵錯。以及 Windows Server 2012 R2、2012和 2008 R2 電腦。This topic describes how to configure the Windows firewall to enable remote debugging on Windows 10, 8/8.1, and 7; and Windows Server 2012 R2, 2012, and 2008 R2 computers. Visual Studio 和遠端電腦不需要執行相同的作業系統。The Visual Studio and remote computer don't have to be running the same operating system. 例如,Visual Studio 電腦可以執行 Windows 10,而遠端電腦則可以執行 Windows Server 2012 R2。For example, the Visual Studio computer can run Windows 10, and the remote computer can run Windows Server 2012 R2.

注意

針對不同的作業系統和舊版 Windows,設定 Windows 防火牆的指示稍有不同。The instructions for configuring the Windows firewall differ slightly on different operating systems, and for older versions of Windows. Windows 8/8.1、Windows 10 和 Windows Server 2012 設定會使用 word 應用 程式,而 windows 7 和 windows server 2008 則使用 word 程式Windows 8/8.1, Windows 10, and Windows Server 2012 settings use the word app, while Windows 7 and Windows Server 2008 use the word program.

設定遠端偵錯程式的埠Configure ports for remote debugging

Visual Studio 和遠端偵錯程式在安裝或啟動期間嘗試開啟正確的埠。Visual Studio and the remote debugger try to open the correct ports during installation or startup. 不過,在某些情況下,例如協力廠商防火牆,您可能需要手動開啟埠。However, in some scenarios, such as a third-party firewall, you may need to open ports manually.

若要開啟埠:To open a port:

  1. 在 Windows [ 開始 ] 功能表中,搜尋並開啟 [ 具有 Advanced Security 的 Windows 防火牆]。In Windows Start menu, search for and open Windows Firewall with Advanced Security. 在 Windows 10 中,這是 使用 Advanced Security Windows Defender 防火牆的In Windows 10, this is Windows Defender Firewall with Advanced Security.

  2. 針對新的連入埠,選取 [ 輸入規則 ],然後選取 [ 新增規則]。For a new incoming port, select Inbound Rules and then select New Rule. 若為外寄規則,請改為選取 輸出規則For an outgoing rule, select Outbound Rules instead.

  3. 在 [ 新增輸入規則] 嚮導 中,選取 [ ],然後選取 [下一步]In the New Inbound Rule Wizard, select Port, and then select Next.

  4. 根據下表中的埠號碼,選取 [ TCP ] 或 [ UDP]。Select either TCP or UDP, depending on the port number from the following tables.

  5. 在 [ 特定本機埠] 下,輸入下清單格中的埠號碼,然後選取 [下一步]Under Specific local ports, enter a port number from the following tables, and select Next.

  6. 選取 [允許連接],然後選取 [下一步]Select Allow the Connection, and then select Next.

  7. 選取要啟用的一或多個網路類型,包括遠端連線的網路類型,然後選取 [下一步]Select one or more network types to enable, including the network type for the remote connection, and then select Next.

  8. 新增規則的名稱 (例如, msvsmonIISWeb Deploy) ,然後選取 [完成]Add a name for the rule (for example, msvsmon, IIS, or Web Deploy), and then select Finish.

    新的規則應該會出現,並在 [ 輸入規則 ] 或 [ 輸出規則 ] 清單中選取。The new rule should appear and be selected in the Inbound Rules or Outbound Rules list.

在遠端電腦上啟用遠端偵錯的連接埠Ports on the remote computer that enable remote debugging

遠端偵錯程式必須在遠端電腦上開啟下列埠:For remote debugging, the following ports must be open on the remote computer:

連接埠Ports 傳入/傳出Incoming/Outgoing 通訊協定Protocol 描述Description
40224022 正在傳入Incoming TCPTCP 針對 VS 2017。For VS 2017. 每個 Visual Studio 版本的埠號碼會遞增2。The port number increments by 2 for each Visual Studio version. 如需詳細資訊,請參閱 Visual Studio 遠端偵錯程式埠指派For more information, see Visual Studio remote debugger port assignments.
40234023 正在傳入Incoming TCPTCP 針對 VS 2017。For VS 2017. 每個 Visual Studio 版本的埠號碼會遞增2。The port number increments by 2 for each Visual Studio version. 此埠僅用來從遠端偵錯程式的64位版本進行遠端偵錯程式的32位處理常式。This port is only used to remote debug a 32-bit process from a 64-bit version of the remote debugger. 如需詳細資訊,請參閱 Visual Studio 遠端偵錯程式埠指派For more information, see Visual Studio remote debugger port assignments.
37023702 傳出Outgoing UDPUDP (遠端偵錯程式探索所需的選擇性) 。(Optional) Required for remote debugger discovery.
連接埠Ports 傳入/傳出Incoming/Outgoing 通訊協定Protocol 描述Description
40244024 正在傳入Incoming TCPTCP 針對 VS 2019。For VS 2019. 每個 Visual Studio 版本的埠號碼會遞增2。The port number increments by 2 for each Visual Studio version. 如需詳細資訊,請參閱 Visual Studio 遠端偵錯程式埠指派For more information, see Visual Studio remote debugger port assignments.
40254025 正在傳入Incoming TCPTCP 針對 VS 2019。For VS 2019. 每個 Visual Studio 版本的埠號碼會遞增2。The port number increments by 2 for each Visual Studio version. 此埠僅用來從遠端偵錯程式的64位版本進行遠端偵錯程式的32位處理常式。This port is only used to remote debug a 32-bit process from a 64-bit version of the remote debugger. 如需詳細資訊,請參閱 Visual Studio 遠端偵錯程式埠指派For more information, see Visual Studio remote debugger port assignments.
37023702 傳出Outgoing UDPUDP (遠端偵錯程式探索所需的選擇性) 。(Optional) Required for remote debugger discovery.

如果您選取 [工具 選項偵錯工具] 下的 [使用 Managed 相容性模式] > > ****,請開啟這些額外的遠端偵錯程式埠If you select Use Managed Compatibility Mode under Tools > Options > Debugging, open these additional remote debugger ports. 偵錯工具管理的相容性模式會啟用舊版的 Visual Studio 2010 版本的偵錯工具。Debugger Managed Compatibility Mode enables a legacy, Visual Studio 2010 version of the debugger.

連接埠Ports 傳入/傳出Incoming/Outgoing 通訊協定Protocol 描述Description
135、139、445135, 139, 445 傳出Outgoing TCPTCP 必要。Required.
137、138137, 138 傳出Outgoing UDPUDP 必要。Required.

如果您的網域原則需要透過 IPSec 執行網路通訊,您必須在 Visual Studio 和遠端電腦上開啟其他埠。If your domain policy requires network communication to be performed through IPSec, you must open additional ports on both the Visual Studio and remote computers. 若要在遠端 IIS web 伺服器上進行調試,請在遠端電腦上開啟埠80。To debug on a remote IIS web server, open port 80 on the remote computer.

連接埠Ports 傳入/傳出Incoming/Outgoing 通訊協定Protocol 描述Description
500、4500500, 4500 傳出Outgoing UDPUDP 如果您的網域原則需要透過 IPSec 進行網路通訊時,則為必要項。Required if your domain policy requires network communication to be performed through IPSec.
8080 傳出Outgoing TCPTCP 網頁伺服器偵錯的必要項。Required for web server debugging.

若要允許特定應用程式通過 Windows 防火牆,請參閱 設定透過 Windows 防火牆進行遠端偵錯To allow specific apps through the Windows firewall, see Configure remote debugging through Windows Firewall.

設定透過 Windows 防火牆進行遠端偵錯Configure remote debugging through Windows firewall

您可以在遠端電腦上安裝遠端偵錯程式,或從共用資料夾執行這些工具。You can install the remote debugging tools on the remote computer, or run them from a shared folder. 無論是哪一種情況,都必須正確設定遠端電腦防火牆。In either case, the remote computer firewall must be configured correctly.

在遠端電腦上,遠端偵錯工具位於:On a remote computer, the remote debugging tools are in:

*<Visual Studio installation directory>\Common7 \ IDE \ 遠端偵錯程式\<x86*, x64, or Appx>*<Visual Studio installation directory>\Common7\IDE\Remote Debugger\<x86*, x64, or Appx>

透過 Windows 防火牆允許和設定遠端偵錯程式Allow and configure the remote debugger through Windows Firewall

  1. 在 Windows [ 開始 ] 功能表中,搜尋並開啟 [ windows 防火牆],或 Windows Defender 防火牆]。In Windows Start menu, search for and open Windows Firewall, or Windows Defender Firewall.

  2. 選取 [ 允許應用程式通過 Windows 防火牆]。Select Allow an app through Windows Firewall.

  3. 如果 [允許的應用程式和功能] 下的 [遠端偵錯程式] 或 Visual Studio 遠端偵錯工具 沒有出現,請選取 [變更設定],然後選取 [允許其他應用程式If Remote Debugger or Visual Studio Remote Debugger doesn't appear under Allowed apps and features, select Change settings, and then select Allow another app.

  4. 如果遠端偵錯程式應用程式仍未列在 [新增應用程式] 對話方塊中,請選取 [流覽],然後 <Visual Studio installation directory> \ \ \ \ <x86*, x64, or Appx> 根據您應用程式的適當架構,流覽至 * Common7 IDE 遠端偵錯程式。If the remote debugger app still isn't listed in the Add an app dialog, select Browse, and navigate to *<Visual Studio installation directory>\Common7\IDE\Remote Debugger\<x86*, x64, or Appx>, depending on the appropriate architecture for your app. 選取 msvsmon.exe,然後選取 [ 新增]。Select msvsmon.exe, and then select Add.

  5. 在 [ 應用程式 ] 清單中,選取您剛剛新增的 遠端偵錯程式In the Apps list, select the Remote Debugger that you just added. 選取 [ 網路類型],然後選取一或多個網路類型,包括遠端連線的網路類型。Select Network types, and then select one or more network types, including the network type for the remote connection.

  6. 選取 [新增],然後選取 [確定]。Select Add, and then select OK.

疑難排解遠端偵錯連接Troubleshoot the remote debugging connection

如果您無法使用遠端偵錯程式附加至您的應用程式,請確定遠端偵錯程式防火牆埠、通訊協定、網路類型和應用程式設定都正確無誤。If you can't attach to your app with the remote debugger, make sure the remote debugging firewall ports, protocols, network types, and app settings are all correct.

  • 在 Windows [ 開始 ] 功能表中,搜尋並開啟 [ windows 防火牆],然後選取 [ 允許應用程式通過 Windows 防火牆]。In the Windows Start menu, search for and open Windows Firewall, and select Allow an app through Windows Firewall. 確定 [ 遠端偵錯程式 ] 或 [ Visual Studio 遠端偵錯工具 會出現在 [ 允許的應用程式和功能 ] 清單中,並選取核取方塊,並選取正確的網路類型。Make sure Remote Debugger or Visual Studio Remote Debugger appears in the Allowed apps and features list with a selected check box, and the correct network types are selected. 如果沒有,請 新增正確的應用程式和設定If not, add the correct apps and settings.

  • 在 Windows [ 開始 ] 功能表中,搜尋並開啟 [ 具有 Advanced Security 的 Windows 防火牆]。In the Windows Start menu, search for and open Windows Firewall with Advanced Security. 請確定 [輸入 (規則] 下出現 [遠端偵錯程式] 或 Visual Studio 遠端偵錯工具,並選擇性地顯示 輸出規則) 綠色核取記號圖示,且所有設定都正確。Make sure Remote Debugger or Visual Studio Remote Debugger appears under Inbound Rules (and optionally, Outbound Rules) with a green checkmark icon, and that all settings are correct.

    • 若要查看或變更規則設定,請在清單中的 遠端偵錯 程式應用程式上按一下滑鼠右鍵,然後選取 [ 屬性]。To view or change the rule settings, right-click the Remote Debugger app in the list and select Properties. 您可以使用 [ 屬性 ] 索引標籤來啟用或停用規則,或變更埠號碼、通訊協定或網路類型。Use the Properties tabs to enable or disable the rule, or change port numbers, protocols, or network types.
    • 如果「遠端偵錯程式」應用程式未出現在 [規則] 清單中,請 新增並設定正確的埠If the remote debugger app doesn't appear in the rules list, add and configure the correct ports.

請參閱See also