Configure Ubuntu Server 18.04 to use a static IP address

Since Ubuntu 17.10 Artful, Netplan has been introduced as the new network configuration utility. This tutorial will show you how to change the IP address from DHCP to static using Netplan.

Introduction

There will be a file placed in the /etc/netplan folder that’s used to configure the network. You might come across two different filenames depending on what installation media you’ve used. These are:

Ubuntu Server 18.04 server ISO

/etc/netplan/01-netcfg.yaml

Ubuntu Server 18.04 cloud image

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

As you can see by the name above, the cloud image uses cloud-init to configure the network, so we can’t just edit this file because changes might be overwritten. We’ll have to disable network configuration by cloud-init.

How to disable network configuration by cloud-init

Create a new file called:

sudo vim /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

Add the following to the file:

network: {config: disabled}

Rename the netplan config file to the same as the one in the server ISO.

sudo mv /etc/netplan/50-cloud-init.yaml /etc/netplan/01-netcfg.yaml

How to change the IP address from DHCP to static on Ubuntu Server 18.04

Open the netplan config file:

sudo vim /etc/netplan/01-netcfg.yaml

You should see a file that looks similar to this:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: true

It’s the ethernets section of the file we want to change below the name of your ethernet adapter. On my system it’s enp0s3. First we want to change dhcp4 to false and then add the static IP config below this. Here’s an example below:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: false
      addresses: [192.168.1.11/24]
      gateway4: 192.168.1.1
      nameservers: 
        addresses: [1.1.1.1,8.8.8.8]

Now apply the config with the following comand:

sudo netplan apply