Host-Only network - DHCP address reservation?

This is for discussing general topics about how to use VirtualBox.
Post Reply
czezz
Posts: 41
Joined: 22. Apr 2012, 12:07

Host-Only network - DHCP address reservation?

Post by czezz »

Hi everyone,
Is there a way to do DHCP address reservation on Host-Only network?
By that i mean: assign specific address (eg. 192.168.56.120) to specific VM (with host-only adapter)
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Host-Only network - DHCP address reservation?

Post by mpack »

AFAIK, no, there is no address reservation scheme in the VirtualBox DHCP server. However many simple home routers also do not have an IP reservation scheme but you can still use a static IP address, you just choose one that fits the netmask and is never likely to be assigned by DHCP.

Also look at the "VBoxManage dhcpserver" commands discussed in the user manual. As mentioned I see no obvious way to reserve an IP address, but I do see a way to limit the range of IP address which the DHCP server will hand out.

Or you can switch off the DHCP server and use static IPs exclusively. I don't imagine that you're having to manage a very large network this way.
czezz
Posts: 41
Joined: 22. Apr 2012, 12:07

Re: Host-Only network - DHCP address reservation?

Post by czezz »

Thanks @mpac

I workarounded it by:
1. temporary reducing pool to only 1 desired address
2. starting up VM (letting it get the only available IP in the pool)
3. shutting down VM
4. reverting pool to normal

At the next boot that VM gets now desired IP (assigned on a previous boot).
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Host-Only network - DHCP address reservation?

Post by mpack »

czezz wrote: 31. Jul 2023, 12:00 At the next boot that VM gets now desired IP (assigned on a previous boot).
I don't believe that can be safely assumed. Yes, the guest OS may now have a preferred IP address which the DHCP may or may not honor, or possibly it's just still inside the lease period of your initial test.

Long term I don't believe yours is a real solution, I suggest you use the solution I outlined above involving limiting the pool and using a static IP that's outside the pool but inside the netmask.
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Host-Only network - DHCP address reservation?

Post by scottgus1 »

In the main Virtualbox configuration folder ".Virtualbox" , there is a ".leases" file for each Host-Only and NAT Network. This file ties a particular IP address to a particular MAC address.

It could be possible to get the network set up just the way you want it then copy that file. When Virtualbox is shut off completely and the background VboxSVC service is off, writing the copy back over the original should theoretically reset Virtualbox to the desired network settings.

Note that the file gets re-written at various times (there's also a "-prev" version of the file, implying regular re-writes by VboxSVC service, like the VMs' .vbox files). So writing the copy back over the original would clear what new settings have been made, possibly damaging other newer Virtualbox networks.

Some reasonable scripting could reassemble a new .leases file with new info in place and the desired old info kept.

Virtualbox does not have any ability to automate this kind of tweak. It is strictly a roll-your-own.
fth0
Volunteer
Posts: 5678
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: Host-Only network - DHCP address reservation?

Post by fth0 »

Like any decent DHCP server, the VirtualBox DHCP server remembers the assignment of IP addresses to MAC addresses, so the workaround of the OP will work as expected (as long as the MAC addresses aren't changed, of course).

The assignments are saved in files that can be found in the VirtualBox global configuration directory and whose names end with ".leases". After rebooting the host and before executing any VirtualBox component, one can edit the files by hand, in case that one wishes to configure multiple assignments. AFAIK, the "VBoxManage dhcpserver" command doesn't offer an option to configure the assignments.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Host-Only network - DHCP address reservation?

Post by mpack »

IMO, if one can avoid manually editing VirtualBox config files then one should do so. Even worse is to have a script modifying them.

I don't see any useful risk/reward balance here. The already suggested solution is risk free.
Post Reply