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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • zheliazko zlatev Mar 17, 2016 @ 19:08

    good job :)

  • twostep Apr 4, 2016 @ 10:48

    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 Apr 30, 2016 @ 14:14

    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 Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum