The network configuration of Linux systems is one of the core tasks of system administration. However, due to the differences in tools and configuration file formats used by different distributions, the methods for network configuration vary. This article will detail the network configuration methods of mainstream Linux distributions, including temporary configurations, permanent configurations, and the use of modern network management tools.
1. General Network Diagnostic Commands
Before introducing specific configuration methods, let’s first understand some general network diagnostic commands that are applicable in most Linux distributions:
# View network interface information
ip addr show
ip link show
# View routing table
ip route show
# Test network connection
ping -c 4 example.com
# View network connection status
ss -tuln
netstat -tuln # Some systems may require net-tools to be installed
# View DNS configuration
cat /etc/resolv.conf
# Test DNS resolution
nslookup example.com
dig example.com
2. Debian/Ubuntu Series
Debian and its derivatives (such as Ubuntu) use<span>/etc/network/interfaces</span>as the traditional network configuration file, and in recent years, they have gradually adopted Netplan as a new network configuration tool.
1. Traditional Configuration Method (/etc/network/interfaces)
# Edit the configuration file
sudo nano /etc/network/interfaces
# Static IP configuration example
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
# Dynamic IP acquisition (DHCP)
auto eth0
iface eth0 inet dhcp
To apply the configuration:
# Restart the network service
sudo systemctl restart networking
# Or restart a specific interface
sudo ifdown eth0 && sudo ifup eth0
2. Modern Configuration Method (Netplan)
Ubuntu 17.10 and later versions use Netplan as the network configuration tool, with configuration files located in<span>/etc/netplan/</span>directory, typically named<span>01-netcfg.yaml</span>or<span>50-cloud-init.yaml</span>.
# Edit the Netplan configuration file
sudo nano /etc/netplan/01-netcfg.yaml
# Static IP configuration example
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
# DHCP configuration example
# network:
# version: 2
# renderer: networkd
# ethernets:
# eth0:
# dhcp4: true
To apply the configuration:
# Test if there are any errors in the configuration
sudo netplan try
# Apply the configuration
sudo netplan apply
3. RedHat/CentOS Series
RedHat, CentOS, and other distributions use files in the<span>/etc/sysconfig/network-scripts/</span>directory for network configuration.
1. Traditional Configuration Method (network-scripts)
# Edit the configuration file for the corresponding interface, usually named ifcfg-eth0 or ifcfg-enp3s0
sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0
Static IP Configuration Example:
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
PREFIX=24
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
DHCP Configuration Example:
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
DEVICE=eth0
ONBOOT=yes
To apply the configuration:
# Restart the network service (CentOS 6)
sudo service network restart
# Restart the network service (CentOS 7+)
sudo systemctl restart network
# Or restart a specific interface
sudo ifdown eth0 && sudo ifup eth0
2. Modern Configuration Method (nmcli)
CentOS 7 and later versions recommend using the command-line tool of NetworkManager<span>nmcli</span>for network configuration.
# View network connections
nmcli connection show
# Create a new static IP connection
sudo nmcli connection add con-name eth0-static ifname eth0 type ethernet \
ip4 192.168.1.100/24 gw4 192.168.1.1
# Set DNS
sudo nmcli connection modify eth0-static ipv4.dns "8.8.8.8 8.8.4.4"
# Create DHCP connection
sudo nmcli connection add con-name eth0-dhcp ifname eth0 type ethernet
# Enable connection
sudo nmcli connection up eth0-static
# Disable connection
sudo nmcli connection down eth0-static
# Modify existing connection's IP address
sudo nmcli connection modify eth0-static ipv4.addresses 192.168.1.101/24
1. Manual Configuration Method
# Edit the network interface configuration file
sudo nano /etc/sysconfig/network/ifcfg-eth0
Static IP Configuration Example:
BOOTPROTO='static'
BROADCAST=''
ETHTOOL_OPTIONS=''
IPADDR='192.168.1.100/24'
MTU=''
NAME='Ethernet Card'
NETMASK=''
NETWORK=''
REMOTE_IPADDR=''
STARTMODE='auto'
USERCONTROL='no'
GATEWAY='192.168.1.1'
DHCP Configuration Example:
BOOTPROTO='dhcp'
BROADCAST=''
ETHTOOL_OPTIONS=''
IPADDR=''
MTU=''
NAME='Ethernet Card'
NETMASK=''
NETWORK=''
REMOTE_IPADDR=''
STARTMODE='auto'
USERCONTROL='no'
Set DNS:
sudo nano /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
To apply the configuration:
# Restart the network service
sudo systemctl restart network
# Or use ifup/ifdown
sudo ifdown eth0 && sudo ifup eth0
In the graphical interface, you can intuitively set parameters such as IP address, subnet mask, gateway, and DNS.
5. Arch Linux Series
Arch Linux adopts a simple and flexible configuration method, primarily managing networks through<span>systemd-networkd</span>or NetworkManager.
1. Using systemd-networkd
# Enable and start the systemd-networkd service
sudo systemctl enable --now systemd-networkd
sudo systemctl enable --now systemd-resolved
# Create a network configuration file
sudo nano /etc/systemd/network/eth0.network
Static IP Configuration Example:
[Match]
Name=eth0
[Network]
Address=192.168.1.100/24
Gateway=192.168.1.1
DNS=8.8.8.8
DNS=8.8.4.4
DHCP Configuration Example:
[Match]
Name=eth0
[Network]
DHCP=yes
2. Using NetworkManager
For desktop environments, Arch Linux typically recommends using NetworkManager:
# Install NetworkManager
sudo pacman -S networkmanager
# Enable and start the service
sudo systemctl enable --now NetworkManager
# Configure using nmcli command (similar usage as in CentOS)
nmcli connection show
mcli connection add con-name eth0-dhcp ifname eth0 type ethernet dhcp4 yes
6. Temporary Network Configuration
In some cases, we may need to perform temporary network configurations (which will not persist after a reboot):
# Set IP address
sudo ip addr add 192.168.1.100/24 dev eth0
# Delete IP address
sudo ip addr del 192.168.1.100/24 dev eth0
# Set default gateway
sudo ip route add default via 192.168.1.1
# Delete default gateway
sudo ip route del default via 192.168.1.1
# Enable network interface
sudo ip link set eth0 up
# Disable network interface
sudo ip link set eth0 down
# Temporarily set DNS (some systems may require restarting the network service)
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf
7. Comparison of Network Management Tools
| Distribution | Traditional Configuration File | Modern Management Tool | Configuration Tool |
|---|---|---|---|
| Debian/Ubuntu | /etc/network/interfaces | Netplan | netplan, ifupdown |
| CentOS/RHEL | /etc/sysconfig/network-scripts | NetworkManager | nmcli, nmtui |
| openSUSE | /etc/sysconfig/network | NetworkManager | YaST, nmcli |
| Arch Linux | No fixed format | systemd-networkd/NetworkManager | nmcli, ip |
8. Conclusion
Although there are differences in network configuration methods among different Linux distributions, the core concepts and goals are consistent: setting IP addresses, subnet masks, gateways, and DNS servers. As systems evolve, modern Linux distributions increasingly tend to use tools like NetworkManager or systemd-networkd for network management, which provide a more unified command-line interface and better dynamic network support.In practical operations, it is recommended to:
- For server environments, prioritize using the static configuration method recommended by the distribution
- For desktop environments, graphical tools can simplify configuration
- Backup original configuration files before making changes, so they can be restored in case of issues
- After configuration, use commands like ping and ip to verify that the network is functioning correctly
Mastering the network configuration methods of different distributions is a fundamental skill for system administrators and is essential for troubleshooting network issues.