Port forwarding with Hyper-V virtual machine on Windows 10
Solution 1
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
Solution 2
Just as a note, you CAN do static NAT mapping without putting your VM on your physical network, this allows you to use your host to be the only device on the phyiscal network while keeping your other VMs behind your host's firewall/segmented and share the same MAC as host which is often needed in universities, schools, dorms, etc.
https://petri.com/create-nat-rules-hyper-v-nat-virtual-switch
Solution 3
After digging through the article linked to in Alexis Tupeck's answer, I was able to set up an Hyper-V Internal Switch, put it on a private subnet, and create a NAT. Then I could set up port-forwards on the NAT to statically assigned VMs.
The following example will set up an internal network with the Hyper-V host on 172.16.10.254 /24, and the guest VM on 172.16.10.100.
• Make a new Hyper-V Internal Switch (can also be done through the Hyper-V Manager):
New-VMSwitch -SwitchName "NAT Switch" -SwitchType Internal
• Assign it an IP address/subnet (e.g. 172.16.10.254 /24):
New-NetIPAddress -IPAddress 172.16.10.254 -PrefixLength 24 -InterfaceAlias "vEthernet (NAT Switch)"
• Set up a NAT on the new subnet:
New-NetNat -Name NATNetwork -InternalIPInterfaceAddressPrefix 172.16.10.0/24
Now you can set up a VM on this virtual switch. Give it a static IP of 172.16.10.100 - use /24 or 255.255.255.0 for the subnet mask - and set its gateway to the Hyper-V host on 172.16.10.254.
(Assuming the Hyper-V host isn't serving DNS on your new subnet, you can likely just point its DNS servers to the same ones the host is using itself, or something external e.g. 1.1.1.1 or 8.8.8.8.)
You should now be able to reach the Internet through the Hyper-V host using the NAT.
• Finally, set up a port-forward to the VM (e.g. TCP 1443 -> 172.16.10.100:443):
Add-NetNatStaticMapping -NatName NATNetwork -Protocol TCP -ExternalIPAddress 0.0.0.0/24 -ExternalPort 1443 -InternalIPAddress 172.16.10.100 -InternalPort 443
After this, you should be able to access the VM's port 443 through localhost:1443
on the Hyper-V host.
Related videos on Youtube

Alexey Starinsky
Updated on September 18, 2022Comments
-
Alexey Starinsky 8 months
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:
- Hyper-V and a static IP address for Ubuntu VM
- Hyper-V VM Static IP - 2 IP Addresses
- Make dynamic IP address fixed?
But it is still not clear enough. Should I configure my
Ethernet adapter vEthernet (Default Switch)
, 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've 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:
EDIT2:
My external virtual switch is configured as follows:
Intel(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: -
Alexey Starinsky almost 4 yearsI created external network adapter and the dynamic IP address of my Ubuntu machine is now 192.168.0.103, see EDIT1. What should I specify as Netmask and Gateway in Ubuntu Settings->Network->IP4 page? If I leave them empty the internet connection is lost.
-
Alexey Starinsky almost 4 yearsHonestly, I do not know exactly what Netmask and Gateway are, I tried 255.255.255.0 and 192.168.0.1, but with no success. See the picture.
-
essjae almost 4 yearsThose should be correct, netmask and gateway (and DNS) will be the same as the host as long as you're using an external virtual switch. Verify that your external switch is connected to your active network card.
-
Alexey Starinsky almost 4 yearsWhere to check this?
-
Alexey Starinsky almost 4 yearsDoes it relate somehow to Network Adapter->Properties->Sharing?
-
essjae almost 4 yearsNo, you don't need to use adapter sharing. Look in the Hyper-V Manager under Virtual Switch Manager. Click on the virtual switch you've created, you can't use the default switch for this, and verify that it's set to "external network" and your network card is listed there. "Allow management operating system to share this network adapter" should also be checked.
-
essjae almost 4 yearsThis is the virtual switch you want to assign to the VM: Ethernet adapter vEthernet (External Virtual Switch)
-
Alexey Starinsky almost 4 yearsI added the screenshots of the switch configuration, see EDIT2. Are they OK?
-
essjae almost 4 yearsThat looks correct. Does the Ubuntu VM get an IP from DHCP with that config?
-
Alexey Starinsky almost 4 yearsIf I set 'manual' in Ubuntu settings the IP address is 192.168.0.113, but there is no internet. With 'automatic' the IP address is 192.168.0.103 and the internet works fine.
-
mr_squall over 3 yearsAfter i add external switch i lost connection to Host by ip-address used by network adapter listed in that switch.