How to setup a static IP on Ubuntu Server 18.04

All the answers telling you to directly edit /etc/netplan/50-cloud-init.yaml are wrong since CloudInit is used and will generate that file. In Ubuntu 18.04.2 it is clearly written inside the file :

$ cat /etc/netplan/50-cloud-init.yaml
# 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:
        eno1:
            dhcp4: true
    version: 2

So you should not edit that file but the one under /etc/cloud/cloud.cfg.d/ if you still want to use CloudInit.

Another way is to completely disable CloudInit first by creating an empty file /etc/cloud/cloud-init.disabled (see https://cloudinit.readthedocs.io/en/latest/topics/boot.html) and then the other answers are OK. Under Ubuntu 18.04.2 I had to use dpkg-reconfigure cloud-init to let it take into account the file /etc/cloud/cloud-init.disabled. I think this is a little bit weird.

I suggest you to rename the file (not the right name since 50-cloud-init.yaml let us think it still uses CloudInit).

Then you may end up with a file name /etc/netplan/01-netcfg.yaml which contains the configuration below. Note the use of the networkd renderer instead of NetworkManager because the configuration is on a server.

network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      dhcp4: no
      addresses: [192.168.1.246/24]
      gateway4: 192.168.1.1
      nameservers:
         addresses: [192.168.1.1]

This is set a static IP instruction in Ubuntu-Server 18.04 and 20.04

$ sudo nano /etc/netplan/50-cloud-init.yaml

Then replace your configuration, for example, the following lines:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens160:  # Your ethernet name.
     dhcp4: no
     addresses: [192.168.1.137/24]
     gateway4: 192.168.1.1
     nameservers:
       addresses: [8.8.8.8,8.8.4.4]

Apply changes:

$ sudo netplan apply

In case you run into some issues execute:

$ sudo netplan --debug apply

[NOTE]:

  • /24 is equivalent with 255.255.255.0
  • ens160 is your ethernet name, you can get it using $ ifconfig
  • Ubuntu 16.04 and 14.04 network-interface configuration have a different method.
  • The file is in YAML format: Use spaces, no tabs.