≡ Menu

KVM libvirt assign static guest IP addresses using DHCP on the virtual machine

I am using KVM/libvirt on Linux operating system and how do I assign static IP address using dnsmasq dhcpd server for my default virtual network switch?

By default, an instance of dnsmasq dhcpd server is automatically configured and started by libvirt for each virtual network switch needing it. Each virtual network switch can given a range of IP addresses provided to guests through DHCP. The default networking switch uses dnsmasq server.
Fig.01: Libvirt uses a program, dnsmasq for DNS and DHCP for default network.

Fig.01: Libvirt uses a program, dnsmasq for DNS and DHCP for default network.

View the current dnsmasq DHCP configuration

Type the following command to list networks
# virsh net-list
Sample outputs:

 Name                 State      Autostart     Persistent
 default              active     yes           yes

To see the default network information, enter:
# virsh net-dumpxml default
Sample outputs:

<network connections='2'>
  <forward mode='nat'>
      <port start='1024' end='65535'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:12:fe:35'/>
  <ip address='' netmask=''>
      <range start='' end=''/>

The DHCP range is between and

How to configure static guest IP addresses on the VM host

First find out your guest VM’s MAC addresses, enter:
# virsh dumpxml {VM-NAME-HERE} | grep -i '<mac'
# virsh dumpxml xenial | grep -i '<mac'

Sample outputs:

<mac address='52:54:00:4c:40:1c'/>

Please note down the MAC addresses of the xenial VM that you want to assign static IP addresses.

Edit the default network

Type the following command:
# virsh net-edit default
Find the following section:

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

Append the static IP as follows after range:

     <host mac='52:54:00:4c:40:1c' name='xenial' ip=''/>


  1. mac='52:54:00:4c:40:1c' – VMs mac address
  2. name='xenial' – VMs name.
  3. ip='' – VMs static IP.

Here is my complete file with three static DHCP entries for three VMs:

  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:12:fe:35'/>
  <ip address='' netmask=''>
      <range start='' end=''/>
      <host mac='52:54:00:a0:cc:19' name='centos7' ip=''/>
      <host mac='52:54:00:f7:a1:c8' name='puffy' ip=''/>
      <host mac='52:54:00:4c:40:1c' name='xenial' ip=''/>

Restart DHCP service:
# virsh net-destroy default
# virsh net-start default

Sample outputs:

Network default destroyed
Network default started

If you are running the guest/VM called xenial shutdown it:
# virsh shutdown xenial
# /etc/init.d/libvirt-bin restart
# virsh start xenial
# ping -a

Sample outputs:

PING ( 56(84) bytes of data.
64 bytes from icmp_seq=1 ttl=64 time=0.518 ms
64 bytes from icmp_seq=2 ttl=64 time=0.202 ms
64 bytes from icmp_seq=3 ttl=64 time=0.327 ms
--- ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.202/0.349/0.518/0.129 ms

Each time the guest or VM called xenial comes online (or rebooted for the kernel update) it will get as static IP address by dnsmasq DHCP server.

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 4 comments… add one }
  • zheliazko zlatev March 17, 2016, 7:08 pm

    good job :)

  • twostep April 4, 2016, 10:48 am

    But during destroy and start, all vm-s which are connected via this bridge lost their connection, and you must have shut and start VM-s again.
    There is any command to reload libvirt net configuration without destroy it?

  • Mohsen Pahlevanzadeh April 30, 2016, 2:14 pm

    Suppose I have a KVM under work. I want to add a guest, But I don’t want to stop my network. How do I do ?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , ,