# Persistent network configuration

> Give a Linux host a name, an address, a gateway and DNS that survive a reboot, with NetworkManager, ifupdown and systemd-networkd.

Source: https://aminbytes.dev/bytes/networking-fundamentals/network-configuration/


Typing `ip addr add` gets a machine online, but only until the next reboot. A server needs its address, gateway, DNS servers and name written down somewhere so they come back every time it boots. That is **persistent** configuration, and every distribution does it a little differently.

The good news: underneath, every method sets the same four things. Once you know those, the tools are just different ways of saying it. This byte covers the files and tools the exam lists, with NetworkManager as the main tool. Changing settings on the fly, without saving them, is the job of [basic network troubleshooting](../network-troubleshooting/).

## What a host needs

To talk to the world, a Linux host needs:

| Setting | Example | Where it ends up |
| ------- | ------- | ---------------- |
| IP address and prefix | `192.168.1.50/24` | on the interface |
| Default gateway | `192.168.1.1` | in the routing table |
| DNS servers | `1.1.1.1`, `9.9.9.9` | `/etc/resolv.conf` |
| Hostname | `web01` | `/etc/hostname` |

You can set these **statically** (typed by you) or get them from a **DHCP** server, which hands out address, gateway and DNS automatically. Laptops and desktops use DHCP. Servers usually get a static address so others can find them.

First, find your interfaces. Modern Linux gives them **predictable names** based on where the hardware sits: `enp0s3` (Ethernet, PCI bus 0, slot 3), `ens33`, `wlp2s0` (Wi-Fi). Older systems and many containers use `eth0` and `wlan0`.

```bash
ip -br link
```

```text
lo        UNKNOWN  00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
enp0s3    UP       08:00:27:4e:66:a1 <BROADCAST,MULTICAST,UP,LOWER_UP>
wlp2s0    DOWN     3c:a9:f4:12:7b:90 <NO-CARRIER,BROADCAST,MULTICAST,UP>
```

## The hostname

The machine's name lives in **`/etc/hostname`**: one line, nothing else.

```bash
cat /etc/hostname
```

```text
web01
```

On any systemd-based distribution, use **`hostnamectl`** to view and change it. It updates the running system and the file in one go:

```bash
hostnamectl
```

```text
 Static hostname: web01
       Icon name: computer-vm
         Chassis: vm
      Machine ID: 4f6c2b8e0d1a4c7e9b3f5a2d8c6e1b07
         Boot ID: a91d3e5c7b2f4a6e8d0c1b3a5f7e9d2c
  Virtualization: kvm
Operating System: Debian GNU/Linux 12 (bookworm)
          Kernel: Linux 6.1.0-28-amd64
    Architecture: x86-64
```

```bash
sudo hostnamectl set-hostname web02
```

systemd knows three kinds of hostname:

- **static**: the one in `/etc/hostname`, used at boot.
- **transient**: the name the kernel has right now, which DHCP may change.
- **pretty**: a free-form name for humans, like `Amin's laptop`, set with `hostnamectl set-hostname --pretty "Amin's laptop"`.

The old `hostname` command (more on it in the troubleshooting byte) also changes the name, but **only until reboot**, because it doesn't touch `/etc/hostname`.

> [!TIP]
> After renaming, update `/etc/hosts` too. Debian and Ubuntu map the hostname to `127.0.1.1` there, and `sudo` complains "unable to resolve host" when the two don't match.

## /etc/hosts, /etc/resolv.conf and /etc/nsswitch.conf

Three small files decide how names become addresses. They get a full byte of their own in [client side DNS](../client-side-dns/), so here is just what you need for host setup.

**`/etc/hosts`** is a static list of names. Each line is an address, the official name, then optional aliases:

```text {title="/etc/hosts"}
127.0.0.1       localhost
127.0.1.1       web02.example.lan web02
192.168.1.10    db01.example.lan  db01
::1             localhost ip6-localhost ip6-loopback
```

**`/etc/resolv.conf`** says which DNS servers to ask:

```text {title="/etc/resolv.conf"}
# Generated by NetworkManager
search example.lan
nameserver 1.1.1.1
nameserver 9.9.9.9
```

**`/etc/nsswitch.conf`** sets the order: check the hosts file first, then DNS.

```text {title="/etc/nsswitch.conf (hosts line)"}
hosts:          files dns
```

> [!IMPORTANT]
> On most modern systems `/etc/resolv.conf` is written by a program (NetworkManager, systemd-resolved, netplan or `resolvconf`). Edit it by hand and your change disappears at the next reconnect. Set DNS servers in that program's configuration instead. The first line of the file usually tells you who owns it.

## NetworkManager and nmcli

**NetworkManager** is the standard network service on RHEL, Fedora, Ubuntu Desktop and most desktop distributions. Debian uses it when you install a desktop. It handles Ethernet, Wi-Fi, VPNs, bridges and more, and it reconnects on its own when a cable or access point comes back.

Two ideas are key:

- A **device** is a network interface: `enp0s3`, `wlp2s0`.
- A **connection** (or profile) is a saved set of settings: address, DNS, Wi-Fi password. A device uses one active connection at a time. You can keep several, like "office" and "home", for the same device.

You control it with **`nmcli`** on the command line, **`nmtui`** (a text menu in the terminal) or the settings panel of your desktop. `nmcli` accepts abbreviations: `nmcli c` is `nmcli connection`, `nmcli d` is `nmcli device`.

### Look around

```bash
nmcli general status
nmcli device status
```

```text
STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN
connected  full          enabled  enabled  missing  enabled

DEVICE   TYPE      STATE                   CONNECTION
enp0s3   ethernet  connected               Wired connection 1
wlp2s0   wifi      disconnected            --
lo       loopback  connected (externally)  lo
```

```bash
nmcli connection show
```

```text
NAME                UUID                                  TYPE      DEVICE
Wired connection 1  9b5e3c1a-7f2d-4e8b-a6c0-1d4f2e8b7a93  ethernet  enp0s3
lo                  2c7e1f4a-8d3b-4a9e-b5c6-0e2f7d1a4b85  loopback  lo
```

`nmcli connection show "Wired connection 1"` prints every setting of one profile, and `nmcli device show enp0s3` shows what the device has right now (addresses, gateway, DNS).

### A static Ethernet connection

Create a new profile called `office` with a fixed address:

```bash
sudo nmcli connection add type ethernet con-name office ifname enp0s3 \
  ipv4.method manual \
  ipv4.addresses 192.168.1.50/24 \
  ipv4.gateway 192.168.1.1 \
  ipv4.dns "1.1.1.1 9.9.9.9" \
  ipv4.dns-search example.lan
sudo nmcli connection up office
```

```text
Connection 'office' (5e1d7c3b-2a4f-4b9e-8c6d-3f0a1e7b9d24) successfully added.
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
```

To change an existing profile, use `modify`, then bring it up again so the change takes effect:

```bash
sudo nmcli connection modify office ipv4.addresses 192.168.1.60/24
sudo nmcli connection modify office +ipv4.dns 8.8.8.8
sudo nmcli connection up office
```

A `+` adds a value to a list setting, a `-` removes one. To switch the profile back to DHCP:

```bash
sudo nmcli connection modify office ipv4.method auto ipv4.addresses "" ipv4.gateway ""
sudo nmcli connection up office
```

Other everyday commands:

| Command | What it does |
| ------- | ------------ |
| `nmcli connection down office` | deactivate a profile |
| `nmcli connection delete office` | remove a profile |
| `nmcli connection reload` | re-read profile files you edited by hand |
| `nmcli device disconnect enp0s3` | take a device offline (and stop auto-connect) |
| `nmcli device reapply enp0s3` | apply changed settings without a full down and up |
| `nmcli networking off` / `on` | turn all networking off and on |

### Wi-Fi

```bash
nmcli radio wifi on
nmcli device wifi list
```

```text
IN-USE  BSSID              SSID      MODE   CHAN  RATE        SIGNAL  BARS  SECURITY
        A4:2B:B0:11:22:33  HomeNet   Infra  36    540 Mbit/s  82      ▂▄▆█  WPA2 WPA3
        C8:3A:35:44:55:66  Neighbor  Infra  6     130 Mbit/s  41      ▂▄__  WPA2
```

Connect, and NetworkManager creates and saves a profile named after the network:

```bash
sudo nmcli device wifi connect HomeNet password 'correct horse battery'
```

```text
Device 'wlp2s0' successfully activated with '0d3a7e2c-5b1f-4c8d-9e6a-2f4b7c1d8e30'.
```

Add `hidden yes` for a network that doesn't broadcast its name. `nmcli device wifi rescan` refreshes the list, and `nmcli radio wifi off` turns the radio off (handy on a plane).

### Where the profiles live

| Distribution | Profile storage |
| ------------ | --------------- |
| RHEL 9, Fedora, Debian | keyfiles in `/etc/NetworkManager/system-connections/*.nmconnection` (readable by root only, they can hold passwords) |
| RHEL 8 and older | `ifcfg-*` files in `/etc/sysconfig/network-scripts/` (still read by RHEL 9, but deprecated) |
| Ubuntu 23.10 and newer | netplan YAML in `/etc/netplan/`, which NetworkManager writes for you |

You can edit a keyfile by hand, then run `nmcli connection reload` and bring it up again.

 (Side note: `nmcli` also has a handy terse mode for scripts: `nmcli -t -f DEVICE,STATE device` prints `enp0s3:connected`, one device per line, with no table padding.)

## ifupdown: /etc/network/interfaces, ifup and ifdown

Debian servers installed without a desktop use the classic **ifupdown** system. Its configuration is **`/etc/network/interfaces`**:

```text {title="/etc/network/interfaces"}
auto lo
iface lo inet loopback

# Static address, brought up at boot
auto enp0s3
iface enp0s3 inet static
    address 192.168.1.50/24
    gateway 192.168.1.1

# DHCP, brought up when the device appears
allow-hotplug enp0s8
iface enp0s8 inet dhcp
```

- `auto` brings the interface up at boot (and with `ifup -a`).
- `allow-hotplug` brings it up when the kernel detects the device.
- `inet` is IPv4, `inet6` is IPv6. The method is `static`, `dhcp`, `loopback` or `manual`.
- DNS servers are set in `/etc/resolv.conf`, or with a `dns-nameservers` line if the `resolvconf` package is installed.

After editing, apply the change by taking the interface down and up:

```bash
sudo ifdown enp0s3 && sudo ifup enp0s3
```

`ifup` and `ifdown` read only this file. They don't know about NetworkManager profiles, and NetworkManager leaves interfaces listed here alone by default.

On the Red Hat family, `ifup` and `ifdown` were scripts from the old `network-scripts` package, which reads the `ifcfg-*` files. That package is gone in RHEL 9. Where the commands still exist there, they hand the work to NetworkManager, so prefer `nmcli connection up` and `down`.

> [!CAUTION]
> Running `ifdown` on the interface your SSH session uses can cut you off. If your shell dies before `ifup` runs, the server stays offline. Use a console, or run the pair inside `tmux` or `screen` so it finishes even if you drop.

## systemd-networkd

**systemd-networkd** is systemd's own lightweight network service. You find it on servers, in containers and cloud images, and behind netplan on Ubuntu Server. It has no GUI and no Wi-Fi password manager. It reads plain `.network` files:

```ini {title="/etc/systemd/network/10-wired.network"}
[Match]
Name=enp0s3

[Network]
Address=192.168.1.50/24
Gateway=192.168.1.1
DNS=1.1.1.1
```

For DHCP, replace the address lines with `DHCP=yes` (or `ipv4`, `ipv6`). Files are read from `/usr/lib/systemd/network/`, `/run/systemd/network/` and `/etc/systemd/network/`, and a file in `/etc` overrides one with the same name in the other two.

```bash
sudo systemctl enable --now systemd-networkd
networkctl list
```

```text
IDX LINK   TYPE     OPERATIONAL SETUP
  1 lo     loopback carrier     unmanaged
  2 enp0s3 ether    routable    configured

2 links listed.
```

Ubuntu Server usually writes these files for you from **netplan**. You edit a YAML file in `/etc/netplan/`, then run `sudo netplan apply` (or `sudo netplan try`, which rolls back if you lose the connection):

```yaml {title="/etc/netplan/01-netcfg.yaml"}
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      addresses: [192.168.1.50/24]
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [1.1.1.1, 9.9.9.9]
```

Run only **one** network manager per interface. NetworkManager, ifupdown and systemd-networkd fighting over the same device is a classic cause of "my settings keep changing".

## Which tool where

| System | Usual tool | Persistent config |
| ------ | ---------- | ----------------- |
| RHEL 9, Fedora | NetworkManager | keyfiles, `nmcli` |
| Ubuntu Desktop | NetworkManager via netplan | `nmcli`, `/etc/netplan/` |
| Ubuntu Server | systemd-networkd via netplan | `/etc/netplan/*.yaml` |
| Debian server (no desktop) | ifupdown | `/etc/network/interfaces` |
| Debian desktop | NetworkManager | `nmcli` |
| Containers, minimal images | systemd-networkd or none | `/etc/systemd/network/*.network` |

 (Side note: DHCP clients have changed too. The classic `dhclient` from ISC is no longer maintained. NetworkManager and systemd-networkd have built-in DHCP clients, so on most current systems you never run a separate one.)

## Exam checklist

- A host needs an address with prefix, a default gateway, DNS servers and a hostname. DHCP can provide the first three.
- `/etc/hostname` holds the static name. `hostnamectl set-hostname` changes it permanently, `hostname newname` only until reboot.
- `/etc/hosts` = static name to address list, `/etc/resolv.conf` = `nameserver` and `search` lines, `/etc/nsswitch.conf` `hosts:` line = lookup order.
- NetworkManager: devices vs connections (profiles). `nmcli device status`, `nmcli connection show`, `add`, `modify`, `up`, `down`, `delete`, `reload`.
- Static IPv4 with nmcli: `ipv4.method manual`, `ipv4.addresses`, `ipv4.gateway`, `ipv4.dns`. Back to DHCP: `ipv4.method auto`. A `modify` needs `connection up` to take effect.
- Wi-Fi: `nmcli radio wifi on`, `nmcli device wifi list`, `nmcli device wifi connect SSID password PASS`.
- Debian ifupdown: `/etc/network/interfaces` with `auto`, `iface ... inet static|dhcp`; apply with `ifdown` then `ifup` (`ifup -a` for all `auto` ones).
- systemd-networkd: `.network` files with `[Match]` and `[Network]` in `/etc/systemd/network/`, status with `networkctl`. Ubuntu Server drives it through netplan.
- Don't hand-edit a managed `/etc/resolv.conf`; set DNS in NetworkManager, netplan or systemd-resolved.


## Byte check

1. Which command sets the hostname permanently on a systemd system?
   - `hostname web01`
   - **`hostnamectl set-hostname web01`** (correct)
   - `nmcli device set web01`
   `hostnamectl set-hostname` changes the running name and writes `/etc/hostname`. Plain `hostname web01` only lasts until the next reboot.

2. You changed `ipv4.addresses` on a NetworkManager connection with `nmcli connection modify`, but the interface still has the old address. What's missing?
   - Restart the whole machine
   - **Run `nmcli connection up <name>` to apply the profile** (correct)
   - Run `ifconfig` to refresh it
   `modify` only saves the profile. Activating it again with `nmcli connection up` (or `nmcli device reapply`) applies the new settings.

3. On Debian with ifupdown, which file holds the persistent interface configuration that `ifup` and `ifdown` read?
   - **`/etc/network/interfaces`** (correct)
   - `/etc/sysconfig/network`
   - `/etc/systemd/network/eth0.conf`
   ifupdown reads `/etc/network/interfaces` (and files in `/etc/network/interfaces.d/`). `/etc/sysconfig/` is the Red Hat family, and networkd files end in `.network`.


