The Linux Page

Setting up your static IP address in Ubuntu 18.04

Spider Web representing various Internet and Intranet network of users.

Older Versions (Ubuntu 17.10-)

With older versions I would just go to /etc/network and edit the interfaces file.

This file would have definitions about the computer interfaces, whether each computer gets a static IP or uses DHCP, etc.

Here is a sample setup for older versions:

auto eth0
iface eth0 inet static
        address 10.0.0.1
        netmask 255.0.0.0
        network 10.0.0.0
        broadcast 10.255.255.255
        gateway 10.0.0.1
        dns-nameservers 10.0.0.8 10.0.0.9
        dns-search m2osw.com
        pre-up /etc/network/firewall

The pre-up is a script run before that specific interface gets initialized. In my case, I want to start my firewall (good idea if the interface is for external access: your firewall should be up before the world has a chance to access your computer.)

The DNS name servers is a space separated list of IP addresses used to access the DNS to be used to determine the IP address from a name,

Newer Versions (Ubuntu 18.04+)

The settings themselves have not changed in Ubuntu 18.04, however, the location where it gets setup is completely different. Also the format changed from a plain text Unix like configuration file to a Yaml format declarative setup.

The configuration is also in a new location and uses a file with a completely different name. You will find the file under /etc/plan and it is named 50-cloud-init.yaml.

As we can see, the extension of the file is .yaml. That means it supports any number of sub-options defined by adding more tabs and a different introducer.

First, notice that they mention the netplan replacement and location of the new files. It's not clear which you have to update to make things work, though.

$ cat /etc/network/interfaces
# ifupdown has been replaced by netplan(5) on this system.  See
# /etc/netplan for current configuration.
# To re-enable ifupdown on this system, you can run:
#    sudo apt install ifupdown

Now, looking at the /etc/netplan files, by default we see not much is added by default:

$ find /etc/netplan/ -exec ls -ld {} \;
drwxr-xr-x 2 root root 4096 Apr  5 21:03 /etc/netplan/
-rw-r--r-- 1 root root  589 Apr  5 21:03 /etc/netplan/50-cloud-init.yaml

Just one file! When you first look at it, you'll notice that the default is that the computer will automatically be given an IP address through DHCP.

The following is what I use when switching to my local static IP address:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        enp0s3:
            addresses: [10.0.0.15/8]
            dhcp4: false
            dhcp6: false
            optional: true
            gateway4: 10.0.0.1
            nameservers:
                addresses: [75.75.75.75,75.75.76.76]
    version: 2

The gate is generally one your own computer in an intrset setup.

Two things that make that definition select a static IP address:

1. The dhcp[46]: false;

2. the addresses: ... definition.

Apply Modifications to /etc/netplan/*

Whenever you make changes to files under /etc/netplan/... it is required let the netplan know otherwise nothing is going to happen until you reboot.

sudo netplan apply

It should be pretty quick and return to the prompt. After that, you can verify that your changes took hold as expected with:

ifconfig

What about the WARNING in that yaml file?

Yes. You noticed a warning and instructions on how to block the configuration from being blown away. If you're not going to use the graphical interface, you're probably safe just like that. On a computer with X-Windows running, I would advice the properly disable the cloud-init as follow:

# Open file
$ sudo vim /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

# Add this line (i for insert and then type the following)
network: {config: disabled}

# Then save and exit (:wq)

The file is not likely to already exist. vim will create it as expected just by editing the content of the file.

Where can I find the other fields?

You should already know that since it shows up in the old interfaces file...

For additional information, use the manual with the following:

man 5 netplan

For example you could specify the size of the MTU (not really recommanded, although on a local network, having a larger MTU could help with large transfers.) There are also options to setup routes. Usually the default is enough, but on a rather specific network, adding a few routes could be useful and it can be done at this point.