
Technical Alley
Reading time:
5minutes
Remember to bookmark our official account
The network configuration of Linux systems varies by distribution, and beginners often get confused by various configuration files and commands. This article organizes the static IP configuration methods for the entire series of CentOS/RHEL and Debian/Ubuntu versions, from traditional configuration files to modern command tools, complete with examples and comparison tables, to help you quickly manage various Linux network settings.
1. CentOS/RHEL Series
1.1 CentOS 6/RHEL 6: Based on ifcfg-ethX File (Traditional Method)
- Configuration File Path:
<span>/etc/sysconfig/network-scripts/ifcfg-eth0</span> - Main Configuration and Comments:
DEVICE=eth0 # Network interface name (required)HWADDR=00:0C:29:3E:53:7E # MAC address of the network card (optional, used for binding devices)TYPE=Ethernet # Connection type (usually Ethernet)UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # Unique identifier (optional)ONBOOT=yes # Enable on bootBOOTPROTO=static # Protocol type: static, dhcp, noneIPADDR=10.0.0.100 # Static IP addressNETMASK=255.255.255.0 # Subnet maskGATEWAY=10.0.0.2 # Default gatewayDNS1=8.8.8.8 # Primary DNS serverDNS2=114.114.114.114 # Secondary DNS server (optional)
- Activation Command:
<span>service network restart</span> - Check Status:
<span>ifconfig</span>(applicable to all Linux systems)
1.2 CentOS 7/RHEL 7: Supports ifcfg-* and nmcli Management
- Configuration File Path:
<span>/etc/sysconfig/network-scripts/ifcfg-ens33</span>(interface names use predictable naming, such as ens33, enp0s3) - Main Configuration and Comments:
TYPE=Ethernet # Network typeBOOTPROTO=none # Boot protocol: none/static, dhcpNAME=ens33 # Interface nameDEVICE=ens33 # Must match the network card nameONBOOT=yes # Enable on bootIPADDR=10.0.0.100 # Static IP addressPREFIX=24 # Subnet prefix (equivalent to 255.255.255.0)GATEWAY=10.0.0.2 # Default gatewayDNS1=8.8.8.8 # Primary DNSDNS2=1.1.1.1 # Secondary DNS
- Activation Command:
<span>systemctl restart network</span>
1.3 CentOS 8/RHEL 8: Recommended nmcli or nmtui (still supports ifcfg)
- nmcli Command to Configure Static IP:
# Create connectionnmcli con add con-name <connection_name> ifname <interface_name> type ethernet ipv4.method manual ipv4.addresses <IP_address/prefix> ipv4.gateway <gateway> ipv4.dns <DNS># Example: Configure ens33 as 10.0.0.100/24nmcli con add con-name static-ens33 ifname ens33 type ethernet ipv4.method manual ipv4.addresses 10.0.0.100/24 ipv4.gateway 10.0.0.2 ipv4.dns "8.8.8.8 1.1.1.1"# Modify configurationnmcli con mod static-ens33 ipv4.dns "8.8.8.8 114.114.114.114"# Enable/Disable connectionnmcli con up static-ens33nmcli con down static-ens33
- Graphical Configuration: Enter
<span>nmtui</span>to access the interactive interface
2. Debian/Ubuntu Series
2.1 Ubuntu 16.04 and Debian 9 and Earlier: Use /etc/network/interfaces
- Configuration File Path:
<span>/etc/network/interfaces</span> - Main Configuration and Comments:
auto eth0 # Enable eth0 on bootiface eth0 inet static # Static configuration (dhcp for automatic acquisition) address 10.0.0.100 # Static IP address netmask 255.255.255.0 # Subnet mask gateway 10.0.0.2 # Default gateway dns-nameservers 8.8.8.8 114.114.114.114 # DNS servers (space-separated)
- Activation Command:
<span>sudo systemctl restart networking</span>
2.2 Ubuntu 18.04+/Debian 10+: Use netplan
- Configuration File Path:
<span>/etc/netplan/01-netcfg.yaml</span>(must follow YAML indentation rules) - Main Configuration and Comments:
network: version: 2 # Syntax version (fixed to 2) renderer: networkd # Background renderer (optional NetworkManager) ethernets: ens33: # Network interface name dhcp4: no # Disable DHCP (enable as yes) addresses: - 10.0.0.100/24 # Static IP and subnet prefix gateway4: 10.0.0.2 # Default gateway nameservers: addresses: - 8.8.8.8 # Primary DNS - 223.5.5.5 # Secondary DNS
- Activation Command:
<span>sudo netplan apply</span>
3. Temporary Configuration (Applicable to All Versions, Invalid After Reboot)
ip addr add 192.168.1.100/24 dev ens33 # Add temporary IPip link set ens33 up # Enable network cardip route add default via 192.168.1.1 # Set temporary gateway
4. Configuration Method Comparison Table
| System Version | Configuration Method | Configuration File/Tool |
|---|---|---|
| CentOS/RHEL 6 | ifcfg | /etc/sysconfig/network-scripts/ifcfg-* |
| CentOS/RHEL 7 | ifcfg + NetworkManager | Same as above + nmcli/nmtui |
| CentOS/RHEL 8 | Recommended NetworkManager | nmcli/nmtui + optional ifcfg files |
| Ubuntu ≤16.04 | interfaces | /etc/network/interfaces |
| Ubuntu ≥18.04 | netplan | /etc/netplan/*.yaml |
| Debian ≤9 | interfaces | /etc/network/interfaces |
| Debian ≥10 | netplan | /etc/netplan/*.yaml |
