1

I'm using Laravel Homestead with Vagrant, and I thought I'd would make sense to use Hyper-V instead of Virtual Box, as it's integrated with Windows.

Here is my problem. Every now and then my Hyper-v virtual machine will get a new IP, usually something 172.20.83.X. This messes up my hosts file as I assing ips to test domains. Thus every time my hyper-v server gets a new ip, I have to re-enter the new ip, which quickly becomes very tedious.

I'm wondering if there is a way I can set the ip to always be the same on my machine.

I tried tinkering with the virtual switch manager for the server, but ended up losing complete internet.

  • What type of virtual switch is yours? external? internal? or private? – S.Leon Sep 3 '18 at 6:50
  • @S.Leon, it's external :) – Jazerix Sep 3 '18 at 13:20
1

It seems that the external DHCP server with the scope of 172.20.23.X will provide IP addresses for your virtual machines. You can use filters in the DHCP console to prevent the DHCP server from issuing addresses to these machines by entering the mac address of the virtual machine. As shown below: enter image description here

enter image description here

Then you can use other DHCP to set reserved addresses for your virtual machine (my understanding is that you don't want to get the address in the 172.20.83.X scope, so use other DHCP) or manually configure static addresses so that their information is the same as the contents of your host file.

  • Also, make sure that you're not using a dynamic MAC address on your Hyper-V VM. This will cause you to get new IPs even with the DHCP reservation. You can check this under the Network adapters advanced settings...I believe, by default, they're set to dynamic. – essjae Sep 7 '18 at 18:19
  • This is in no way a criticism of your answer, as at least it provides "workaround" to be applied post-provisioning, but not to be able to specify a static IP address, programatically, while provisioning the VM, is a huge pain. Many workflows require creating and destroying these types of VMs routinely, so any degree of "manual" work required is problematic. Hopefully, Vagrant will address this via github.com/hashicorp/vagrant/issues/8384 – Ben Johnson Nov 28 '18 at 16:38
0

The Asker, and anyone with a similar use-case, is likely operating on a workstation (not Windows Server edition), for local development purposes, in which case a relatively simple solution exists.

Create the Switch

In Hyper-V Manager, create a new Virtual Switch, under Actions panel -> Virtual Switch Manager... -> Create Virtual Switch. Assuming the VM should be able to connect to the Internet, choose the External switch type.

On the next screen, give the switch a name, such as internet-enabled, and choose the physical network interface that is connected to the Internet.

The resultant switch should look something like this:

enter image description here

Hard-Code the Switch's IP Address (Gateway for the VM)

Locate the network adapter that is associated with the new switch, i.e., under Control Panel -> Network and Internet -> Network Connections; its name will reflect the name given in the previous step, e.g., vEthernet (internet-enabled). Right-click the adapter and choose Properties.

Next, click Internet Protocol Version 4 (TCP/IPv4) and click Properties. Enter 192.168.10.1 for the IP address, and 255.255.255.0 for the Subnet mask. The settings should look like this:

enter image description here

Assign a Static IP Address in Guest VM

The final step is to assign a static IP address in the guest VM.

Unfortunately, Vagrant does not (as of v2.2.1) support static IP configuration (see: https://github.com/hashicorp/vagrant/issues/8384 ), so this static IP cannot be assigned prior to provisioning the VM.

Until Vagrant supports this capability, one can simply provision the VM normally and edit the network configuration subsequently.

The process for setting a static IP varies per guest OS, but in Ubuntu 18.04 LTS, for example, it's as simple as editing /etc/netplan/01-netcfg.yaml to include values that match the virtual switch configuration from above (the spaces must be exact!):

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: no
      addresses: [192.168.10.10/24]
      gateway4: 192.168.10.1
      nameservers:
        addresses: [8.8.8.8,8.8.4.4]

After saving the file, apply the changes with:

$ sudo netplan apply

The VM should reflect the static IP immediately, which can be confirmed with ifconfig:

$ sudo ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.10  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::215:5dff:fe38:12a  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:38:01:2a  txqueuelen 1000  (Ethernet)
        ...

The host can now ping the guest at 192.168.10.10, and the guest can ping the host at 192.168.10.1.

References:

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.