LXC Ubuntu 12.04 Creating containers with veth and Setting IP hwaddr

November 19, 2014 | 3 Minute Read

LXC containers in ubuntu

Creating LXC containers in ubuntu 12.04 LTS and setting IP, veth

Say you dont want to use the default lxcbr0 created by the lxc and want to have your own linux bridge.

  • Create two containers container 1 and container 2
sudo lxc-create -t ubuntu -n container1
sudo lxc-create -t ubuntu -n container2

Creating first container always takes longer time. You can use lxc-clone as well for faster creation of the containers

  • Deleting the default bridge that lxc will create and make containers without bridge and a variable veth.
sudo vi /var/lib/lxc/container1/config

Remove  the line with default bridge(delete following line from the config file):

lxc.network.link = lxcbr0

Change hwaddr with whatever u want and add the veth pair to be whatever you want (say i want veth 55 connected to container1 and have hwaddr 00:00:00:00:00:37). Add veth.pair if its not already there.

lxc.network.hwaddr = 00:00:00:00:00:37
lxc.network.veth.pair = veth55

The following command may or may not work if you insert it to config file

lxc.network.ipv4 = 192.168.0.9/24

Do the same for container 2.

sudo vi /var/lib/lxc/container2/config

Delete following:

lxc.network.link = lxcbr0

Insert following:

lxc.network.hwaddr = 00:00:00:00:00:d8
lxc.network.veth.pair = veth216

The following command may or may not work if you insert it to config file

lxc.network.ipv4 = 192.168.0.41/24
  • Starting containers in background mode. (-d)
sudo lxc-start -d -n container1
sudo lxc-start -d -n container2
  • Check to if the containers are running and debug
sudo lxc-list --fancy

[Optional-if the containers aren't running or stopped] If the containers are not running however stopped which means something went wrong. You can always check what went wrong by trying to start container as following

sudo lxc-start -n container1

Now search internet for whatever problem you might have.

Lets get back to business.

  • Entering the containers to set IP
sudo lxc-attach -n container1
ifconfig eth0 192.168.0.9 netmask 255.255.255.0 up
sudo lxc-attach -n container2
ifconfig eth0 192.168.0.41 netmask 255.255.255.0 up

Exit from the containers with just an exit command.

  • In HOST machine we have to deletede fault bridge (if there is any) and create net veth
  • Show all the bridges
    brctl show
  • Deleting default (any) bridge
    ip link set lxcbr0 down
    brctl delbr lxcbr0
  • Check to see if the bridge is still there
    ifconfig -a
  • Add your own linux bridge skbbr
    brctl addbr skbbr
  • Add interfaces to bridge:
    brctl addif skbbr veth55
    brctl addif skbbr veth216
  • Show all the bridges. this time you will see the interfaces veth55 and veth216 added to the bridge skbbr
    brctl show
  • Check ifconfig to see if the bridge is up. Look for skbbr.  If you don't see it but you can see it in ifconfig -a then you need to bring the bridge up.
    ip link set skbbr up
  • Now you can log into any container and ping from one to another.
    sudo lxc-attach -n container1
    ping 192.168.0.41

Don’t ask questions in this blog please, I don’t have time to answer those.

Thanks

obaida