Skip to content

Configure static IP address on Ubuntu 16.04 LTS Server

Ubuntu 16.04 has been out for just over a month now, and I’m in the process of upgrading some boxen. In some cases I’ve been completely reinstalling them for that clean fresh feel, and find myself once again having to configure static IP addresses.

Doing this can be problematic (made worse by the fact that even the official Ubuntu LTS documentation doesn’t give correct advice), particularly when it comes to DNS resolution, and in my case, a very-old-school habit of blindly wanting to edit the /etc/resolv.conf file which you really should not be doing any more these days.

Step 1. Edit the /network/interfaces file

sudo nano /etc/network/interfaces

sudo nano /etc/network/interfacesThe interfaces file is where you need to change from the default “dhcp” setting to add some information about the “static” IP address that you want to configure.

In this case, look for the line in the file that says “# The primary network interface” and directly beneath this you’ll see something like (the default DHCP configuration):

auto ens160
iface ens160 inet dhcp

If you’ve been using previous versions of Ubuntu you might have noticed that the interface name above “ens160” looks a bit odd. It used to be called “eth0” or “eth1”, but as from Ubuntu 15.10 the interface name is now allocated based on a few other factors – read more about that here if you’re interested.

Simply comment, remove or edit the line that says ends with “dhcp” and add the following information (here’s an example only):

iface <interface> inet static
	address 192.168.0.100
	netmask 255.255.255.0
	gateway 192.168.0.1

Next you’ll want to add the nameservers by adding the line “dns-nameservers” followed by a list of IP addresses.  A lot of people use Google’s public DNS, or if you have details from your service provide use them instead.

dns-nameservers 8.8.8.8 8.8.4.4

PRO-TIP: Double-check the file again, and ensure all details are correct, before proceeding.  If you are configuring a remote system you could easily cut yourself off if you get it wrong.

Save the file, and you’re done.

Step 2. Restart the networking service (or reboot)

Once you are confident the change has been made, and if you don’t want to reboot you can just restart the networking service.

sudo /etc/init.d/networking restart

After doing this, and provided you don’t get any errors, your primary network interface should now be configured with the static IP address details you have provided.

Failing that, a reboot will also do the trick quite nicely, and with the current version of 16.04 I suspect there’s a bug that is causing “ifconfig -a” not to update unless you perform a reset.

sudo reboot

Good luck, and may your boxen be correctly addressed.

32 thoughts on “Configure static IP address on Ubuntu 16.04 LTS Server Leave a comment

  1. yo lo uso asi

    iface em1 inet static
    address 192.168.1.10
    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.255
    gateway 192.168.1.1
    dns-nameservers 200.72.1.5

    • Hello!

      Excuse me in 16.4 I under/etc/network/interfaces configured in the static IP, I again using ifconfig IP display is another IP.In interfaces configured to complete is invalid, what reason is this

  2. Thanks for the g8 article . Surprisingly I don’t have
    iface ens160 inet dhcp
    inside this path etc/network/interfaces. I have 16.04 ubuntu.

    • Thank you 🙂

      The “ens160” reference for the Ethernet interface can vary from system to system, so just substitute with whatever you have.

  3. It’s the oddest thing. If I edit the network adapters config file using a the CLI or SSH it does not work and I lose all network connectivity, the interface actually disappears. If I install a GUI and Sudo edit the file (open terminal in the GUI, type ‘sudo nautilus’ then browse to the file’s location and edit) it works. I scrapped 4 VMs before finding this “recipe.” Weird or what?

    • After following this “helpful guide” my adapter has disappeared as well. Made all my changes via the command line as well.

      Should not need gui tools to edit this file! What the hell did the Ubuntu Team do? Good Grief!!!

      • OK Googled the fix: Don’t delete the “auto enpwtf” line above the iface line or your network adapter will disappear!

  4. I tried to restart networking by doing but the old DHCP address persisted. Rebooting gave me the new static address however.
    Is “service” deprecated? If so, invoking it silently does nothing – no diagnostic.

    • Yes “service” isn’t the right thing to use anymore, you could also try this:

      sudo systemctl restart NetworkManager.service

  5. I followed these steps but still gets host unreachable anytime I ping using the configured IP address to see my ubuntu server can be reached. Please help

  6. For those who have experimented problems with Ubuntu 16.04 LTS (networking service restart didn’t work):

    ip addr flush ens160 && ifdown ens160 && ifup ens160

    That did the trick for me 😉

  7. Hi, In my /etc/network/interfaces, I didn’t see any ethernet-like interface. Look:

    root@amr:/etc/network# ifconfig
    enp0s31f6 Link encap:Ethernet HWaddr 70:4d:7b:88:e6:7c
    inet addr:192.168.2.150 Bcast:192.168.2.255 Mask:255.255.255.0
    inet6 addr: fe80::fab5:dec9:7bfd:af07/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:24031 errors:0 dropped:0 overruns:0 frame:0
    TX packets:22581 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:32890636 (32.8 MB) TX bytes:1825913 (1.8 MB)
    Interrupt:16 Memory:f7300000-f7320000

    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:494 errors:0 dropped:0 overruns:0 frame:0
    TX packets:494 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:38600 (38.6 KB) TX bytes:38600 (38.6 KB)

    root@amr:/etc/network#
    root@amr:/etc/network#
    root@amr:/etc/network# cat interfaces
    # interfaces(5) file used by ifup(8) and ifdown(8)
    auto lo
    iface lo inet loopback
    root@amr:/etc/network#

    Can you help to tell me how to configure it to be static ip? Thanks!

  8. thanks Michael McKinnon,
    i want to run my Ubuntu 16.04 from two IP address
    192.168.1.104
    and
    192.168.1.105

    please give me any suggestion.

    • Hi there, sorry for the delay in responding.

      To achieve this you can simply repeat the “iface inet static” section for the second address you wish to add in the /etc/network/interfaces file.

      Alternatively if you just wanted to add a second IP address (known as an alias) temporarily while the system is running it’s also very easy to do something like “sudo ifconfig eth0:1 192.168.1.105” – this will create a new “alias” interface called eth0:1 (the :1 addon indicates this) – and to turn it off “sudo ifconfig eth0:1 down”.

      If you’re not sure what interfaces the system has use “ifconfig -a” for a full list, or “ifconfig -s” for a short list.

      Cheers,

      Michael

      • Thanks Michael,

        the “sudo ifconfig enp2s2:1 192.168.1.121” is working fine for me

        but the below code in /etc/network/interfaces is not working. i am using Ubuntu 16.04

        auto lo enp2s2
        iface lo inet loopback
        iface enp2s2 inet static
        address 192.168.1.102
        netmask 255.255.255.0
        gateway 192.168.1.255

        iface enp2s2:1 inet static
        address 192.168.1.121
        netmask 255.255.255.0
        gateway 192.168.1.255

        please give me any suggestion.

      • At the top of your interfaces file modify the “auto lo enp2s2” line to read “auto lo enp2s2 enp2s2:1” – this tells the network configuration which interfaces to bring up at boot time.

  9. On freshly installed 16.04.5 I lost a good hour before realizing I had to rem out _both_ of the first 2 lines. I don’t know if I accidentally deleted the 2nd “#” or if it was never there. This issue appears to only interfere with static IP assignment, not DHCP.

  10. brother i put primary network interface ip and am not able to ping and open in web console first i was not able to login with root user then i tried sudo su and make it super user i want to load zabbix

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.