Adding static ip address in Ubuntu

Saurabh Mhatre
CodeClassifiers
Published in
2 min readFeb 12, 2017
image source:Pixabay

In today’s tutorial we are going to configure static ip in ubuntu. The steps are as follows:

Install vim editor if have not previously:

sudo apt-get install vim

Now open interfaces file in networks using the following command:

sudo vim /etc/network/interfaces

Add the following code:

uto lo eth0
iface lo inet loopback
iface eth0 inet static
address 172.17.0.2
netmask 255.255.0.0
gateway 172.17.0.1
dns-nameservers 8.8.8.8

The address,netmask and gateway value will change according to each os. The address and netmask has to be added from parameters in ifconfig command:

root@HOMEPC:# ifconfig
eth0 Link encap:Ethernet HWaddr a4:17:31:9c:11:4d
inet addr:172.17.0.2 Bcast:192.168.1.255 Mask:255.255.0.0
inet6 addr: fe80::e49a:8a31:3c56:f9d0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:292071 errors:0 dropped:0 overruns:0 frame:0
TX packets:179742 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:386351683 (386.3 MB) TX bytes:21029080 (21.0 MB)

Add inet addr as address and Mask value as netmask in interfaces file.

For gateway we need to run the following command:

root@HOMEPC:/var/lib/docker# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.17.0.1 0.0.0.0 UG 600 0 0 eth0

Copy the gateway value from above to interfaces file.

Once you are done with editing save and close the interfaces file.

Next run the following commands to start using static ip address:

ifdown eth0
ifup eth0

or

sudo service networking restart

Thats all for today’s tutorial.

Bonus tip:

If you are trying to assign static ip to a docker container make sure you add following tag in the run command:

docker run --cap-add=NET_ADMIN

Connect deeper:

You can find my previous article here and like our facebook page to get notified about similar posts in the future here: Technoetics

--

--