Routed (static) networking with lxc

Host configuration

We will call the veth interface veth0 for simplicity, although lxc names it vethXXXXX where XXXXX is a random string. It is assumed the primary network interface (WAN) on the host is eth0.

Once you have your container running, run the following commands

     ip addr add dev veth0 $HOSTIP/$HOSTNETMASK peer $GUESTIP/32

And finally you will need to enable ip forwarding on the host:

     echo 1 > /proc/sys/net/ipv4/ip_forward

or change the sysctl setting net.ipv4.ip_forward to 1 in sysctl.conf.

Guest configuration

All you need in the lxc configuration is

     lxc.network.type = veth

Don't set lxc.network.link

Then for routing

    ip addr add dev eth0 $GUESTIP/32 peer $HOSTIP/$HOSTNETMASK
    ip route add default via $HOSTIP

Automating it all

  lxc.network.script.up = /path/to/the/netup/script
  #!/bin/bash
  ip addr add dev $5 $HOSTIP/$HOSTNETMASK peer $GUESTIP
auto eth0
iface eth0 inet manual
    pre-up ip link set dev eth0 up
    up ip addr add dev eth0 $GUESTIP/32 peer $HOSTIP/$HOSTNETMASK
    post-up ip route add default via $HOSTIP
    pre-down ip addr del dev eth0 $GUESTIP/32
    down ip link set dev eth0 down

ipv6

    echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
  ip addr add dev $5 you:r:routed::1/64
  ip addr add you:r:routed::2/64 dev eth0
  ip route add default via you:r:routed::1/64 dev eth0