Ubuntu NetPlan
Ubuntu NetPlan configuration for IP address setup
Preview
Netplan is Ubuntu’s standard way to declare networking in YAML and hand it off to a backend (“renderer”) like systemd-networkd or NetworkManager. Config lives under /etc/netplan/*.yaml, and Netplan can safely trial changes with automatic rollback.
Identify your interface before writing YAML
When a machine is fresh (or YAML isn’t there yet), quickly discover the real device names:
# List interfaces (names, state)
ip -br link
# Include addresses (IPv4/IPv6)
ip -br addr
ip -br -4 addr
ip -br -6 addr
# Drill into a specific interface
ip addr show dev enp3s0
ip -br addr show dev enp3s0
-
-br/--briefshows tidy tables;-j/--jsongives machine-readable output. -
ip ais shorthand forip addr. You only needshow dev IFACEwhen narrowing to one device; for listing,showisn’t required.
Minimal recipes
Put one of these under
/etc/netplan/01-net.yaml, thensudo netplan try→ confirm →sudo netplan apply.
1) DHCP (server-style)
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
dhcp4: true
2) Static IPv4 + DNS + default route
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
addresses: [10.10.10.20/24]
nameservers:
search: [corp.local]
addresses: [10.10.10.253, 8.8.8.8]
routes:
- to: default
via: 10.10.10.1
3) Multiple gateways (primary/backup via metrics)
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
addresses: [10.0.0.10/24]
routes:
- to: default
via: 10.0.0.1
metric: 100
- to: default
via: 10.0.1.1
metric: 200
4) Bridge (commonly for KVM/containers)
network:
version: 2
renderer: networkd
ethernets:
enp3s0: { dhcp4: false }
bridges:
br0:
dhcp4: true
interfaces: [enp3s0]
5) Bond (active-backup or 802.3ad/LACP)
network:
version: 2
renderer: networkd
bonds:
bond0:
interfaces: [enp3s0, enp4s0]
dhcp4: true
parameters:
mode: active-backup # or: 802.3ad
6) VLAN (tagged subinterface)
network:
version: 2
renderer: networkd
ethernets:
mainif: { dhcp4: false }
vlans:
vlan15:
id: 15
link: mainif
addresses: [10.3.99.5/24]
7) Wi-Fi (desktop/laptop; use NetworkManager)
network:
version: 2
renderer: NetworkManager
wifis:
wlp2s0:
access-points:
"MY_SSID":
password: "********"
dhcp4: true
Safe-apply workflow (remote friendly)
- Inspect the merged config:
sudo netplan get
- Trial with auto-rollback:
sudo netplan try # default 120s countdown
- Commit:
sudo netplan apply
If you lose connectivity during step 2, Netplan restores the previous state automatically after the timeout—exactly what you want over SSH.
Troubleshooting checklist
- netplan status [IFACE] (add
--diffto compare YAML vs live). - Verbose apply:
sudo netplan --debug apply. - IP/Routes:
ip -br addr,ip route. - DNS:
resolvectl status. - Logs:
journalctl -u systemd-networkdorjournalctl -u NetworkManager. - Netplan itself recommends using
ip addrto see actual IP state.
About cloud images & generated YAML
On cloud images you’ll often find a generated file like /etc/netplan/50-cloud-init.yaml. Your platform (cloud-init, MAAS, etc.) may own networking until you switch it to Netplan-only or adjust its template. The key is knowing Netplan reads all YAML from /{lib,etc,run}/netplan and merges them lexicographically—later files amend/override earlier ones.
Appendix: iproute2 one-liners
# Interface names only
ip -o link show | awk -F': ' '{print $2}'
# IPv4 per NIC (comma-separated)
for i in $(ls /sys/class/net | grep -v lo); do
printf "%-12s %s\n" "$i" "$(ip -4 -o addr show dev $i | awk '{print $4}' | paste -sd, -)"
done
# JSON (good for scripts)
ip -j addr show | jq .
-
-br/--brieffor compact tables,-j/--jsonfor machine-readable, and object names can be abbreviated (ip addr→ip a).