Port Forwarding with Hyper-V Virtual Machine on Windows 10

hyper-vUbuntuwindows 10

I have Ubuntu 18.04 in a Hyper-V virtual machine on Windows 10 and I need to make its port 51235 to be accessible from the internet.

There are various related posts, for example:

  1. Hyper-V and a static IP address for Ubuntu VM
  2. Hyper-V VM Static IP – 2 IP Addresses
  3. Make dynamic IP address fixed?

but it is still not clear enough should I configure my 'Ethernet adapter vEthernet (Default Switch)' to which my virtual machine is connected to or should I create a new specific network adapter? How to enable NAT? And what is value my static IP address? What is the easiest way to configure Ubuntu to make it have static (fixed) IP address?

below I provided the list of my network adapters obtained with 'ipconfig /all' command:

Windows IP Configuration

   Host Name . . . . . . . . . . . . : DESKTOP-L5J499D
   Primary Dns Suffix  . . . . . . . :
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter vEthernet (Internal Ethernet Port Windows Phone Emulator Internal Switch):

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #2
   Physical Address. . . . . . . . . : 00-15-5D-00-68-05
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::34b5:c9a0:8af:f4d2%5(Preferred)
   IPv4 Address. . . . . . . . . . . : 169.254.80.80(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.0.0
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : 637539677
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-22-33-5B-4C-B0-6E-BF-C0-EE-1C
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter vEthernet (Internal Ethernet Port Microsoft Emulator NAT Switch):

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #3
   Physical Address. . . . . . . . . : 00-15-5D-00-68-06
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::d4d9:73c7:599c:f657%15(Preferred)
   IPv4 Address. . . . . . . . . . . : 172.16.80.1(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : 738202973
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-22-33-5B-4C-B0-6E-BF-C0-EE-1C
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Intel(R) Ethernet Connection (2) I219-V
   Physical Address. . . . . . . . . : B0-6E-BF-C0-EE-1C
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::e015:e896:a9c:5ef3%8(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.0.104(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Lease Obtained. . . . . . . . . . : Wednesday, June 5, 2019 4:38:09 PM
   Lease Expires . . . . . . . . . . : Wednesday, June 5, 2019 10:38:08 PM
   Default Gateway . . . . . . . . . : 192.168.0.1
   DHCP Server . . . . . . . . . . . : 192.168.0.1
   DHCPv6 IAID . . . . . . . . . . . : 61894335
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-22-33-5B-4C-B0-6E-BF-C0-EE-1C
   DNS Servers . . . . . . . . . . . : 192.168.0.1
   NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter vEthernet (Default Switch):

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter
   Physical Address. . . . . . . . . : 00-15-5D-01-1A-46
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::502b:1b91:2c6:5a6a%27(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.244.65(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.240
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : 452990301
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-22-33-5B-4C-B0-6E-BF-C0-EE-1C
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

EDIT1:

With the dynamic IP address on Ubuntu I have this:

$ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group defaul                          t qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group defa                          ult qlen 1000
    link/ether 00:15:5d:00:68:04 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.103/24 brd 192.168.0.255 scope global dynamic noprefixroute e                          th0
       valid_lft 5759sec preferred_lft 5759sec
    inet6 fe80::6476:961e:3158:86f5/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

on Windows side:

Windows IP Configuration

Ethernet adapter vEthernet (Internal Ethernet Port Windows Phone Emulator Internal Switch):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::34b5:c9a0:8af:f4d2%5
   IPv4 Address. . . . . . . . . . . : 169.254.80.80
   Subnet Mask . . . . . . . . . . . : 255.255.0.0
   Default Gateway . . . . . . . . . :

Ethernet adapter vEthernet (Internal Ethernet Port Microsoft Emulator NAT Switch):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::d4d9:73c7:599c:f657%15
   IPv4 Address. . . . . . . . . . . : 172.16.80.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

Ethernet adapter vEthernet (External Virtual Switch):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::b453:fe72:12d3:5982%37
   IPv4 Address. . . . . . . . . . . : 192.168.0.104
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.0.1

Ethernet adapter vEthernet (Default Switch):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::502b:1b91:2c6:5a6a%27
   IPv4 Address. . . . . . . . . . . : 192.168.244.65
   Subnet Mask . . . . . . . . . . . : 255.255.255.240
   Default Gateway . . . . . . . . . :

I tried the following Ubuntu settings, but they breaks the internet connection:

enter image description here

EDIT2:

My external virtual switch is configured as follows:

enter image description here

Interl(R) Ethernet Connection (2) I219-V is my network card listed in Device Manager. My external virtual switch is selected as the network adapter for my virtual machine:

enter image description here

Best Answer

Hyper-V Network Adapter: You want to set up an external virtual switch in Hyper-V, if you haven't already and assign that to your VM. This will allow your VM access to your host's network. You don't want to use the default NAT adapter with Hyper-V, it's not configurable and doesn't allow port forwarding.

To set up an external virtual switch:
In Hyper-V Manager, click Virtual Switch Manager, select External, click Create Virtual Switch. Under External network, select the network card that is connected to your network, Allow management OS to share this network adapter should remain checked. Click OK. Assign this new virtual switch to the Ubuntu VM. Note: this will temporarily disconnect your host PC from the network while the virtual switch is installing.

NAT: You should already have NAT enabled on your firewall/router. It's not a good idea to be directly connected to the internet. You don't need NAT on your Windows 10 host for this to work, it will only add to the complexity.

Static IP: You would pick this based on your network. You should pick a value that's outside your DHCP scope, but in the same subnet (192.168.0.x).

Port forwarding: Use the port forwarding on your router/firewall to handle the port forward. Use an external virtual switch and assign your VM an IP from your regular network. Configure the port forwarding for the Port/IP address in your router/firewall.

Configure Ubuntu for static IP: You do this in your VM, not in an Hyper-V component. In Ubuntu, go to Settings-->Network. Click the gear icon next to the wired network connection. Click the IPv4 tab, click Manual, and enter the IP settings specific to your network. The subnet mask, gateway, and DNS values should be the same as the host's.

See this link for pictures of above: https://linuxconfig.org/how-to-configure-static-ip-address-on-ubuntu-18-04-bionic-beaver-linux

Related Question