How to configure Firewalld#

Allow DHCP, DNS with Firewalld#

In order to allow instances to access the DHCP and DNS server that LXD runs on the host when using firewalld you need to add the host’s bridge interface to the trusted zone in firewalld.

To do this permanently (so that it persists after a reboot) run the following command:

firewall-cmd --zone=trusted --change-interface=<LXD network name> --permanent

E.g. for a bridged network called lxdbr0 run the command:

firewall-cmd --zone=trusted --change-interface=lxdbr0 --permanent

This will then allow LXD’s own firewall rules to take effect.

How to let Firewalld control the LXD’s iptables rules#

When using firewalld and LXD together, iptables rules can overlaps. For example, firewalld could erase LXD iptables rules if it is started after LXD daemon, then LXD container will not be able to do any oubound internet access. One way to fix it is to delegate to firewalld the LXD’s iptables rules and to disable the LXD ones.

First step is to allow DNS and DHCP.

Then to tell to LXD totally stop to set iptables rules (because firewalld will do it):

lxc network set lxdbr0 ipv4.nat false
lxc network set lxdbr0 ipv6.nat false
lxc network set lxdbr0 ipv6.firewall false
lxc network set lxdbr0 ipv4.firewall false

Finally, to enable iptables firewalld’s rules for LXD usecase (in this example, we suppose the bridge interface is lxdbr0 and the associated IP range is 10.0.0.0/24:

firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -i lxdbr0 -s 10.0.0.0/24 -m comment --comment "generated by firewalld for LXD" -j ACCEPT
firewall-cmd --permanent --direct --add-rule ipv4 filter OUTPUT 0 -o lxdbr0 -d 10.0.0.0/24 -m comment --comment "generated by firewalld for LXD" -j ACCEPT
firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -i lxdbr0 -s 10.0.0.0/24 -m comment --comment "generated by firewalld for LXD" -j ACCEPT
firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -s 10.0.0.0/24 ! -d 10.0.0.0/24 -m comment --comment "generated by firewalld for LXD" -j MASQUERADE
firewall-cmd --reload

To check the rules are taken into account by firewalld:

firewall-cmd --direct --get-all-rules

Warning: what is exposed above is not a fool-proof approach and may end up inadvertently introducing a security risk.