Blog about software development


Static IP addresses in a KVM network

23 Jan 2020 - by 'Maurits van der Schee'

In a previous post I have shown how to use the serial console in KVM (on Ubuntu 18.04) and how to access it on the KVM CLI. In this post I will show how to set up static IP addresses for your KVM virtual machines. Static IP addresses are a requirement for doing port forwarding, a topic we will touch in another post in this blog series. In this post we will also explore how to reconfigure the IP range for your KVM virtual machines.

Set fixed IP address

In order to find the MAC address of the virtual machine named "maurits-cloud" you can run:

virsh dumpxml maurits-cloud | grep "mac address"

This shows the XML of the virtual machine named "maurits-cloud" and searches for a line that contains the string "mac address".

Now show the "default" network's XML using "virsh" with the command:

virsh net-dumpxml default

Now look for the following block:

  <range start='' end=''/>

We change the range to start from 100 instead of 2 with the following commands:

virsh net-update default delete ip-dhcp-range "<range start='' end=''/>" --live --config
virsh net-update default add ip-dhcp-range "<range start='' end=''/>" --live --config

And we add a reservation for the specific host based on it's MAC address using:

virsh net-update default add-last ip-dhcp-host "<host mac='52:54:00:b0:59:5e' name='maurits-cloud' ip=''/>" --live --config

We can run the following command to check the effectiveness of the changes:

virsh net-dumpxml default

It should now show:

  <range start='' end=''/>
  <host mac='52:54:00:b0:59:5e' name='maurits-cloud' ip=''/>

You may want to run the following in the guest to release and renew the DHCP lease:

sudo dhclient -r && sudo dhclient

NB: There is no need to destroy the network or restart any virtual machines like some manuals suggest.

Set the DHCP identifier in Ubuntu 18.04

If you have a Ubuntu 18.04 virtual machine, then it may sometimes not retrieve an IP address based on the MAC address, but on the machine identifier as specified in "/etc/machine-id". Apply the following to make the DHCP client send the MAC address as DHCP identifier. This ensures that the DHCP server responds with the static IP address you specified for the MAC address.

Look in the file "/etc/netplan/01-netcfg.yaml" and look for the line:

dhcp4: yes

and add the line "dhcp-identifier: mac" after it, so that it becomes:

dhcp4: yes
dhcp-identifier: mac

Note that this is a YAML file, so indentation matters.

Edit and restart the network

In order to make large changes to the network you need to edit the network configuration:

virsh net-edit default

After editing the configuration you save the file. The changes will NOT be effective until you destroy and start the network.

To destroy the network we first need to shutdown all virtual machines. To shutdown (destroy) a single virtual machine named "maurits-cloud" you can run:

virsh destroy maurits-cloud

Instead of "destroying" the virtual machines it is better to log in to the virtual machines and power them off manually.

You need to repeat this until all virtual machines have been shutdown. Now you can then restart the network using:

virsh net-destroy default
virsh net-start default

Next we need to start the virtual machines again. This command will start the virtual machine named "maurits-cloud":

virsh start maurits-cloud

After starting all virtual machines you will see that the new network configuration is in effect for the virtual machines.

Change IP range

Sometimes you want to use a different IP range. In the following example we show how to change the default KVM network from the "" range to the "" range. Let's start by editing our network configuration:

virsh net-edit default

And look for the line:

<ip address='' netmask=''>

Change it into:

<ip address='' netmask=''>

Also look for the line:

<range start='' end=''/>

Change it into (should fall within the defined address space):

<range start='' end=''/>

Also updates all "host" lines that look like this:

<host mac='52:54:00:b0:59:5e' name='maurits-cloud' ip=''/>

Change them to ensure their IP address is in the correct range (within the network, but outside the DHCP range):

<host mac='52:54:00:b0:59:5e' name='maurits-cloud' ip=''/>

This will NOT be effective until you destroy and start the network.

Next: Setting up port forwarding in KVM

In the next post I will show you how to set up port forwarding to KVM virtual machines. Port forwarding should be done towards virtual machines with a static IP address. Without static IP addresses the forwards may point to different virtual machines machines at some point in time, due to expiring DHCP leases.

Click here to read the next article (on port forwarding in KVM).

PS: Liked this article? Please share it on Facebook, Twitter or LinkedIn.