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 Internal
This will create a new
Internal Switch
in Hyper-V, but also a newNetwork Adapter
on your host machine.Get the
ifIndex
of the new adapterGet-NetAdapter
You will get an similar output:
Name InterfaceDescription ifIndex Status ---- -------------------- ------- ------ vEthernet (SwitchName) Hyper-V Virtual Ethernet Adapter #3 56 Up
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 asTEST-NET-2
in RFC5737. Should only be used for documentation and examples.Replace
InterfaceIndex
with theifIndex
for adapter on your host.Now we need to create a NAT gateway
New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 198.51.100.0/24
Assign your Virtual Machine to
SwitchName
switch.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