Static IP Addresses for Virtual Machines in Hyper-V
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.
Create Internal Switch
New-VMSwitch -Switch "SwitchName" -SwitchType InternalThis will create a new
Internal Switchin Hyper-V, but also a newNetwork Adapteron your host machine.Get the
ifIndexof the new adapterGet-NetAdapterYou will get an similar output:
Name InterfaceDescription ifIndex Status ---- -------------------- ------- ------ vEthernet (SwitchName) Hyper-V Virtual Ethernet Adapter #3 56 UpAssign an IP Address to the adapter.
New-NetIPAddress -IPAddress 198.51.100.1 -PrefixLength 24 -InterfaceIndex 56Note:
198.51.100.0/24is defined asTEST-NET-2in RFC5737. Should only be used for documentation and examples.Replace
InterfaceIndexwith theifIndexfor adapter on your host.Now we need to create a NAT gateway
New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 198.51.100.0/24Assign your Virtual Machine to
SwitchNameswitch.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