Linux Basics - Set A Static IP On Ubuntu

This tutorial explains how to set a static IP on an Ubuntu system from the command line. It covers the network configuration for all recent Ubuntu versions and includes instructions to configure a static IP address, set the hostname and configure name resolving.


Step 1: Configure the network interface

In this step, you will manually configure your network interface by editing the following files using your preferred text editor(nano gedit vi). For the purpose of this example, I'm using the "nano" editor. You can edit the appropriate file by entering the following command into the terminal:

You can copy and paste directly from this line.

sudo nano /etc/network/interfaces

Enter your root password, once your preferred editor opens the file you can see...

auto lo eth0
iface lo inet loopback
iface eth0 inet dynamic

If you have more than one network card, please be sure to make changes to the correct network interface.

Statically configured network cards will have a section like:

auto lo eth0
iface lo inet loopback
iface eth0 inet static
        address your ip here)
        gateway gateway ip here,usually the address of the router)

If you use "nano" editor, type Ctrl+x to save changes.

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?

<---Type "y"

File Name to Write: interfaces


Here is an example:

auto lo eth0
iface lo inet loopback
iface eth0 inet static

 And here the complete network configuration file from an Ubuntu 16.04 system.

Ubuntu network configuration file

Step 2: Configure the DNS servers

Changes in /etc/resolv.conf are required only on Systems with Ubuntu < 14.04, for newer Ubuntu versions the nameservers get configured in the /etc/network/interfaces file.

a) Ubuntu versions < 14.04

In this step, you will manually configure your dns configuration file.

sudo nano /etc/resolv.conf

Once your editor opens the file you want to enter the following information...

nameserver your dns server ip)
nameserver your alt dns server ip)

If you use "nano" editor, type Ctrl+x to save changes.

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?

<---Type "y"

File Name to Write: resolv.conf


Here is an example:


b) Ubuntu versions 14.04 and newer

Open the /etc/network/interfaces file again and add a line dns-nameservers right after the gateway line.

sudo nano /etc/network/interfaces

auto lo eth0
iface lo inet loopback
iface eth0 inet static
        address your ip here)
        gateway gateway ip here,usually the address of the router)

The nameservers and are provided by Google for public use, so you can use them in your network configuration.

If you use "nano" editor, type Ctrl+x to save changes.

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?

<---Type "y"

File Name to Write: interfaces


Step 3: Restart networking

Manually restart your network interface with the new settings.

For Ubuntu < 14.04 use the networking init script:

sudo /etc/init.d/networking restart

This should return a result that looks like the following:

*Reconfiguring network interfaces… [OK]

For Ubuntu versions 14.04 and newer use systemctl instead:

systemctl restart [email protected]

At this point you can check if the settings are correct:


If everything is correct you will get this result.

eth0      Link encap:Ethernet  direcciónHW 00:33:27:46:2v:34
          Direc. inet:  Difus.:  Másc:

Step 4: Configure the Hostname

The hostname of an Ubuntu Server or Desktop is being configured in the files /etc/hostname and /etc/hosts. The /etc/hostname file sets the actual system hostname while /etc/hosts is used for the local name resolution.

In this example, I will change the hostname of my system to

First, edit the /etc/hostname file

sudo nano /etc/hostname

The hostname file contains the local part of the hostname only. The local part here is "obelix". Change the content of the /etc/hostname file to:


and save the file. The hostname file as it looks in nano after editing:

Edit the hostname on Ubuntu

Then open the /etc/hosts file with an editor:

sudo nano /etc/hosts

and change the line that starts with the IP address of the system like this:     obelix

Here a screenshot of the hosts file.

Configure the hosts file

The format is like this:

[IP Addesss] [full hostname incl. domain] [local part of the hostname]

Finally, restart the system to apply the hostname change.

See you...

Share this page:

Suggested articles

22 Comment(s)

Add comment


From: Pilgrim at: 2012-04-16 10:16:51

This command is deprecated on new debian and ubuntu:

/etc/init.d/networking restart

solution is a use command:

ifdown eth0 && ifup eth0

See #565187 netbase: Command deprecated and not explained

From: Anonymous at: 2013-12-10 04:52:06

Bless you.  I saw that error and was noticing I had to completely restart to get the command to take.  I was not liking that and once I scrolled down to see if someone saw the same, I found your post.  Thank you so much!

From: Katz at: 2012-04-16 15:18:18

What´s the ubuntu version, 11.10 or 12.04?

From: Freaki at: 2012-09-15 02:22:27

I dunno about you but I don't like using the resolv.conf, I prefer to set the the nameserver to read my routers, and typically I'll use google's or

 Heres a small guide I found if anyone is interested.

From: Ranting at: 2012-10-09 20:06:21

I used to use the command line method before Ubuntu 12.04 but now prefer the network manager method of setting a static IP.

This is only because after using this method then opening the connection manager to check what was showing (I know I could have checked on cli but dont like to be limited!) all the settings were reset to dhcp and my modifications were reset!

I didn't really fancy removing the network manager. Anybody else feel like they are being forced to use graphical methods more and more with newer Ubuntu releases?

From: Ranting at: 2013-11-26 22:46:29

These instructions are slightly dated now and a lot of other instructions I'm seeing fail to address Network Manager managing the connection.
This article about setting a static IP in Ubuntu the proper way was very useful for me.
It's useful to note that the instructions on many sites and askubuntu answers do not apply to wireless devices!



From: Anonymous at: 2013-04-28 14:34:35

Hmm any idea why you don't just do everything within the /etc/network/interfaces configuration folder like here:

 Or does it make more sense to actually split things up like you did within the resolv.conf and interfaces configuration folder?

From: brubakes at: 2013-10-19 14:27:37

Just what I needed.  Thanks!

From: Chad at: 2015-08-13 14:23:27

Hey this is not working on 14.04 Server Edition.


auto eth0iface eth0 inet static        address        netmask        gateway        # dns-* options are implemented by the resolvconf package, if installed        dns-nameservers








eth0      Link encap:Ethernet  HWaddr 00:25:90:3a:c2:36            inet addr:  Bcast:  Mask:          inet6 addr: fe80::225:90ff:fe3a:c236/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:406 errors:0 dropped:174 overruns:0 frame:0          TX packets:182 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:40001 (40.0 KB)  TX bytes:28133 (28.1 KB)          Interrupt:16 Memory:fb5e0000-fb600000

From: till at: 2015-08-13 14:26:00

It is working fine fro me on Ubuntu 14.04 server. Your ifconfig output shows that the IP settings got applied correctly. If you have problems with your IP setup, then please post in the forum to get help.

From: shabeer ahmad at: 2015-09-30 07:20:51

i have followed the above procedure for setting up interfaces of my ubuntu 14.04 for my eth0 interface not showiing ip address after giving ifconfig command

From: TempleClause at: 2016-02-06 16:39:53

You should specify what to do for version 14.04... 

For me it's unclear what to do since < 14.04 and > 14.04 does not match 14.04 in either case....

From: anonymus at: 2016-04-13 21:14:39

Thanks very very match my friend!! This issues it's verry helpfulyy.

From: Mukund at: 2016-04-27 06:00:41


Can you guide me to set the static IP in Ubuntu 16.04

From: till at: 2016-04-27 06:03:02

The instructions above are valid for Ubuntu 16.04 as well.

From: ilayaraja at: 2016-04-29 04:41:09

I changed my IP in my ubuntu by using this method.


From: sanwarul hoq at: 2016-06-02 13:47:28

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?... any kind of talk will ....

From: Anonymous at: 2016-09-12 19:29:33

I did exactly as instructed, and did not get the desired result, in fact, nothing changed!

From: JonhDevNet at: 2016-09-16 21:52:14

I also tried in version 16.04 but did not work.

I will keep trying.

From: Lucas at: 2016-10-17 19:26:26

Before I ask, I would just like to let you know this is a great and detailed article and it taught and helped me so much. I am running Ubuntu 16.04, and followed all the steps. But I cannot find "eth0      Link encap:Ethernet  direcciónHW 00:33:27:46:2v:34

          Direc. inet:  Difus.:  Másc: ..."

The closest thing I can find is "eth0      Link encap:Ethernet  HWaddr 00:23:5a:18:18:ab  

          inet addr:  Bcast:  Mask:

          UP BROADCAST MULTICAST  MTU:1500  Metric:1


Also, I could not access the internet until I disconnected from the Ethernet network! Is this normal?

From: Tom S at: 2016-12-23 16:31:08


From: Ratboy at: 2017-03-04 19:16:50

hi, i tried this to but now my internet does't work anymore. if i do ifconfig i dont get that anymore  eth0      Link encap:Ethernet  direcciónHW 00:33:27:46:2v:34

          Direc. inet:  Difus.:  Másc:

and befor i did get 

lo link encap (and a lot more)now i only get that.

can it be that my internet doesn't use eth0 but ens160?