Life's random bits By b1thunt3r (aka Ishan Jain)…
Static IP Addresses for Virtual Machines in Hyper-V

Static IP Addresses for Virtual Machines in Hyper-V

Ishan jain
By Default, Default Switch in Hyper-V only have Dynamic IP addresses for Virtual Machines.

The Default Switch in Hyper-V is not meant to have Static IP. When you reboot your host machine, the Default Switch gets a new IP range that is not used by known network by host machine.

One solution is to create an Internal Switch with NAT.

  1. Create Internal Switch

    New-VMSwitch -Switch "SwitchName" -SwitchType Internal
    

    This will create a new Internal Switch in Hyper-V, but also a new Network Adapter on your host machine.

  2. Get the ifIndex of the new adapter

    Get-NetAdapter
    

    You will get an similar output:

    Name                      InterfaceDescription                    ifIndex Status
    ----                      --------------------                    ------- ------
    vEthernet (SwitchName)    Hyper-V Virtual Ethernet Adapter #3          56 Up    
    
  3. Assign an IP Address to the adapter.

    New-NetIPAddress -IPAddress 198.51.100.1 -PrefixLength 24 -InterfaceIndex 56
    

    Note: 198.51.100.0/24 is defined as TEST-NET-2 in RFC5737. Should only be used for documentation and examples.

    Replace InterfaceIndex with the ifIndex for adapter on your host.

  4. Now we need to create a NAT gateway

    New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 198.51.100.0/24
    
  5. Assign your Virtual Machine to SwitchName switch.

  6. Assign static IP to your VM

    ip    198.51.100.11
    mask  255.255.255.0
    gw    198.51.100.1
    dns   1.1.1.1
    

Resources