How to map static IP address using dnsmasq under LXD (Linux containers)

last updated in Categories , , , ,

I am using LXD (Linux container) to host Apache, MySQL, Perl and PHP-based app. I would like to assign or map fixed IP address to my MySQL container. How do I allow dnsmasq DHCP server to assign a static IP address to LXD container based on name or MAC address?

You can set per host/container parameters for the DHCP server. An LXD machine (VM) can be always given the same hostname, IP address and leas time. For example, you can tell dnsmasq to always given the VM msyqldb1 the IP address


How to always allocate the LXD VM/machine named db the IP address

Your dnsmasq DHCP server can read the file contains information about one host per line. The default file name is /var/lib/lxd/networks/lxdbr0/dnsmasq.hosts. The format of a line is as follows:




The advantage of storing DHCP host information in this file is that it can be changed without re-starting dnsmasq server when the file will be re-read when dnsmasq receives SIGHUP. Edit the /var/lib/lxd/networks/lxdbr0/dnsmasq.hosts file, enter:
$ sudo vi /var/lib/lxd/networks/lxdbr0/dnsmasq.hosts
Sample config:

# always set IP to for apache1 vm,apache1
# always set IP to for db1 vm having mac addr 00:16:ee:62:ff:90

Save and close the file. Send SIGHUP to dnsmasq using killall command as follows:
$ sudo killall -SIGHUP dnsmasq
Now you can start lxd vm or restart already running lxd vm:
$ lxc restart db1
$ lxc start db1

Verify new IP address:
$ lxc list
$ lxc info db1

Sample outputs:

Fig.01: Set DHCP IPs of lxd/lxc (Linux containers) on Linux
Fig.01: Set DHCP IPs of lxd/lxc (Linux containers) on Linux


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

1 comment

  1. Please make faq for how to assign private communication between containers and having internet access from them.

    Still, have a question? Get help on our forum!