Setting a static IP address in Ubuntu 16.04 vs 18.04 LTS – IT Infra Engineer

Setting a static IP address in Ubuntu 16.04 vs 18.04 LTS

Static IP addresses are an important part of networking for many types of servers, although a bit more overhead to configure they ensure a consistent state. The Ubuntu configuration of static addresses varies depending on the operating system version you are running, this guide will look at the 2 most recent versions with Long Term Support (LTS)

This guide shows you how to configure a static IP address on an Ubuntu server running 16.04 LTS or 18.04 LTS that is currently configured with DHCP.

Assuming your Ubuntu server is connected to the network on DHCP, to verify this run

ifconfig

This should return the interface along with the IP address and subnet issued by DHCP.

To find the gateway address run

ip route | grep default

Configuring a static IP using the network interfaces configuration file (16.04 LTS)

To set a static address, edit the following file,

sudo vi /etc/network/interfaces

If you are presented with the following below, please see the section below titled “Configuring a static IP using Netplan”

# 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

Change the file from the DHCP configuration which will look similar to below (Do not change the loopback settings)

# The primary network interface
auto ens160
iface ens160 inet dynamic

to the static configuration as per your requirements, i.e.

# The primary network interface
auto ens160
iface ens160 inet static
   address <ip address>
   netmask <subnet mask>
   gateway <gateway address>
   dns-nameservers <dns server 1> <dns server 2>

Exit vi with ESC the :wq! and enter

Reboot and reconnect on new IP to ensure changes have applied correctly.

Configuring a static IP using Netplan (18.04 LTS)

Starting from Ubuntu 18.04 LTS, Ubuntu uses Netplan to configure network interfaces by default. Netplan is a YAML based configuration system, which is designed to simplify the configuration process.

Check for the netplan yaml filename using

ls /etc/netplan/

Once you have the filename, edit this file using

sudo vi /etc/netplan/50-cloud-init.yaml

This should be set similar to the DHCP config as following;

Edit the file so it looks similar to the following;

# 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:
    version: 2
    ethernets:
        eth0:
            dhcp4: no
            addresses: [172.16.100.234/24]
            gateway4: 172.16.100.1
            nameservers:
                    addresses: [8.8.8.8,8.8.4.4]
            match:
                macaddress: 02:0e:0d:42:65:1c
            set-name: eth0

Exit vi with ESC the :wq! and enter

Apply the yaml configuration

 sudo netplan apply

Verify with ifconfig that the changes have come into effect and ensure you can ping a device outside of the local subnet