How to set a Static IP in Ubuntu – Ubuntu Network Confirguration

Setting a static IP in Ubuntu is useful for a lot of things. You may want to forward ports from your router to serve web pages, use SSH from outside your local network or set up some sort of media server.

These instructions have been tested and confirmed with Ubuntu 16.04 LTS and Ubuntu 14.04 LTS but should be fine on other versions. 

For whatever reason, Ubuntu is trying to make our lives easier by introducing graphical tools to be used by default but kind of making our terminal life a bit harder!

How to set a static IP in Ubuntu (the quick answer):
  1. Disable the graphical management of your network connection in /etc/NetworkManager/NetworkManager.conf
  2. Gather the information for the static IP (interface, IP to be used, default gateway, subnet, DNS)
  3. Modify /etc/network/interfaces to include the information above.
  4. Restart networking and network-manager services

These instructions assume you are using a wired ethernet port to set a static IP on and already have an internet connection to create a static IP for (otherwise you would be finding it hard to read this right now!).

 

Disabling visual (nm-tool) network management

Using Ubuntu’s graphical interface to set a static IP (as described in my previous article about setting a static IP using nm-tool), uses some underlying tools that disable the manual / terminal method which most Linux users are more adapted to.

To regain control of our network interfaces we first have to disable nm-tools’ management of them. Type/copy the following command:

Your file should look something like this:

A few things to do:

  1. Disable dnsmasq by commenting out the line with a # (hash) at the beginning of the line.
  2. Set managed (under the ifupdown section) to true.

Dnsmasq offers local DNS caching and network DHCP naming, both of which aren’t necessary on a standard workstation (Some may argue that local DNS caching is useful but I’ve always found Google’s DNS servers to be fast enough). More information on dnsmasq is available from Ubuntu Documentation [https://help.ubuntu.com/community/Dnsmasq].

I find the terminology for ‘managed’ a little confusing here because I have to set this to true to get Network Manager to recognise that I am actually managing this connection manually!

So our NetworkManager.conf now looks like this….

 

Which connection do I use for my static IP?

So, lets have a look at our current network information, open a terminal and type/copy the following command:

You will now be presented with all the available network connections (whether connected or not). Your output should look something like this:

What are the ifconfig connections?

To elaborate on the connections here:

  • lo – this is a loopback connection, a virtual connection used to connect to the machine (itself) only
  • eth0 – an ethernet interface (wired port)
  • wlan0 – a wireless interface (device)

You may well have more eth interfaces, especially in servers where the machine may need to be connected to the network and other machines. You may well have more wlan interfaces if for some reason you have multiple wireless devices connected to your machine.

Setting a static IP for a wireless interface is a bit different to the instructions contained here (to be covered in the future).

I will be using eth0.

 

What static IP should I use?

There are some IP ranges reserved for private use. These are:

  • 10.0.0.0 – 10.255.255.255
  • 172.16.0.0 – 172.31.255.255
  • 192.168.0.0 – 192.168.255.255

The exact reason for these ranges is explained *more clearly* on wikipedia.

My personal preference is to use IP’s between 192.168.0.100 – 192.168.0.200. Steer clear of using an IP at the beginning and end of any of the ranges as they are usually used for network administration devices.

So, the IP I’m going to use is 192.168.0.101.

 

What is my Network Gateway?

Your Network Gateway is the gateway between your network and the Internet, the connection between 2 networks – your network and the worlds!

In most residential broadband set-ups, this will be the router provided by your Internet supplier. In commercial scenario’s, this could be a firewall or other routing device.

To find your current gateway type/copy this command:

The output will show a line containing the word ‘default’, the IP on this line will be the IP of your Network Gateway… sometimes used interchangeably with Default Gateway (sometimes incorrectly).

My default/network gateway is 192.168.0.1 as shown below on the first line.

 

What is my Netmask / Subnet?

A subnet is used to divide a network into logical groups – subnetworks. A Netmask specifies the boundaries of a subnetwork. The exact methods in which it does this are outside the scope of this of this post (maybe one for the future!).

To find your subnet, use ifconfig again. Type/copy the following: (REPLACING <iface> with the interface currently connected to the internet eg. wlan0 or eth0)

Your subnet is the value after Mask, in 99% of residential router’s this value will be 255.255.255.0.

My subnet is 255.255.255.0.

 

What about DNS?

DNS (Domain Name Servers) are like indexes which resolve the domain name to an IP. I primarily use Google’s Public DNS but you may choose to use your gateway IP to let your router deal with DNS.

The name servers I will be using are 8.8.8.8 and 8.8.4.4.

Setting the static IP

Staic IPTo specify a static connection we need to add the information we gathered above to /etc/network/interfaces.

My /etc/network/interfaces file (on every distro I’ve tried so far) was pretty much empty by default (as below), if you have something more you should be careful with removing entries, start by commenting out lines (using #) to see exactly what you are using.

My current /etc/network/interfaces file looks like this:

What do I change in /etc/network/interfaces?

There are 5 items to add to this file:

  • Static declaration (using <interface> chosen above): iface <interface> inet static
  • Static IP address: address <chosen ip>
  • Network gateway: gateway <gateway ip>
  • Netmask / subnet: netmask <netmask ip>
  • DNS nameservers: dns-nameservers <dns ip’s>

Open your /etc/network/interfaces file for editing by typing/copying the following:

Adding my chosen network interface, static IP, default gateway IP, subnet and nameservers (separated by a space), my /etc/network/interfaces file now looks like this:

 

Restart your networking services

Finally, restart your networking services to put your new settings into effect.

Restarting networking for Ubuntu 15.04 and higher

Since Ubuntu 15.04, the init daemon system was fully changed to systemd, this includes the latest LTS release, Ubuntu 16.04. Though the previous upstart commands are still usable in many cases you should start to use the newer syntax:

Restarting networking for Ubuntu 14.10 and lower

Restart the following services with the previous syntax typical of upstart:

This should restart your network interfaces and services. Use ifconfig to confirm your new static IP!

 

  • Pingback: How to Set a Static IP in Ubuntu – the Semi-Graphical Way! | Sudo JuiceSudo Juice()

  • Thanks for the instructions, but they will only work if applied straight to an Ubuntu server and not on a desktop Ubuntu because the Network Manager will ignore anything in the interfaces file unless specified.

    I found the the most comprehensive instructions for setting a static IP and configuring Network Manager to read your settings here…. http://www.sudo-juice.com/how-to-set-a-static-ip-in-ubuntu-the-proper-way/

  • Thanks for the instructions, but they will only work if applied straight to an Ubuntu server and not on a desktop Ubuntu because the Network Manager will ignore anything in the interfaces file unless specified.

    I found the the most comprehensive instructions for setting a static IP and configuring Network Manager to read your settings here

  • GTechQuest

    this is so HELPFULL!!! Thankyou….will this stop resolv.conf from reseting everytime i restart?, i am learning to configure BIND9 and this is annoying….this site is Awesome!!!

  • AyThink

    Hi,
    thank you for the tutorial,
    it is the perfect and complete guide to set static Ip on Ubuntu,
    but like every time,
    after flowing all instruction :
    1- i can communicate between local devices connected to the LAN
    2- the Ubuntu machine show me
    “connect : Network is unreachable”

    Thank you

  • tastydisqus

    I thought I’d chime in regarding these steps on the Ubuntu Server distro, which does not include a GUI by default. Network manager is therefor not installed and can be ignored.

  • vinz

    Thank you for the guide.
    However it doesn’t seem to work for me unless I add

    auto eth0
    before
    iface eth0 inet static

    Does that make any sense?

    • Johan D.

      Sure. Else you have to connect manually. That’s no problem, just a click but still…

    • This has been added to the guide. Thanks

  • Paul

    Thank you for the guide. The details on how to get the values were extremely helpful.

  • Nithesh

    perfect!!!

  • Jeremy Stewart

    Well written guide. Thanks

  • tdurden99

    Hi, great guide. Still i have I question. When i set static ip addr. my apache https server doesn’t work? Do you have any clue why is this a issue? thanks, best regards.

    • This could happen for a number of reasons. I would check:
      1. the IP/alias you are connecting on
      2. your virtualhost settings
      3. the alias your ssl certificates are set up for

      It could be a lot of things but I’m going to guess it’s number 3!

      Good luck!

  • James Van Zandt

    Besides adding “auto eth0”, I’d suggest one other revision. On a recent Ubuntu, the command

    sudo service networking restart

    fails with this unhelpful message:

    stop: Job failed while stopping
    start: Job is already running: networking

    A more informative message is written to /var/log/upstart/networking.log:

    Stopping or restarting the networking job is not supported.
    Use ifdown & ifup to reconfigure desired interface.

    So it’s better to finish with these commands

    ifdown eth0
    ifup eth0

    or else just reboot.

    • findaway

      If I follow the above step it says:

      stop: Env must be KEY=VALUE pairs
      start: Env must be KEY=VALUE pairs

  • Phrenks

    Thank you, very helpful.

    However, this is probably the best single example I’ve seen of why it’s hard to get users to start using linux. This is a very long tutorial to accomplish a very simple task, which can be done quickly and painlessly in Windows (I can’t believe I’m saying anything positive about that horrible, terrible OS, but there it is)

    • Viktor Nova

      Uh, I would hardly call the process of setting a static IP in Windows “quick and painless”, much less intuitive or obvious. How many weird things do you have to click on to get to that screen again? And make sure you don’t check “Validate settings on exit” before clicking OK, or Windows might decide it knows better than you about what your IP should be.

      Doing this in just about any Linux distro through whatever GUI is easy enough to not even need a guide, this tutorial is specifically about doing it in the terminal, for those people who want or need to. Is that even possible in Windows? I honestly don’t know

  • Viktor Nova

    Thanks, it’s been a few years since I’ve set up an Ubuntu server, and it indeed has changed! Very helpful, this makes way more sense than the old way

  • Rose Krueger

    Thanks for the info – Oh I’m doing this via my Macbook Pro…like a lot of us. – Thanks info is great!

  • justin underwood

    Thank you Sudo Juice. Great name by the way.

    Have been struggling with a solution for this for a few days.

    This is the only solution I’ve found that mentions editing the

    /etc/NetworkManager/NetworkManager.conf

    and after editing ‘etc/network/interfaces’ file to restart these services –

    sudo service network-manager restart
    sudo service networking restart
    sudo service resolvconf restart

    I think that is what was missing in other articles.

    Many thanks for your help.

  • Kevin Jones

    Just thought I would add that I needed to add

    auto eth0

    to my network config. Then it worked. Thanks for the guide!

  • ashay

    can i access the web page using static ip on some distant network meaning a pc not connected to my router

  • Zach Dahl

    I followed this guide and am now unable to ssh into my server. What do I need to do to remedy this?

    • At what step did it log you out? Is this a remote server or one you have physical access to. If it is remote and managed by a provider, you may be able to get their support to remedy it for you.

  • ScottRobinett

    very helpful, thanks

  • Toby

    you shouldn’t need auto eth0 providing you’s added the broadcast parameter which is shown at the top of the page in the graphic but missed in the /etc/network/interfaces configuration

  • Funktapus

    Using “auto lo eth0” instead of “auto lo” worked for me. Hope it helps somebody.

  • eyewall

    I’ve spent the past couple of hours trying to figure out why I lost internet connection when I set a static IP address, and this is exactly what I needed. All I needed to do was disable the visual nm-tool as you described. Thanks!

  • Tobias

    2016 and still very helpful. ?

    Just wanted to add that I had to add the computer IP and name to /etc/hosts to resolve an error message.

  • Sergio Webo

    Thank you, my head was about to explode trying to figure out why was the interface file bringing up problems such as: RTNETLINK answers file exists

    You solved the problem and explain it very clear. Thanks

  • Jon Eliot Strømme

    Nice guide! One important detail has changed since 2014: The Network Manager people have changed the semantics of “managed”, so now it should be “false” in order for Network Manager to leave it alone.

  • Bear

    Thank you, very helpful, now I can find my server

  • jOps

    Under Ubuntu 16.04 there’s also a /etc/network/interfaces.d/eth0 file you want to take care of. I put all the eth0 related stuff into there and commented everything else in there.

    Perhaps this article could be updated fo newer Ubuntu versions.

  • mouniir oouli

    thank U .
    great and prperly great . it works properly .

  • amateurcoder

    useful tutorial, but didn’t quite set it up. can you post an updated instruction set for current version ubuntu 16.04 ? Thanks!

  • f

    When entering the Static IP, Can I do a range for Static? Let’s say 1-10 range.

  • qu1j0t3

    Did not work for me on Ubuntu 18.04 bionic. After restarting Network Manager and networking services, still retains the DHCP address rather than the static one. (Also the interface is enp3s0 rather than eth0.)