Networking setup for LXC

22 Nov 2016
Posted by stylesen

There are many ways in which we can setup networking for LXC containers - simple to highly complex. In this blog post I will get the simple steps required in order to have networking work for LXC containers using libvirt. It is hard to create bridges on WiFi interfaces unless your network foo is high (YMMV), but libvirt makes things simple irrespective of the interface. When your dev box is a laptop and want to use LXC on it, then instead of spending hours to get the networking work with the WiFi or avoid getting stranded to cable when using LXC on the laptop, libvirt comes handy. The steps below are tested on Debian Stretch / Testing / Unstable / Sid - give it a shot on other distros with equivalent packages.

Install the required packages and start the virtual bridge
# apt-get install libvirt-clients libvirt-daemon-system ebtables dnsmasq
# virsh net-start default
# /sbin/ifconfig -a

There should be a new virtual bridge seen as follows:
virbr0    Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Link all your containers to the new virtual bridge by editing /etc/lxc/default.conf as seen below:
# cat /etc/lxc/default.conf
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = virbr0

Make the default virtual network bridge interface start when host boots:
# virsh net-autostart default
# virsh net-info default
Name:           default
UUID:           xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Active:         yes
Persistent:     yes
Autostart:      yes
Bridge:         virbr0