When a computer connects to a TCP/IP¹ network, DHCP (the Dynamic Host Configuration Protocol) is typically used to dynamically assign an IP address to the network device. In the years before DHCP every computer needed to have a manually-assigned network configuration that included information such as an IP address, a subnet mask, network gateway, DNS server addresses, and so on. Most of us haven't had to think about these numbers in well over a decade, but there are times when it's very important that a given computer is always assigned the same IP address.
At my office, where I am on a team to develop a new suite of software for the company, direct access to various development resources is restricted to a handful of computers — mine being one of them. My machine is always assigned the same IP address so long as I'm working at a handful of "approved locations". This is accomplished by sending the MAC address (the media access control address) to the DHCP server at the time of connection. If the MAC address matches a rule, then a specific IP address is given to that network device.
My Windows VM depends on this IP being assigned to it as almost all of my development is done from within this virtual container. The problem is that, because I replaced the IT-supplied Windows installation with Ubuntu, the reserved IP address is being given to Ubuntu rather than Windows. Ubuntu does not need direct access to the development resources, so a dynamic IP would be fine. So how can we make sure the reserved address is always given to the Windows VM?
Luckily, it's really very simple.
First, let's fire up the terminal and get the name of the network device with the DHCP reservation.
ifconfig
In my case, the wired network adapter is called enp0s25
. Yours might be eth0
if it's wired, wlan0
if it's wireless, or something completely different so be sure to get the proper name. The MAC address for the device can be seen in the ifconfig
listing as HWaddr
(Hardware Address). That's the number we want to use for the VirtualBox VM.
Open VirtualBox, select the VM you want to have the custom MAC address and click the "Settings" button.
Expand the "Advanced" tab and set the MAC Address to the same 12-character value as you found in ifconfig
. The colons are unnecessary here.
Now that VirtualBox is configured, we need to change the "MAC Address" value on Ubuntu. No two machines on a network can have the same IP address or MAC address. For this reason, we'll make a quick change to the reported address from Ubuntu by incrementing the MAC address by one.
In the terminal:
sudo ifconfig {network device} down
sudo ifconfig {network device} hw ether xx:xx:xx:xx:xx:xx
sudo ifconfig {network device} up
For me, this looked like:
sudo ifconfig enp0s25 down
sudo ifconfig enp0s25 hw ether 54:ee:75:80:49:c5
sudo ifconfig enp0s25 up
One thing to keep in mind with the MAC address value is that hexadecimal values are used. This means we can only use the numbers 0 to 9 and the characters a to f. Anything outside of this range will result in an error.
Once this is done, hit ifconfig
again to confirm you've been given a new, dynamic IP address. Now you can launch your virtual machine and confirm you've been assigned the proper reserved IP address there.
—
- TCP/IP is the most commonly-used network protocol in the world. If you're reading this — and it's the early 21st century — you're probably using TCP/IP right now.