applied linux.

Chapter 3 of 9

Network Configuration

On this page

Linux network configuration can be managed in several ways, depending on the network type, the distribution, and the tools you prefer. Below are the main forms of Linux network configuration:

Manual Network Configuration

You can manually configure network settings directly by editing the necessary configuration files or using command-line utilities. This method is the most basic and gives you granular control over network parameters.

Network Configuration in Ubuntu/Debian

For Debian-based systems, you can configure network interfaces by editing /etc/network/interfaces. Here’s an example:

# Configure a static IP
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4

To bring up or down a specific network interface:

sudo ifup eth0
sudo ifdown eth0

Network Configuration in Rocky/RedHat

For Red Hat-based distributions such as Rocky, network interfaces are typically configured in /etc/sysconfig/network-scripts/ifcfg-eth0. Here’s an example:

DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

After editing, restart the network service:

sudo systemctl restart network

Using Network Manager

NetworkManager is a dynamic network control and configuration system that supports multiple network types (Ethernet, Wi-Fi, VPNs, etc.). It’s commonly used in desktop environments and provides both a graphical interface and command-line tools.

GUI Tools

  • GNOME NetworkManager: Allows you to easily configure networks through a graphical interface in desktop environments.
  • nm-applet: A system tray applet for managing networks, often found in GNOME or other desktop environments.

Command-Line Tools

  • nmcli: Command-line tool for managing networks.

Example of listing available connections:

nmcli con show

Example of setting up a static IP address:

nmcli con mod eth0 \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns 8.8.8.8 \
ipv4.method manual

nmcli con up eth0
  • nmtui: Text-based user interface for NetworkManager. It provides a simple way to configure networks from the terminal.
sudo nmtui

Systemd-networkd

systemd-networkd is a daemon that manages network configurations, particularly useful in server environments or when minimal software is desired. Configuration is done via files in /etc/systemd/network/.

Example Static IP Configuration:

[Match]
Name=eth0

[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8

After editing the configuration, reload and restart the systemd-networkd service:

sudo systemctl restart systemd-networkd

ifconfig and ip Command

Two key command-line utilities for configuring network interfaces are ifconfig (deprecated) and ip.

ifconfig (older tool)

While still present on some older distributions, ifconfig has been replaced by ip on most modern systems. However, ifconfig can still be used to view and configure network interfaces.

View network interfaces:

ifconfig

Bring an interface up:

sudo ifconfig eth0 up

ip (modern replacement for ifconfig)

The ip tool from the iproute2 package is the preferred utility for network configuration on modern Linux systems.

View all network interfaces:

ip addr show

Bring an interface up:

sudo ip link set eth0 up

Assign a static IP address:

sudo ip addr add 192.168.1.100/24 dev eth0

Remove an IP address:

sudo ip addr del 192.168.1.100/24 dev eth0

View routing information:

ip route show

DHCP Client

Many Linux systems use DHCP (Dynamic Host Configuration Protocol) to automatically configure network settings. The DHCP client software most commonly used includes dhclient, dhcpcd, or systemd-networkd’s built-in DHCP client.

Start or stop DHCP client service:

sudo systemctl start dhclient@eth0
sudo systemctl stop dhclient@eth0

You can also run the DHCP client manually if you are setting up temporary configurations:

sudo dhclient eth0

Netplan (Ubuntu)

On Ubuntu (from 17.10 onwards), netplan is used for network configuration. It applies Yet Another Markup Language (YAML)-based configurations to either NetworkManager or systemd-networkd which look like the configuration below.

Example Configuration (Static IP):

In /etc/netplan/01-netcfg.yaml:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses: [192.168.1.100/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]

Apply the configuration:

sudo netplan apply

Wi-Fi Configuration

Wi-Fi network configuration can be handled by tools like wpa_supplicant (for manually managing Wi-Fi connections) or through NetworkManager’s GUI or CLI tools.

wpa_supplicant

This is a service that can manage Wi-Fi connections directly, often used in headless or server environments.

To configure a Wi-Fi network, you would create a file like /etc/wpa_supplicant.conf:

network={
    ssid="YourNetworkSSID"
    psk="YourNetworkPassword"
}

Then, start wpa_supplicant:

sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf

Firewall and Advanced Networking

For more advanced networking tasks, you can configure firewalls, routing, and network address translation (NAT) using tools like iptables, firewalld, or ufw.

iptables

iptables allows you to configure the rules that govern how packets are routed, filtered, and handled on your system. It’s a powerful, low-level tool used for creating complex firewall rules and routing configurations.

Install Additional Software

Just as every instrument needs the right tuning. Your Linux system needs the right mix of software to perform at its best. Installing this software requires temporarily elevating your privileges to the root or administrative user. There are two primary utilities that are used: “sudo” and “doas”. We’ll use the more common sudo.

During your initial installation you were asked to setup a user password, which you used to login. Now you need to use that same password to elevate your privileges. You can do so by typing “sudo -i”

Whether you need a new text editor, a development environment, or media players, installing additional software is like adding new instruments or accessories to your band. For example, the “tldr” package provides complementary information to the system manual pages. Using the commands below with elevated privileges the “tldr” package is installed as follows:

Distribution Command
Debian-based sudo apt install tldr
Rocky Linux sudo dnf -y install tldr
Arch Linux sudo pacman -S tldr

Now any time that you want a command explained plainly, you can type “tldr” and the command. Try typing “tldr cd” or “tldr ls” to get some ideas how to move around.

Tailoring your software setup is akin to picking the perfect drumsticks or guitar picks — these small choices make a big difference in each performance.

Keeping your system up to date.

System Updates

  • Think of system updates as regular tuning sessions. Use package managers to update your system, ensuring that all packages are current.
  • For Debian-based systems (like Ubuntu), you’d use apt:
    • sudo apt update && sudo apt upgrade
  • For Rocky, you’d use dnf:
    • sudo dnf update
  • For Arch Linux, you’d use pacman:
    • sudo pacman -Syu

Keeping your system updated is like making sure your guitar strings are tight and your drum skins are intact—it’s essential for a flawless performance.

Configure Hardware Drivers

  • Hardware Drivers:
  • Ensure your system’s hardware components, like GPUs and wireless network adapters, are functioning correctly.

    You might need to install proprietary drivers, particularly for graphics cards or special hardware.

  • For example, on Ubuntu, you can use:

    sudo ubuntu-drivers autoinstall

    This allows Ubuntu to automatically install the most common drivers required for the configured hardware.

This step is like ensuring your guitar’s amplifier is correctly set up - it establishes the best sound possible for each performance.

Exercise

To set a static IP address on a Linux system that uses ifconfig and netplan, you first need to install the necessary tools if they aren’t already available. In most Debian-based systems, you can install net-tools using the following command:

sudo apt-get install net-tools

Once installed, you’ll need to edit the network configuration file for your specific network interface. The configuration file is usually located in /etc/netplan/. Open it with a text editor, such as nano:

sudo nano /etc/netplan/01-netcfg.yaml

Inside this file, you’ll configure the static IP address. Here’s an example of what the configuration might look like:

network:
  version: 2
  ethernets:
    eth0:
      addresses: [192.168.1.100/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]

In this example: - eth0 is the network interface (it might be different on your system). - 192.168.1.100/24 is the static IP address. - 192.168.1.1 is the gateway (the IP address of your router). - The DNS nameservers are set to Google’s public DNS servers (8.8.8.8 and 8.8.4.4).

After making these changes, save the file and exit the editor. To apply the new configuration and enable the static IP, run:

sudo netplan apply

Once applied, your system will have a static IP address. This means the IP will remain fixed and won’t change after reboots or network restarts.

$ try it — a real Linux shell, running in your browser

Boots a Buildroot Linux kernel client-side with v86, a 32-bit x86 emulator compiled to WebAssembly. Nothing you type leaves this tab. First boot fetches a ~10 MB kernel; it then runs offline.