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

Posted on in Categories , , , , last updated January 23, 2017

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 192.168.1.100.

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

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:

{ipaddr},{hostname}

OR

{macadd},{ipaddr},{hostname}

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 10.147.164.2 for apache1 vm
10.147.164.2,apache1
# always set IP to 10.147.164.3 for db1 vm having mac addr 00:16:ee:62:ff:90
00:16:ee:62:ff:90,10.147.164.3,db1

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 and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

1 comment

Leave a Comment