Static IP Banner

How to set a Static IP in Ubuntu – The Proper Way!

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.

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!

The quick answer to setting up a static IP in Ubuntu the proper way is:

  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:

gksudo gedit /etc/NetworkManager/NetworkManager.conf

Your file should look something like this:

[main]
plugins=ifupdown,keyfile
dns=dnsmasq

[ifupdown]
managed=false

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….

[main]
plugins=ifupdown,keyfile
#dns=dnsmasq

[ifupdown]
managed=true

 

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:

ifconfig -a

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

ifconfig
eth0      Link encap:Ethernet  HWaddr 11:26:5d:97:t5:e9  
          UP BROADCAST MULTICAST  MTU:1500  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:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:43 Base address:0xc000 

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:16436  Metric:1
          RX packets:4390 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4390 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:448014 (448.0 KB)  TX bytes:448014 (448.0 KB)

wlan0     Link encap:Ethernet  HWaddr e9:f2:44:e7:b6:a1  
          inet addr:192.168.0.10  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fa80::fed5:22fe:fec6:a6b9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:92540 errors:0 dropped:0 overruns:0 frame:0
          TX packets:60313 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:107589287 (107.5 MB)  TX bytes:9187835 (9.1 MB)

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:

ip route show

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.

default via 192.168.0.1 dev wlan0  proto static 
169.254.0.0/16 dev wlan0  scope link  metric 1000 
192.168.0.0/24 dev wlan0  proto kernel  scope link  src 192.168.0.10  metric 2

 

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)

ifconfig <iface> | grep Mask

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:

auto lo
iface lo inet loopback

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:

gksudo gedit /etc/network/interfaces

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:

auto lo
iface lo inet loopback

iface eth0 inet static
address 192.168.0.101
gateway 192.168.0.1
netmask 255.255.255.0
dns-nameservers 8.8.8.8 8.8.4.4

 

Restart your networking services

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

Type/copy the following:

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

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

  • http://www.sudo-juice.com/ Gareth Parmar

    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/

  • http://www.sudo-juice.com/ Gareth Parmar

    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…

  • Paul

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

  • Nithesh

    perfect!!!