ubuntu, updated

Change IP, gateway and routes

Copy-paste Netplan examples for static IP, DHCP, gateway, extra IPs, static routes and VLANs.

The safe workflow

sudo cp /etc/netplan/50-cloud-init.yaml ~/netplan-backup.yaml   # 1. backup
sudoedit /etc/netplan/50-cloud-init.yaml                        # 2. edit
sudo chmod 600 /etc/netplan/*.yaml                              # 3. permissions (else a warning)
sudo netplan try                                                # 4. test, Enter to keep
ip -br a && ip r                                                # 5. check

Use netplan try over SSH: if you lock yourself out, the old config comes back after 120 seconds.

Warning: YAML uses spaces, never tabs, and the indentation must be exact. sudo netplan generate points to the line if something is wrong.

Static IP with gateway and DNS

network:
  version: 2
  ethernets:
    ens18:
      dhcp4: false
      addresses:
        - 192.168.10.20/24          # IP + prefix (/24 = 255.255.255.0)
      routes:
        - to: default
          via: 192.168.10.1         # gateway
      nameservers:
        addresses: [192.168.10.1, 9.9.9.9]
        search: [corp.example.com]

The old gateway4: key is deprecated. Use routes: with to: default.

Back to DHCP

network:
  version: 2
  ethernets:
    ens18:
      dhcp4: true

Common changes

A second IP on the same interface

      addresses:
        - 192.168.10.20/24
        - 192.168.10.21/24

A static route to another network

      routes:
        - to: default
          via: 192.168.10.1
        - to: 10.50.0.0/16
          via: 192.168.10.254

IPv6 static

      addresses:
        - 192.168.10.20/24
        - 2001:db8:10::20/64
      routes:
        - to: default
          via: 192.168.10.1
        - to: "::/0"
          via: 2001:db8:10::1

A VLAN on top of an interface

network:
  version: 2
  ethernets:
    ens18: {}
  vlans:
    vlan20:
      id: 20
      link: ens18
      addresses: [10.20.0.5/24]

Temporary changes (gone after reboot)

Useful for quick tests. netplan apply or a reboot resets them.

WhatCommand
Add an IPsudo ip addr add 192.168.10.30/24 dev ens18
Remove an IPsudo ip addr del 192.168.10.30/24 dev ens18
Change the default gatewaysudo ip route replace default via 192.168.10.1
Add a routesudo ip route add 10.50.0.0/16 via 192.168.10.254
Interface down / upsudo ip link set ens18 down / up

Hostname

sudo hostnamectl set-hostname srv-web01

Also update the 127.0.1.1 line in /etc/hosts to the new name, otherwise sudo complains that it can’t resolve the host.

§ home
~/knubertus/home/
1,1 Top