Windows – Network Sharing + Port forwarding to VM

Loading

測試環境 windows 2019 + Hyper-V

主要是公司環境受限一個網孔只能取得一組 IP,這時候 Hyper-V 裡面的 VM 就無法連線了.

可以透過 windows 本身的 Network Sharing 功能,把想要分享的 Network 開給 VM 的 vswitch.

VM 就會獲得一組從 Network Sharing 得到一組內部 IP ,透過這 IP 就可以使用公司網路.

如需把 VM 的 RDP:3389 轉出去也只需要透過設定 Port forwarding 即可,以下從 ChatGPT 得到的指令.

Netsh Port Forwarding
適用於 將主機(Host)接收的流量轉發到 VM。

  1. 確認 VM 的 IP 地址
    在 Hyper-V 虛擬機內執行:

    > ipconfig
    

    記下 IPv4 地址(例如 192.168.1.101)。

  2. 使用 Netsh 設定 Port Forwarding
    在 Hyper-V 主機(Host) 上開啟 CMD(管理員權限):

    範例:將 主機的 8201 埠 轉發到 VM 的 3389(RDP) 埠:

    > netsh interface portproxy add v4tov4 listenport=8201 listenaddress=0.0.0.0 connectport=3389 connectaddress=192.168.1.101
    
  3. 允許防火牆流量(我是先把防火牆都先關閉了)
    允許 8201 埠流量:

    netsh advfirewall firewall add rule name="Forward to VM Alex" dir=in action=allow protocol=TCP localport=8201
    
  4. 檢查目前的 Port Forwarding 規則
    > netsh interface portproxy show all
    
    Listen on ipv4:             Connect to ipv4:
    
    Address         Port        Address         Port
    --------------- ----------  --------------- ----------
    0.0.0.0         8201        192.168.1.101 3389
    
  5. 刪除某條規則
    > netsh interface portproxy delete v4tov4 listenport=8201 listenaddress=0.0.0.0
    
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.