Post thumbnail

How to set a static IP on Ubuntu Server 20.04

networkdns

Previous versions of Ubuntu Server used a file under /etc/network to configure static IP addresses, but this has been changed in later versions of Ubuntu. When installing Ubuntu Server, a tool called NetPlan will probably configure your address with DHCP during the cloud-init step. To set a static IP we will need to keep using NetPlan to change from using DHCP, fortunately it is still easy to do.

First we need to find out the name of the ethernet device we want to set a static IP for, we can list them by typing ip add. This is what it could look like after I've stripped some of the lines for readability.

1: lo: <LOOPBACK,UP,LOWER_UP>
    [...]
2: ens18: <BROADCAST,MULTICAST,UP,LOWER_UP>
    link/ether 82:84:27:bc:81:e2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.193/24
    [...]

Here we only have two interfaces, and the first one is the loopback which we can ignore. That means the one we want to set a static IP address for is ens18. If you do have more interfaces you need to find out which one is the correct one. You can check the MAC address or inet if that is the currently used IP address.

Next step is to add the configuration to the file /etc/netplan/01-netcfg.yaml with sudo privileges. If it doesn't already exist you can create it. Change the values to match your needs and make sure the indentation is correct since this is a yaml file.

network:
  version: 2
  renderer: networkd
  ethernets:
    ens18: # change to your interface
      dhcp4: no
      addresses: [192.168.1.175/24] # The static IP for this device
      gateway4: 192.168.1.1 # The IP of your router
      nameservers: # Comma separated DNS server(s)
        addresses: [192.168.1.128,9.9.9.9]
      dhcp6: no

If you're not running your own DNS server (which I recommend you do if you are tech savvy enough reading this) you can enter your router's, ISP's or even better 9.9.9.9.

In case you are wondering, the /24 at the end of the address is called "CIDR notation" and is specifying what subnet we are using. Unless you know what you want you can keep it at /24. It means the addresses in this subnet range from 192.168.1.0 to 192.168.1.255.

When you are done with the configuration you can apply the settings with netplan apply and you should be good to go.