Setting a static IP for a linux container (lxc) in Ubuntu 14.04
I am going to detail you how to set up a static IP address in the linux container you may have created for your system. This instructions work with Ubuntu 14.04, but will definitely work in many other version and distributions.
First let’s gather some information about the current state of the network, to get the assigned range of IP addresses, as well as the network mask. So we will SSH inside the container and run:
[email protected]:~# ifconfig eth0 Link encap:Ethernet HWaddr 00:16:3e:21:15:9e inet addr:10.0.3.116 Bcast:10.0.3.255 Mask:255.255.255.0 inet6 addr: fe80::216:3eff:fe21:159e/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:114 errors:0 dropped:0 overruns:0 frame:0 TX packets:110 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:18336 (18.3 KB) TX bytes:16136 (16.1 KB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 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) |
Looking at the first lines we discovered that the dynamic IP of our machine right now is 10.0.3.116 and the Network Mask is 255.255.255.0. This means that we can assign any IP address within the range 10.0.3.XXX
Now one more piece of information is missing. We need to find the gateway. To do so, we will run the following command
[email protected]:~# ip route ls default via 10.0.3.1 dev eth0 10.0.3.0/24 dev eth0 proto kernel scope link src 10.0.3.116 |
Here we find out on the first line that the gateway is 10.0.3.1.
Now we have all the information necessary to set up the configuration. To set up the static IP address, we will edit the file /etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 10.0.3.150
netmask 255.255.255.0
gateway 10.0.3.1
dns-nameserver 8.8.8.8
Since the device we are going to use to set the network is eth0, is it under it where we are going to set the configuration. We will define the address we want to give to the machine, in this case 10.0.3.150, even though it could be different as long as it is within the range, and the rest of the information. Note that the indentation is mandatory!
For the DNS nameserver, we have added 8.8.8.8 as it is Google’s DNS and it works usually nicely.
Al we have to do now is to restart our container, and the IP address should be set to the value we determined.
[email protected]:~# lxc-ls --fancy NAME STATE IPV4 IPV6 AUTOSTART ----------------------------------------------- server RUNNING 10.0.3.150 - NO |
Comments
Introduction to Linux Containers (lxc) | Enrique Moreno Tent
[…] Set a static IP address to your containers […]