Linux Network Configuration¶
Changes made with ip take effect immediately and vanish at reboot. Persistent network configuration belongs to a network manager: Netplan on Ubuntu, NetworkManager on RHEL. Find out which one owns the interface before you change anything, or your change will be silently overwritten.
What You'll Learn¶
- How to inspect interfaces, addresses, routes, and link health with
ip - Which tool owns network configuration on Ubuntu, RHEL, and cloud images
- How to set a static address, routes, and DNS servers with Netplan and with
nmcli - What
netplan try,apply,generate,get, andseteach do, and what Netplan writes underneath - How to set NIC options such as wake-on-LAN, offloads, speed, and duplex persistently
- How to set the hostname so it survives reboots and cloud-init
- How to change the network over SSH without locking yourself out
Mental Model¶
The kernel holds the live network state: interfaces, addresses, and routes.
ipreads and edits that state directly. A network manager reads configuration files at boot and whenever you apply them, and writes that state for you. Change the files and apply them; useipto look, and to make temporary changes while testing.
| System | Configuration files | Applied by |
|---|---|---|
| Ubuntu Server 24.04 and 26.04 LTS | /etc/netplan/*.yaml |
Netplan, which generates config for systemd-networkd |
| Ubuntu Desktop | /etc/netplan/*.yaml, or set in the GUI |
NetworkManager, which since Ubuntu 23.10 saves its connections as Netplan YAML |
| RHEL 9, Rocky, AlmaLinux | /etc/NetworkManager/system-connections/*.nmconnection |
NetworkManager (nmcli, nmtui) |
| Cloud images | Generated by cloud-init on first boot | cloud-init, then the system's network manager |
Inspect the Network¶
ip -br link # interfaces and whether they're UP
ip -br addr # addresses per interface
ip route # routing table; "default via" is the gateway
ip route get 10.20.4.15 # the exact route and source address for one destination
ip -s link show ens5 # packet, error, and drop counters
ethtool ens5 | grep -E 'Speed|Duplex|Link detected'
Interface names describe where the device is attached: ens5, enp0s3, and eno1 are typical. Some clouds still use eth0. Use the name your system shows; never assume eth0.
Which network manager is active:
networkctl list # systemd-networkd: interfaces it manages
nmcli device status # NetworkManager: devices and their connections
ls /etc/netplan/ # Netplan files, if any
Temporary Changes With ip¶
Useful for testing. Everything here is lost at reboot or when the network manager reapplies its configuration:
sudo ip addr add 10.0.1.21/24 dev ens5 # add a second address
sudo ip route add 10.20.0.0/16 via 10.0.1.254 # add a route
sudo ip link set ens5 mtu 9001 # change the MTU
sudo ip addr del 10.0.1.21/24 dev ens5 # undo
Ubuntu: Netplan¶
A static address, a default gateway, an extra route, and DNS servers:
network:
version: 2
ethernets:
ens5:
dhcp4: false
addresses:
- 10.0.1.20/24
routes:
- to: default
via: 10.0.1.1
- to: 10.20.0.0/16
via: 10.0.1.254
nameservers:
addresses: [10.0.0.2, 10.0.0.3]
search: [example.internal]
sudo chmod 600 /etc/netplan/60-static.yaml # Netplan warns if others can read it
sudo netplan generate # validate the YAML without applying it
sudo netplan try # apply, and roll back after 120 s unless you confirm
sudo netplan apply # apply without the safety net
netplan status # what's applied, per interface
netplan try is the safe way to change networking over SSH. If the change cuts you off, you can't press Enter to confirm, and the old configuration comes back on its own after 120 seconds (change it with --timeout).
| Command | What it does |
|---|---|
netplan generate |
Converts the YAML into configuration for the renderer, without applying it. The quickest syntax check |
netplan try |
Applies the configuration and rolls it back unless you confirm in time |
netplan apply |
Applies the configuration to the running system, with no rollback |
netplan get [key] |
Prints the merged configuration from every file, or one key such as ethernets.ens5 |
netplan set key=value |
Writes a change into a YAML file for you, for example sudo netplan set ethernets.ens5.dhcp4=true (it writes /etc/netplan/70-netplan-set.yaml) |
netplan status |
Shows what is applied, per interface (needs systemd-networkd) |
Netplan doesn't configure interfaces itself. It writes files for a renderer, systemd-networkd by default, and the renderer does the work. These are the files netplan generate wrote for the example above on Ubuntu 26.04 (Netplan 1.2):
[Match]
Name=ens5
[Network]
LinkLocalAddressing=ipv6
Address=10.0.1.20/24
DNS=10.0.0.2
DNS=10.0.0.3
Domains=example.internal
[Route]
Destination=0.0.0.0/0
Gateway=10.0.1.1
[Route]
Destination=10.20.0.0/16
Gateway=10.0.1.254
Never edit files under /run/systemd/network/: Netplan rewrites them on every generate and at boot. They're useful for checking what your YAML turned into.
Two warnings you'll see from netplan generate, quoted from a real run:
WARNING **: Permissions for /etc/netplan/60-static.yaml are too open. Netplan configuration should NOT be accessible by others.
WARNING **: `gateway4` has been deprecated, use default routes instead.
The first goes away with chmod 600. For the second, replace gateway4: with a routes: entry with to: default, as in the example. Netplan also configures bonds, bridges, and VLANs (bonds:, bridges:, vlans:) in the same file.
NIC settings: wake-on-LAN, offloads, speed, and duplex¶
ethtool -s and ethtool -K change NIC settings until the next reboot. To make them stick on a Netplan system, split them in two:
Wake-on-LAN and offloads are Netplan keys. The offload keys work with the networkd renderer only:
network:
version: 2
ethernets:
ens5:
dhcp4: true
wakeonlan: true
generic-receive-offload: false
large-receive-offload: false
tcp-segmentation-offload: false
Netplan turns these into a systemd .link file. On Ubuntu 26.04, netplan generate wrote:
[Match]
OriginalName=ens5
[Link]
WakeOnLan=magic
TCPSegmentationOffload=false
GenericReceiveOffload=false
LargeReceiveOffload=false
Speed, duplex, and auto-negotiation have no Netplan keys. Set them in systemd's .link configuration (BitsPerSecond=, Duplex=, AutoNegotiation=). systemd applies only the first matching .link file, by file name, so a separate 20-ens5.link would be ignored once Netplan has written 10-netplan-ens5.link. Add a drop-in for Netplan's file instead; drop-ins merge into it from /etc:
[Link]
BitsPerSecond=1G
Duplex=full
AutoNegotiation=no
If Netplan writes no .link file for the interface (the static-address example earlier produced none), create your own with a name that sorts before 99-default.link, such as 10-ens5.link, with a [Match] section such as OriginalName=ens5. Check ls /run/systemd/network/ after netplan generate to see which files Netplan wrote.
Check which file and drop-ins apply, then confirm on the NIC:
udevadm test-builtin net_setup_link /sys/class/net/ens5 2>&1 | grep ID_NET_LINK_FILE
ethtool ens5 | grep -E 'Speed|Duplex|Auto-negotiation|Wake-on'
ethtool -k ens5 | grep -E 'generic-receive|large-receive|tcp-segmentation'
ID_NET_LINK_FILE=/run/systemd/network/10-netplan-ens5.link
ID_NET_LINK_FILE_DROPINS=/etc/systemd/network/10-netplan-ens5.link.d/50-speed.conf
.link settings are applied when the device appears, so they take effect at the next boot (or when the device is re-added). Forcing a fixed speed and duplex on one side only, while the switch port still auto-negotiates, causes a duplex mismatch: change both ends together.
Netplan and NetworkManager¶
The renderer key decides who configures an interface: networkd (the default) or NetworkManager. It can be set for the whole file, a device type, or one device:
Ubuntu Desktop ships the same setting in /usr/lib/netplan/00-network-manager-all.yaml, so NetworkManager owns every interface there. Since Ubuntu 23.10, NetworkManager also saves each connection you create with nmcli or the GUI back into Netplan, as /etc/netplan/90-NM-<connection UUID>.yaml. Change those connections with nmcli, not by editing the YAML.
There's no supported "disable Netplan" switch, and you rarely need one: without YAML files, Netplan does nothing. To let NetworkManager manage a server's interfaces, install network-manager and set renderer: NetworkManager. To stop cloud-init writing its own file, see the next section.
Stop cloud-init from overwriting it¶
On cloud images, cloud-init writes /etc/netplan/50-cloud-init.yaml at boot. If you manage networking yourself, tell cloud-init to stop:
RHEL Family: NetworkManager and nmcli¶
NetworkManager stores each configuration as a connection bound to a device. Find the connection name first; it's often not the interface name:
nmcli connection show
# NAME UUID TYPE DEVICE
# System eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 ethernet eth0
Set a static address in one command, then reactivate the connection:
sudo nmcli connection modify "System eth0" \
ipv4.method manual \
ipv4.addresses 10.0.1.20/24 \
ipv4.gateway 10.0.1.1 \
ipv4.dns "10.0.0.2 10.0.0.3" \
ipv4.dns-search example.internal
sudo nmcli connection up "System eth0"
sudo nmcli connection modify "System eth0" +ipv4.routes "10.20.0.0/16 10.0.1.254" # add a route
nmcli -f ipv4 connection show "System eth0" # review the settings
cat /etc/NetworkManager/system-connections/*.nmconnection # the stored file (needs root)
sudo nmtui # a text menu, if you prefer
RHEL 9 stores connections as keyfiles in /etc/NetworkManager/system-connections/. The older /etc/sysconfig/network-scripts/ifcfg-* files are deprecated; convert any you still have with nmcli connection migrate.
Run modify and up together on one command line when working over SSH. If the new address is wrong, the session drops either way, but the change finishes instead of stopping halfway. Have console access ready before changing the interface you're connected through.
Netplan vs nmcli¶
They aren't competitors: Netplan is a configuration generator, and NetworkManager (driven by nmcli) is a network daemon. Which one you use depends on who manages the interface:
| Netplan | nmcli (NetworkManager) |
|
|---|---|---|
| What it is | YAML files rendered into config for systemd-networkd or NetworkManager |
Command-line client for the NetworkManager daemon |
| Where it's the default | Ubuntu Server and cloud images | RHEL, Rocky, AlmaLinux, Fedora, and Ubuntu Desktop |
| Where config lives | /etc/netplan/*.yaml |
/etc/NetworkManager/system-connections/*.nmconnection (on Ubuntu 23.10+, Netplan YAML) |
| Apply a change | netplan try or netplan apply |
nmcli connection up <name> |
| Safety net over SSH | netplan try rolls back automatically |
None built in; keep console access |
| Good at | Declarative files you can template and review | Laptops, Wi-Fi, VPNs, and changing things interactively |
On a Netplan server, don't also make changes with nmcli unless the interface uses renderer: NetworkManager. Otherwise two tools claim the same interface, and the one that wins at the next boot isn't always the one you tested.
DNS Resolution¶
Set DNS servers in the network configuration, as above, not in /etc/resolv.conf. On Ubuntu that file points to the local systemd-resolved stub (127.0.0.53) and is managed for you; edits are overwritten.
resolvectl status # upstream servers per interface (Ubuntu)
cat /etc/resolv.conf # what applications read
getent hosts db.example.internal # resolve exactly as applications do, including /etc/hosts
DNS explains the resolution path and how to debug it with dig.
Hostname¶
sudo hostnamectl set-hostname web01.example.internal
hostnamectl # static hostname, OS, kernel, virtualization
Add the name to /etc/hosts so the machine can always resolve itself, even without DNS:
On cloud instances, cloud-init may reset the hostname at boot. Set preserve_hostname: true in a file under /etc/cloud/cloud.cfg.d/ to keep yours.
Cloud Instances¶
On AWS, Azure, and Google Cloud, the platform assigns addresses and serves them over DHCP. Leave the primary interface on DHCP:
- To add a private address, assign a secondary IP to the network interface through the cloud API first. Some images configure it automatically; others need it added to Netplan or
nmclias well. - A static address that doesn't match what the platform assigned stops traffic: the cloud network drops packets from addresses it didn't assign.
- Firewalls are usually security groups or network rules outside the instance; check them as well as the host firewall described in Users, sudo, and SSH Hardening.
Troubleshooting¶
| Symptom | Check | Likely cause |
|---|---|---|
Interface DOWN or NO-CARRIER |
ip -br link, ethtool ens5 |
Cable, virtual NIC detached, or interface disabled in config |
| No IPv4 address | journalctl -u systemd-networkd or journalctl -u NetworkManager |
DHCP failed, or a static config has a typo |
| Local network works, internet doesn't | ip route, ip route get 1.1.1.1 |
Missing or wrong default route |
| IP works, names don't | resolvectl status, getent hosts |
No DNS servers configured, or wrong search domain |
| Connectivity drops in and out | ip -s link (rising errors), sudo arping -D -I ens5 10.0.1.20 |
A bad link, or another host using the same address |
| Config "doesn't stick" after reboot | ls /etc/netplan/, cloud-init logs |
Change made with ip only, or cloud-init regenerated the file |
For problems beyond the host itself, such as routing, firewalls, ports, and TLS, use the Network Troubleshooting Toolkit.
Common Mistakes¶
- Making a change with
ipand losing it at the next reboot. - Editing
/etc/resolv.confdirectly on a system wheresystemd-resolvedor NetworkManager manages it. - Running
netplan applyover SSH instead ofnetplan try. - Assigning a static IP to a cloud instance that doesn't match the address the platform gave it.
- Editing the file cloud-init generates, then losing the change at the next boot.
- Running
nmcli connection modifyand forgettingconnection up, so nothing changes until the next restart. - Putting NIC speed or duplex in a second
.linkfile that never applies, because Netplan's10-netplan-*.linksorts first. Use a drop-in for Netplan's file. - Editing the generated files under
/run/systemd/network/instead of the YAML they come from.
Check Yourself¶
Changing the IP over SSH
You're connected over SSH and need to change the server's address. Which command protects you if the new configuration cuts you off?
-
sudo netplan apply -
sudo netplan try -
sudo netplan generate
try applies the change and reverts it after 120 seconds unless you confirm. If you've lost the session, you can't confirm, so the old configuration comes back. generate only checks and renders the YAML.
Speed that never sticks
Netplan sets wakeonlan: true on ens5. You add /etc/systemd/network/20-ens5.link with BitsPerSecond=1G, reboot, and the speed doesn't change. Why?
- Netplan overwrites every
.linkfile at boot - systemd applies only the first matching
.linkfile, and Netplan's10-netplan-ens5.linksorts before yours -
BitsPerSecond=only works with NetworkManager
Put the setting in a drop-in for Netplan's file, /etc/systemd/network/10-netplan-ens5.link.d/50-speed.conf. udevadm test-builtin net_setup_link shows which file and drop-ins apply.
Interview Questions¶
- How do you find which tool manages networking on a Linux server?
- How would you set a static IP on Ubuntu 24.04 over SSH without risking a lockout?
- Why might a network change disappear after a reboot?
- Where should DNS servers be configured on a modern Ubuntu system, and why not in
/etc/resolv.conf? - What does
ip route gettell you thatip routedoesn't? - How would you persistently set a NIC's speed and disable an offload on an Ubuntu server managed by Netplan?
- What's the difference between Netplan and NetworkManager, and when would you use
renderer: NetworkManager?
Next¶
You've finished Linux. Continue to Networking, or put these commands into scripts with Shell Scripting.