Translation(s): English - Français
Alternatives to this network setup for containers can be found on the LXC main page.
In case of routed dhcp backed network provided by the host using libvirt, here is an example.
Dependencies
Debian distribution: These instruction have not been written for Debian 6.0 (Squeeze). For instance virsh net-info is not supported until Wheezy.
Installing libvirt
Debian Jessie and below:
apt-get install libvirt-bin
Debian Stretch/Sid:
apt-get install libvirt-clients libvirt-daemon-system
The configuration below also requires ebtables and dnsmasq:
apt-get install ebtables dnsmasq
After a restart, the ebtable module should be loaded and the default network should be set up. You can verify this with:
virsh net-info default
Using default network
The default network configuration usually resides in /var/lib/libvirt/network/default.xml
(in jessie_di_alpha_1 it's /etc/libvirt/qemu/networks/default.xml)
root@debian:/# cat /var/lib/libvirt/network/default.xml <!-- WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE OVERWRITTEN AND LOST. Changes to this xml configuration should be made using: virsh net-edit default or other application using the libvirt API. --> <network> <name>default</name> <uuid>999d40xx-22db-c999-f68x-42cfbeba99a3</uuid> <forward mode='nat'/> <bridge name='virbr0' stp='on' delay='0' /> <ip address='192.168.122.1' netmask='255.255.255.0'> <dhcp> <range start='192.168.122.100' end='192.168.122.254' /> </dhcp> </ip> </network>
- Start the network and verify existence of virbr0 device
virsh net-start default ifconfig -a
- Link your container to the new device and setup a dhcpclient inside the container
root@debian:/# cat /var/lib/lxc/a_container/config [...] lxc.network.type = veth lxc.network.flags = up lxc.network.link = virbr0 lxc.network.hwaddr = 00:FF:AA:00:00:01 lxc.network.ipv4 = 0.0.0.0/24 [...]
- Optional: If you like to give the containers always the same IP (mapped by mac address) you could enhance the dhcp node in your network config
<dhcp> <range start="192.168.122.100" end="192.168.122.254" /> <host mac="00:FF:AA:00:00:01" name="foo.example.com" ip="192.168.122.101" /> <host mac="00:FF:AA:00:00:02" name="bar.example.com" ip="192.168.122.102" /> </dhcp>
- Optional: Make default network start when host boots
virsh net-autostart default virsh net-info default