Since Ubuntu 18.04, network configuration lives in Netplan YAML files under /etc/netplan/. Netplan translates them into settings for the actual backend — systemd-networkd on servers, NetworkManager on desktops. Here’s how to set a static IP, add addresses and DNS, and apply changes without cutting off your own SSH session.

Find your interface and current settings

ip -br addr
ip route
ls /etc/netplan/

Typical output on a VPS:

lo      UNKNOWN  127.0.0.1/8 ::1/128
eth0    UP       203.0.113.10/24 fe80::…/64

Interface names vary: eth0, ens3, enp1s0. Note your interface, address, prefix length (/24) and default gateway (default via … in ip route).

A static IP configuration

Edit (or create) a file such as /etc/netplan/01-netcfg.yaml. YAML is indentation-sensitive — use spaces, never tabs:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      addresses:
        - 203.0.113.10/24
      routes:
        - to: default
          via: 203.0.113.1
      nameservers:
        addresses: [1.1.1.1, 8.8.8.8]

Notes:

  • routes: - to: default replaces the older gateway4: key, which is deprecated and prints warnings on current releases.
  • Netplan files must not be world-readable: sudo chmod 600 /etc/netplan/*.yaml.

Apply safely with netplan try

Changing the network over SSH is risky: one mistake and the connection drops. netplan try applies the config and automatically rolls back after 120 seconds unless you confirm:

sudo netplan generate     # syntax check
sudo netplan try

If your SSH session still responds, press Enter to keep the configuration. If it froze, wait two minutes and the old settings return. Then make it permanent:

sudo netplan apply

Cloud-init may overwrite your changes

On many VPS and cloud images, cloud-init generates /etc/netplan/50-cloud-init.yaml and can regenerate it on boot. If your edits disappear after a reboot, disable cloud-init’s network management:

echo "network: {config: disabled}" | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

Then put your configuration in your own file and remove or rename the cloud-init one.

Files are processed in lexical order, and later files override earlier ones for the same keys, so 99-custom.yaml wins over 50-cloud-init.yaml.

Multiple IP addresses

Additional IPs on the same interface are just more entries:

      addresses:
        - 203.0.113.10/24
        - 203.0.113.11/24
        - 198.51.100.20/32

For an extra IP from a different subnet (common for additional IPs on a VPS), use the prefix your provider specifies — often /32, sometimes with a separate route. See adding a secondary IP on Linux and our Multi-IP VPS page.

IPv6

      addresses:
        - 203.0.113.10/24
        - "2001:db8:1::10/64"
      routes:
        - to: default
          via: 203.0.113.1
        - to: default
          via: "2001:db8:1::1"

Quote IPv6 addresses to keep YAML happy. More in configuring IPv6 on Linux.

DNS settings

The nameservers block configures systemd-resolved. Check what’s in effect:

resolvectl status

/etc/resolv.conf points to 127.0.0.53, the local resolved stub — that’s normal. Don’t edit /etc/resolv.conf directly; change Netplan instead. See changing the DNS server on Linux.

Back to DHCP

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: true

Match by MAC address

If interface names might change (for example after hardware changes on a dedicated server), match by MAC address:

  ethernets:
    uplink:
      match:
        macaddress: "52:54:00:12:34:56"
      set-name: eth0
      addresses: [203.0.113.10/24]

Troubleshooting

Error in network definition: expected mapping or similar — indentation. Each nesting level is two spaces; list items start with - under their key.

Permissions for /etc/netplan/… are too open — sudo chmod 600 /etc/netplan/*.yaml.

gateway4 has been deprecated — replace gateway4: with a routes: entry as shown above.

No connectivity after apply — use your VPS provider’s VNC console, then check ip addr, ip route and sudo journalctl -u systemd-networkd -n 50. Most often the gateway or prefix length is wrong.

Changes revert after reboot — cloud-init; see above.

Useful commands

sudo netplan get                   # show merged configuration
sudo netplan status                # current state (newer Netplan versions)
networkctl status eth0             # networkd view of the interface
ip -br addr && ip route

On Debian, which uses /etc/network/interfaces instead, see Debian networking configuration. Our Ubuntu VPS images come preconfigured with the correct IP, so you only need Netplan when adding addresses or changing DNS.

  • Ubuntu
  • Networking
  • Netplan