Introduction
Linux, the powerhouse of servers and development environments, offers unparalleled control over networking. From simple home setups to complex enterprise infrastructures, understanding Linux network configuration is a fundamental skill. This cheat sheet aims to provide a concise yet comprehensive guide, covering traditional tools, the modern iproute2
suite, and specific configurations for Red Hat and Ubuntu. We’ll also highlight how to make your ad-hoc changes persistent across reboots.
Networking in Linux can seem daunting at first, with a multitude of commands and configuration files. However, at its core, it’s about managing network interfaces, IP addresses, routing tables, and name resolution. This guide will walk you through the essential commands and concepts, empowering you to confidently configure your Linux network.
Important Note: The network configurations detailed in this blog post are primarily intended for Linux systems running as pure servers or those without a graphical desktop environment (DE). If your Linux system has a Desktop Environment installed (such as GNOME, KDE, or XFCE), it will likely use its own built-in network management utility (e.g., NetworkManager). These DE-specific tools typically supersede and may conflict with the manual command-line and configuration file adjustments discussed here. For desktop users, it’s generally recommended to manage your network settings through your DE’s graphical interface to avoid unexpected behavior.
Linux Network Cheat Sheet
We’ll categorize our commands and configurations for clarity. Remember that ad-hoc commands are temporary and will be lost on reboot. To make them persistent, you’ll need to save them to the appropriate configuration files for your distribution.
I. Traditional Network Configuration (Legacy Tools)
While largely superseded by iproute2
, these tools are still found on older systems or can be useful for quick checks.
ifconfig
: Displays and configures network interfaces.
ifconfig -a # Show all interfaces ifconfig eth0 # Show specific interface (e.g., eth0) sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 # Assign IP address and netmask (ad-hoc) sudo ifconfig eth0 up # Bring interface up sudo ifconfig eth0 down # Bring interface down
route
: Manages the IP routing table.
route -n (numerical output) # Display routing table sudo route add default gw 192.168.1.1 # Add a default gateway (ad-hoc) sudo route add -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.1.1 # Add a static route to a network: sudo route del -net 10.0.0.0 netmask 255.255.255.0 # Delete a route
Netstat
: Displays network connections, routing tables, interface statistics, etc.
netstat -tuln # Show all listening ports netstat -rn # Show routing table netstat -i # Show interface statistics
II. IP Route2 (Modern Tools)
The iproute2
suite is the preferred and more powerful set of tools for network configuration in modern Linux distributions.
ip link
: Manages network devices.
ip link show # List all interfaces sudo ip link set dev eth0 up sudo ip link set dev eth0 down # Bring interface up/down sudo ip link set dev eth0 down sudo ip link set dev eth0 address 00:11:22:33:44:55 sudo ip link set dev eth0 up # Change MAC address (requires interface down):
ip address
(or ip addr
): Manages IP addresses on interfaces.
ip addr show or ip a # List IP addresses sudo ip addr add 192.168.1.100/24 dev eth0 # Add IP address sudo ip addr del 192.168.1.100/24 dev eth0 # Delete IP address
ip route
: Manages the routing table.
ip route show # Display routing table sudo ip route add default via 192.168.1.1 dev eth0 # Add a default gateway sudo ip route add 10.0.0.0/24 via 192.168.1.1 dev eth0 # Add a static route sudo ip route del 10.0.0.0/24 # Delete a route
ip neigh
(or ip neighbour
): Manages ARP/Neighbor cache.
ip neigh show # Show ARP cache sudo ip neigh add 192.168.1.100 lladdr 00:11:22:33:44:55 dev eth0 # Add a static ARP entry
III. Modern Red Hat and Latest Ubuntu Network Configuration
Modern Linux distributions have moved towards more abstracted network configuration tools.
Red Hat (RHEL/CentOS/Fedora)
Red Hat-based systems primarily use NetworkManager and nmcli
(NetworkManager Command Line Interface) for configuration, with ifcfg
files as a legacy option.
nmcli
(NetworkManager CLI):
nmcli con show # List all connections nmcli dev status # List all devices sudo nmcli connection add type ethernet con-name myconnection ifname eth0 ip4 192.168.1.100/24 gw4 192.168.1.1 # Create a new Ethernet connection (ad-hoc, then persist) sudo nmcli con up myconnection sudo nmcli con down myconnection # Bring a connection up/down sudo nmcli con modify myconnection ipv4.dns "8.8.8.8" sudo nmcli con reload myconnection (to apply changes) # Modify an existing connection (e.g., add DNS server) sudo nmcli con delete myconnection # Delete a connection
ifcfg
files (Legacy but still widely used for persistence):
– Located in/etc/sysconfig/network-scripts/
.
– Example/etc/sysconfig/network-scripts/ifcfg-eth0
for static IP:
TYPE=Ethernet BOOTPROTO=static NAME=eth0 DEVICE=eth0 ONBOOT=yes IPADDR=192.168.1.100 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 DNS1=8.8.8.8
After editing, restart NetworkManager or the network service: sudo systemctl restart NetworkManager
or sudo systemctl restart network
(if NetworkManager is not used/installed)
Latest Ubuntu (and Debian)
Ubuntu (18.04 and later) uses Netplan for network configuration, which generates configuration for backend renderers like NetworkManager or systemd-networkd. Older versions use /etc/network/interfaces
.
Netplan (Ubuntu 18.04+):
– Configuration files are YAML files located in /etc/netplan/
.
– Example /etc/netplan/01-netcfg.yaml
for static IP:
network: version: 2 renderer: networkd # or NetworkManager 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 Netplan configuration: sudo netplan apply
— Generate debug information: sudo netplan generate --debug
/etc/network/interfaces
(Older Ubuntu/Debian):
Example for 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
Restart networking service: sudo systemctl restart networking
IV. Making Ad-Hoc Commands Persistent
As mentioned, commands run directly in the terminal are temporary. To make them survive a reboot, you need to configure them through the distribution-specific configuration methods.
- For Red Hat/CentOS (using NetworkManager/
nmcli
):- When you use
nmcli con add
ornmcli con modify
, the changes are automatically saved to NetworkManager’s connection profiles (typically in/etc/NetworkManager/system-connections/
). These profiles are persistent. - If using legacy
ifcfg
files, directly edit the files in/etc/sysconfig/network-scripts/
.
- When you use
- For Ubuntu/Debian (using Netplan):
- Create or modify YAML files in
/etc/netplan/
. After making changes, runsudo netplan apply
. Netplan handles generating the persistent configuration. - For older Ubuntu/Debian, edit
/etc/network/interfaces
.
- Create or modify YAML files in
General approach for persistence:
- Identify your distribution’s primary network configuration tool/method. (NetworkManager with
nmcli
/ifcfg
for Red Hat, Netplan for recent Ubuntu,/etc/network/interfaces
for older Ubuntu/Debian). - Translate your ad-hoc commands into the respective configuration syntax.
- Edit the appropriate configuration file(s). Always make a backup before editing:
sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak
- Apply/restart the network service. This varies by distribution and configuration tool (e.g.,
sudo netplan apply
,sudo systemctl restart NetworkManager
,sudo systemctl restart networking
).
Final Thoughts
Mastering Linux network configuration is an invaluable skill. While ad-hoc commands provide immediate results for troubleshooting or temporary setups, understanding the persistent configuration methods for your specific Linux distribution is key to building stable and reliable network environments. Embrace iproute2
for its power and flexibility, and leverage distribution-specific tools like NetworkManager with nmcli
or Netplan for streamlined and persistent configurations. Always remember to back up your configuration files before making significant changes!