Python Host-Only DHCP Boot Up Script

python script logo

I had an annoying problem with VirtualBox and host-only networking on a Linux guest.

I wanted to use a “real” DHCP server for address allocation on some of the host-only networks, instead of the limited one provided by VirtualBox’s host-only networking. The problem was that the host-only interfaces (e.g. vboxnet0, etc) were not being created until the VMs which use them were started. so the DHCP server had no interfaces to listen on and was bombing out with this message:

dhcpd: Not configured to listen on any interfaces!

The manual workaround was simple, but annoying: I had to remember to (quickly!) start the DHCP server after starting the first VM so the address-allocation would work. The DNS server the VMs were supposed to use had similar problems: it couldn’t receive requests from the host-only networks without a restart.

The obvious solution was: create the host-only interfaces automatically on system boot, and make sure the DHCP and DNS servers were listening on the host-only networks.

Google didn’t show me any solutions to this problem, so I wrote this small Python script to extract the correct configurations for the host-only interfaces:

The script is executed from /etc/rc.local on boot as the user who runs VirtualBox VMs (which is required to access the host-only network cofiguration), followed by restarting the DHCP and DNS servers, by adding the following lines to /etc/rc.local:

# bring up vboxnet interfaces
sudo -u piper /home/piper/bin/

# Now kick dhcpd and bind. They might not be running
/etc/init.d/isc-dhcp-server restart || true
/etc/init.d/bind9 restart || true

…and I never have to remember to restart the DHCP and DNS servers manually again 🙂




Leave a Reply