Hyper-v Default Switch static IP

Is it possible to set a static IP address for Hyper-V's default switch adapter? Every time I reboot my PC the address has changed.


Solution 1:

You can set a static address in Control Panel > Network Connections > Change adapter settings by right-clicking the vEthernet switch, but Windows will reset it to a random address after the reboot, and that action cannot be disabled.

A solution could be to reset it always to the same value by using the netsh command or its PowerShell alternatives. Netsh needs to be run as Administrator.

The command syntax is:

netsh interface ip set address [name=]InterfaceName [source=]{dhcp | static [addr=]IPAddress[mask=]SubnetMask [gateway=]{none | DefaultGateway [[gwmetric=]GatewayMetric]}}

An example command to set the switch to static IP of 192.168.100.1, mask 255.255.255.0 and gateway none is:

netsh interface ip set address name="vEthernet (Default Switch)" static 192.168.100.1 255.255.255.0 none

If necessary, you could set a script containing the command to run when Windows is started or on user logon.

Another solution would be to create a new switch, whose IP address will stay, unlike the default switch.

Solution 2:

I'm aware that it may be a little off topic, but since we're setting this IP in order to e.g connect via SSH to our Hyper-V instance, then it's possible to assign static MAC address and then write some "self-discovery" script so we will dont have to use assign ip to vEthernet everytime

Assign static MAC:

Right click on Hyper-V instance -> Settings > NetworkCard > Advanced Functions -> Choose Static MAC and FILL MAC

enter image description here

In my case it is powershell script which extracts IP from arp discovery basing on that static MAC and then connects to it via SSH

$str = ((arp -a | findstr /i 00-15-5D-01-83-0B)[0]); 
$ip = $str.Split(" ")[2].Trim()
ssh root@$ip

Explaination:

arp

Displays and modifies the IP-to-Physical address translation tables used by
address resolution protocol (ARP).

ARP -s inet_addr eth_addr [if_addr]
ARP -d inet_addr [if_addr]
ARP -a [inet_addr] [-N if_addr] [-v]

  -a            Displays current ARP entries by interrogating the current
                protocol data.  If inet_addr is specified, the IP and Physical
                addresses for only the specified computer are displayed.  If
                more than one network

arp -a | findstr /i 00-15-5D-01-83-0B

/i = ignore case

Returns:

 192.168.1.31          00-15-5d-01-83-0b     dynamic
 192.168.43.170        00-15-5d-01-83-0b     static

[0] Index

Picks:

192.168.1.31          00-15-5d-01-83-0b     dynamic

$str.Split(" ")[2].Trim()

Returns:

192.168.1.31

then ssh connects to it