Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Ubuntu 18.04 Server - Set Static IP With Netplan

Netplan is the new networking system that is taking over from editing the /etc/networking/interfaces file. The most common thing you will want to do is set a static IP. You can do that by simply editing the /etc/netplan/01-netcfg.yaml file.

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

The file will look like below:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens2:
      dhcp4: yes

Here is an example configuration to set a static IP of 192.168.1.100 with your router/gateway being at 192.168.1.1 and wanting to use OpenDNS dns servers:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens2:
        addresses:
            - 192.168.1.100/24
        gateway4: 192.168.1.1
        nameservers:
            addresses:
                - 208.67.222.222
                - 208.67.220.220

The CIDR needs to reflect your network's subnet's netmask (useful lookup table). It is not the CIDR for IPs you allocate to the server (e.g. don't use /32)

Before I could paste, I had to fix an "issue" with vim first with the following commands:

echo "set mouse=" > .vimrc
sudo echo "set mouse=" | sudo tee -a /root/.vimrc Ubuntu Blog - Ubuntu Bionic: Netplan

Once you have successfully edited your configuration file, you can apply it with:

sudo netplan apply

Now your server should be using the new IP.

Extra Info

Netplan is actually pretty powerful and you can split your configuration up across multiple files in multiple locations as well as configuring bridges/bonding etc. I recommend reading these two posts for more information: