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

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.

ADVERTISEMENTS

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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • dimdo Feb 19, 2017 @ 15:50

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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.