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.
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:
<dhcp> <range start='192.168.122.2' end='192.168.122.254'/> </dhcp>
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='192.168.122.2' end='192.168.122.254'/>" --live --config virsh net-update default add ip-dhcp-range "<range start='192.168.122.100' end='192.168.122.254'/>" --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='192.168.122.10'/>" --live --config
We can run the following command to check the effectiveness of the changes:
virsh net-dumpxml default
It should now show:
<dhcp> <range start='192.168.122.100' end='192.168.122.254'/> <host mac='52:54:00:b0:59:5e' name='maurits-cloud' ip='192.168.122.10'/> </dhcp>
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.
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 "
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:
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.
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.
Sometimes you want to use a different IP range. In the following example we show how to change the default KVM network from the "192.168.122.0/24" range to the "10.0.122.0/24" range. Let's start by editing our network configuration:
virsh net-edit default
And look for the line:
<ip address='192.168.122.1' netmask='255.255.255.0'>
Change it into:
<ip address='10.0.122.1' netmask='255.255.255.0'>
Also look for the line:
<range start='192.168.122.100' end='192.168.122.254'/>
Change it into (should fall within the defined address space):
<range start='10.0.122.100' end='10.0.122.254'/>
Also updates all "host" lines that look like this:
<host mac='52:54:00:b0:59:5e' name='maurits-cloud' ip='192.168.122.10'/>
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='10.0.122.10'/>
This will NOT be effective until you destroy and start the network.
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.