# 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`, and `set` each 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. `ip` reads 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; use `ip` to 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

```bash
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'
```

```text
$ ip -br addr
lo               UNKNOWN        127.0.0.1/8 ::1/128
ens5             UP             10.0.1.20/24 fe80::8ff:fe12:3456/64
```

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:

```bash
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:

```bash
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:

/etc/netplan/60-static.yaml

```yaml
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]
```

```bash
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):

/run/systemd/network/10-netplan-ens5.network

```ini
[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:

```text
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:

/etc/netplan/60-nic.yaml

```yaml
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:

/run/systemd/network/10-netplan-ens5.link

```ini
[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`:

/etc/systemd/network/10-netplan-ens5.link.d/50-speed.conf

```ini
[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:

```bash
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'
```

```text
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:

/etc/netplan/01-network-manager-all.yaml

```yaml
network:
  version: 2
  renderer: NetworkManager
```

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:

/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

```yaml
network: {config: disabled}
```

## 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:

```bash
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:

```bash
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"
```

```bash
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.

```bash
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](<https://computecentral.in/foundations/networking/02-dns/index.md#resolution-on-linux>) explains the resolution path and how to debug it with `dig`.

## Hostname

```bash
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:

/etc/hosts

```text
127.0.0.1   localhost
127.0.1.1   web01.example.internal web01
```

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 `nmcli` as 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](<https://computecentral.in/foundations/linux/05-users-sudo-and-ssh-hardening/index.md#host-firewall>).

## 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](<https://computecentral.in/foundations/networking/05-network-troubleshooting-toolkit/index.md>).

## Common Mistakes

- Making a change with `ip` and losing it at the next reboot.
- Editing `/etc/resolv.conf` directly on a system where `systemd-resolved` or NetworkManager manages it.
- Running `netplan apply` over SSH instead of `netplan 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 modify` and forgetting `connection up`, so nothing changes until the next restart.
- Putting NIC speed or duplex in a second `.link` file that never applies, because Netplan's `10-netplan-*.link` sorts 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

> [!NOTE]
>
> **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`
> - [x] `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.

> [!NOTE]
>
> **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 `.link` file at boot
> - [x] systemd applies only the first matching `.link` file, and Netplan's `10-netplan-ens5.link` sorts 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 get` tell you that `ip route` doesn'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](<https://computecentral.in/foundations/networking/index.md>), or put these commands into scripts with [Shell Scripting](<https://computecentral.in/foundations/shell-scripting/index.md>).
