Assign Static Internal IP Addresses to LXC

LXC assigns random internal IP addresses to Containers by default. But it can be hard to refer to them that way. A good practice is to set static IPs.

Imagine your LXC’s name is my-lxc. We have to open the network interfaces file and make some modifications to it.

You can do this from inside the LXC by attaching (lxc-attach -n my-lxc) to it. But I prefer to do this from the host while the LXC is turned off (lxc-stop -n my-lxc).

To make the necessary modifications, issue the following command.

nano /var/lib/lxc/my-lxc/rootfs/etc/network/interfaces

It’ll open a file that looks like this:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

You need to make it look like this (Changed parts are marked in red.):

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
 address 10.0.3.2
 netmask 255.255.255.0
 gateway 10.0.3.1
 dns-nameserver 8.8.8.8

Note that you will have to change the “address” 10.0.3.2 into an unused IP address within that range. 10.0.3.1 is always assigned to the host and you can use from 10.0.3.2 to 10.0.3.255 (If you find yourself needing more than 254 LXCs, may be LXC is not the solution you’re looking for.).

Leave a Reply