Kahibaro
Discord Login Register

3.3.5 Editing network configuration files

Understanding How Linux Stores Network Configuration

Linux distributions use different mechanisms and file locations for network configuration, but they all solve the same problems:

You should always know what networking stack your distro uses before editing anything:

The safest workflow:

  1. Identify the active networking system.
  2. Locate the correct configuration file(s).
  3. Edit with root privileges.
  4. Validate syntax if a tool is available.
  5. Apply/reload the configuration in a controlled way.

The sections below focus on editing configuration files for the most common setups.


Identifying Which System Manages Networking

Use these checks (you can run more than one):

systemctl is-active NetworkManager
systemctl is-active systemd-networkd

Only edit the system that is actually active; mixing them often leads to confusion.


Editing `/etc/network/interfaces` (Debian-Style)

Used by: classic ifupdown on Debian/old Ubuntu.

Main file: /etc/network/interfaces
Additional per‑interface snippets may live in /etc/network/interfaces.d/.

Basic Structure

Sections begin with a auto or allow-hotplug line, followed by an iface stanza:

# Bring up at boot
auto enp0s3
# Interface configuration
iface enp0s3 inet static
    address 192.168.1.50/24
    gateway 192.168.1.1
    dns-nameservers 1.1.1.1 8.8.8.8

Key parts:

For DHCP:

auto enp0s3
iface enp0s3 inet dhcp

Common Options

Inside an iface block:

  address 192.168.1.50
  netmask 255.255.255.0

Applying Changes

After editing:

sudo ifdown enp0s3 && sudo ifup enp0s3
# or reboot if you’re unsure

Be careful when doing this over SSH: misconfiguration can lock you out.


Editing Netplan Configuration (Ubuntu 18.04+)

Used by: Ubuntu server/desktop.
Netplan converts YAML in /etc/netplan/ into backend configs (NetworkManager or systemd-networkd).

Main files:

YAML Basics for Netplan

Example: Static IP on Ethernet

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: no
      addresses:
        - 192.168.1.50/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
          - 1.1.1.1
          - 8.8.8.8
        search:
          - example.com

Common Fields

For DHCP:

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp0s3:
      dhcp4: yes

Validating and Applying

After editing:

sudo netplan try

If satisfied:

sudo netplan apply

If you get YAML errors, netplan will show line/column hints; double‑check indentation and colons.


Editing `systemd-networkd` Files

Used by: some servers (Debian, Ubuntu, Arch, etc.), containers, minimalist systems.
You configure networkd directly if there is no netplan/NetworkManager or if you choose to.

Key directories:

`.network` File Structure

A .network file describes how systemd-networkd should manage an interface.

Example static IP:

# /etc/systemd/network/10-enp0s3.network
[Match]
Name=enp0s3
[Network]
Address=192.168.1.50/24
Gateway=192.168.1.1
DNS=1.1.1.1 8.8.8.8
Domains=example.com

For DHCP:

[Match]
Name=enp0s3
[Network]
DHCP=yes

Common Sections

Enabling and Applying

Ensure services are enabled:

sudo systemctl enable --now systemd-networkd systemd-resolved

After changing .network files:

sudo systemctl restart systemd-networkd

In some setups you also link /etc/resolv.conf to systemd-resolved’s stub, but that belongs in DNS configuration specifics.


Editing RHEL/Fedora `ifcfg-*` Files

Used by: traditional RHEL/CentOS/Fedora networking (still supported, sometimes behind NetworkManager).

Location: /etc/sysconfig/network-scripts/ifcfg-<interface>

Example static IPv4:

# /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.1.50
PREFIX=24
GATEWAY=192.168.1.1
DNS1=1.1.1.1
DNS2=8.8.8.8

For DHCP:

TYPE=Ethernet
BOOTPROTO=dhcp
NAME=ens33
DEVICE=ens33
ONBOOT=yes

Key fields:

Apply changes with:

sudo nmcli connection reload   # if NetworkManager manages them
# or legacy:
sudo systemctl restart network

Beware: modern RHEL/Fedora relies primarily on NetworkManager; editing ifcfg-* is often reflected via NetworkManager rather than the old network service.


Editing DNS Configuration Files

DNS may be managed by different components; two common file approaches:

`/etc/resolv.conf`

Traditional file used by the resolver library.

Typical content:

nameserver 1.1.1.1
nameserver 8.8.8.8
search example.com

However, on modern systems this file is often auto‑generated by:

You can check with:

ls -l /etc/resolv.conf

If it is a symlink to something like /run/systemd/resolve/stub-resolv.conf, editing it will not persist across reboots. In that case, configure DNS in:

`/etc/hosts`

Local static hostname resolution. Manual entries here override DNS:

127.0.0.1   localhost
192.168.1.10   app-server.local app-server

Typical use:

No service restart is required; changes take effect immediately.


openSUSE `wicked` and `ifcfg` Files (Brief)

openSUSE traditionally uses wicked with ifcfg-* files under /etc/sysconfig/network/:

Structure is similar in spirit to RHEL ifcfg-*, but keys differ (e.g., BOOTPROTO, STARTMODE, etc.). When in doubt, use distro documentation or yast / wicked CLI tools and inspect the generated files.


Safe Editing Practices

When editing any network configuration file:

  1. Use root privileges safely
    • sudo nano /path/to/file
    • Or sudoedit /path/to/file to reduce mistakes.
  2. Make a backup first
   sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak
  1. Validate syntax when possible
    • netplan try
    • systemd-analyze verify /etc/systemd/network/*.network (for advanced systemd-networkd usage)
    • YAML linting tools for netplan files.
  2. Avoid locking yourself out over SSH
    • Keep an open root console (physical, serial, or virtual machine console).
    • Use tools like netplan try which auto‑revert on failure.
    • Make incremental changes instead of large rewrites.
  3. Document your changes
    • Add brief comments in files (# why this IP, what service, date).
    • Keep a simple change log, especially on servers.

By understanding how to safely edit your distribution’s network configuration files, you gain precise, scriptable control over IP addressing, routing, and DNS, which is essential for reliable Linux system administration.

Views: 106

Comments

Please login to add a comment.

Don't have an account? Register now!