19

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.

3
  • 2
    No, the default switch isn't configurable. I believe this still works in the newer versions of Win 10: petri.com/using-nat-virtual-switch-hyper-v
    – essjae
    Feb 18, 2020 at 18:57
  • Can you set a static address in Control Panel > Network Connections > Change adapter settings via right-clicking the vEthernet switch?
    – harrymc
    Mar 26, 2020 at 9:01
  • @harrymc yes I can set a static address, but it will be automatically overwritten to some random address after system reboot
    – Vladimir
    Mar 26, 2020 at 12:26

3 Answers 3

13
+50

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.

8
  • 192.168.100.1 should be avoided since it's the standard IP of cable modems' WebUI.
    – JW0914
    Mar 26, 2020 at 13:36
  • @JW0914: It's just the first thing that came into my mind.
    – harrymc
    Mar 26, 2020 at 14:10
  • @harrymc I've posted a similar question here: superuser.com/q/1541599/678751 Can you please elaborate on your suggestion of "creating a new virtual switch" w/ static IP?
    – Wlad
    Apr 13, 2020 at 17:55
  • Default switch IP changed while using this script , but my host is not getting IP , later i tried configured a static IP for host , but still unable to ping gateway
    – Arun
    Aug 17, 2020 at 4:57
  • Isn't the "random" address assigned because of the subnet settings? For example, if have a "static" IP of 192.168.56.1, and a subnet of 255.255.255.240, won't I get a random IP in the range 192.168.56.1 - 192.168.56.14? If I changed the subnet to 255.255.255.252 will this reduce the random range?
    – Shiraz
    Oct 17, 2020 at 18:30
6

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

1
  • 1
    Nice! You have a small typo, though - last result should be "192.168.1.31"
    – Shiraz
    Oct 17, 2020 at 18:39
-1

0

I found an alternative solution as neither changing the IP of the HyperV default switch via netsh nor creating an alternative switch worked for me. Instead of directly changing the IP of the Hyper-V adapter we can restrict the possibilities for the algorithm granting the IP addreses to the switch by adding loopback interfaces that block IP ranges. Loopback interfaces can be introduced using hdwwiz.exe > Next > "..manually select from a list" > Network Adapters > Manufacturer Microsoft > Microsoft KM-TEST Loopback Adapters The IP address and subnet mask for the loopback adapter can be set by "view network connections", right click the newly created interface > Properties/Internet Protocol Version4 /Properties

We can exploit that the subnet mask of the Hyper V default switch seems to be constantly at 255.255.240.0. So within the 192.168.x range it always uses one of 192.168.0.1, 192.168.16.1, 192.168.32.1 ... 192.168.240.1 as IP addresses. We can introduce e.g. a loopback interface with IP 192.168.0.1 and subnet mask 255.255.128.0, that will block all IPs from 192.168.0.1 to 192.168.127.255 Now introduce another one with IP 192.168.128.1 and subnet mask 255.255.192.0, that will block all IPs from 192.168.128.1 to 192.168.191.255 Another one could be IP 192.168.192.1 with subnet mask 255.255.224.0, blocking 192.168.192.1 to 192.168.223.255 If you know also block 192.168.240.1 via a loopback interface using IP 192.168.240.1 and subnet mask 255.255.255.0 the only possible IP address the Hyper V adapter can have is 192.168.224.1 while there's still plenty of IP addresses available in the 192.168 range. Maybe its necessary to block the 172.16.x range in the same manner. On the other hand it might also be necessary for you to be less retsrictive in the 192.168.0.x range if there are devices relying on these to be available. There seems to be the possibility to introduce loopback interfaces via Powershell scripts but haven't tried that so far.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .