Skip to content

Instantly share code, notes, and snippets.

@ntenisOT
Last active July 25, 2023 10:00
Show Gist options
  • Save ntenisOT/d53cd246b0d4f52295183f16805fe8dc to your computer and use it in GitHub Desktop.
Save ntenisOT/d53cd246b0d4f52295183f16805fe8dc to your computer and use it in GitHub Desktop.
Configure NAT and Static IP in Hyper-V for Ubuntu and Windows VM

Configure NAT and Static IP in Hyper-V for Ubuntu and Windows VM

This guide explains how to create a NAT network in Hyper-V on Windows Server 2022, and how to configure an Ubuntu VM and a Windows VM with static IP addresses on this network.

Steps on Hyper-V Host (Windows Server)

  1. Open a PowerShell session as an administrator.

  2. Create an internal virtual switch named "NAT":

    New-VMSwitch -SwitchName "NAT" -SwitchType Internal
  3. Identify the interface index of the NAT switch:

    Get-NetAdapter

    Look for "vEthernet (NAT)" in the output and note the corresponding interface index.

  4. Assign an IP address to the NAT switch:

    New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex <InterfaceIndex>

    Replace <InterfaceIndex> with the interface index you identified in the previous step.

  5. Create a NAT object:

    New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 192.168.0.0/24

During Ubuntu Installation

When the installer asks for network details, fill in the fields as follows:

  • Subnet: 192.168.0.0/24. This is the IP address range for your network.
  • Address: Choose an address within the range 192.168.0.2 to 192.168.0.254 that is not in use by another device. This will be the IP address of your Ubuntu VM.
  • Gateway: 192.168.0.1. This is the IP address of the NAT switch in your Hyper-V setup, which acts as the default gateway.
  • Name servers: 8.8.8.8, 8.8.4.4. These are the DNS servers that your VM will use to resolve domain names.
  • Search domains: Leave this field empty unless you have a specific domain that you want to use for DNS searches.

Please note that these values are based on the network configuration described in this guide. Adjust them as necessary if your network is set up differently.

Steps in Ubuntu VM

  1. Open a terminal.

  2. Edit the Netplan configuration file. The exact filename can vary, but it's typically 01-netcfg.yaml or 50-cloud-init.yaml:

    sudo nano /etc/netplan/01-netcfg.yaml
  3. Modify the file to set a static IP address. For example:

    network:
      version: 2
      renderer: networkd
      ethernets:
        ens3:
          dhcp4: no
          addresses:
            - 192.168.0.2/24
          gateway4: 192.168.0.1
          nameservers:
            addresses: [8.8.8.8, 8.8.4.4]

    Replace ens3 with the name of your network interface, if it's different.

  4. Save and close the file. In nano, you can do this by pressing Ctrl+X, then Y, then Enter.

  5. Apply the changes:

    sudo netplan apply

Steps in Windows VM

  1. Open Network & Internet settings.

  2. Open the Ethernet settings, then click on the network adapter that you want to configure.

  3. In the settings for the network adapter, scroll down to the "IP settings" section and click on "Edit".

  4. Change to a static IP configuration and enter the IP address and DNS details:

    • IP address: 192.168.0.3
    • Subnet prefix length: 24
    • Gateway: 192.168.0.1
    • Preferred DNS: 8.8.8.8
    • Alternate DNS: 8.8.4.4
  5. Save the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment