Why Learn Linux Network Configuration?¶
As a Linux newbie, mastering network configuration is the first step to daily server usage and service deployment. Whether accessing the internet, remotely connecting to a server, or deploying websites/databases, network understanding and configuration are essential. This article will guide you from basic concepts to practical steps, enabling you to quickly get started with Linux network configuration in simple language.
I. Basic Network Concepts (Must-Know for Beginners)¶
Before configuring networks, understand these core concepts (like knowing the steering wheel and brakes before driving):
- IP Address: A device’s “ID” on the network, composed of 32-bit binary numbers (e.g.,
192.168.1.100), uniquely identifying the device. - Subnet Mask: Determines “insiders” vs. “outsiders.” For example,
255.255.255.0(abbreviated/24), indicating the first 24 bits of the IP are the “network segment” and the last 8 bits are the “host ID.” - Gateway: The “entrance/exit” for devices to connect to external networks, typically the router’s IP (e.g., home router
192.168.1.1). - DNS: The Domain Name System, translating domain names (e.g.,
www.baidu.com) to IP addresses. Common DNS servers:8.8.8.8(Google) and114.114.114.114(China).
II. Check Current Network Status (First Skill for Beginners)¶
Before configuration, check your device’s network info with simple, memorable commands:
- View IP Address:
ip addr show # Recommended (modern Linux default; ifconfig requires net-tools)
Example output:
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:11:22:33:44:55 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic noprefixroute eth0
Here, 192.168.1.100 is the IP, /24 is the subnet mask, and 192.168.1.1 is the gateway (check via default route).
- View Routing Table:
route -n # View gateways and default routes
- Test Network Connectivity:
ping 127.0.0.1 # Test local loopback (must succeed; otherwise, system network issues exist)
ping www.baidu.com # Test external connectivity (requires DNS resolution)
III. Quick Start: Configure Dynamic IP (DHCP Auto-Acquisition)¶
In home or corporate LANs, routers often auto-assign IPs via DHCP, and Linux will acquire them automatically.
Method: Use nmcli (Universal for CentOS/Ubuntu):
# 1. List existing network connections
nmcli connection show
# 2. Configure DHCP (assuming the network card is eth0)
nmcli connection modify eth0 ipv4.method auto # Set to auto-acquire
nmcli connection up eth0 # Activate the connection
# 3. Verify IP
ip addr # Confirm the assigned IP (e.g., 192.168.1.xxx)
IV. Advanced: Configure Static IP (Fixed IP)¶
For servers needing long-term fixed IPs (e.g., hosting websites/databases), manually configure:
Step 1: Determine Network Parameters
Assume your network is in the 192.168.1.0 segment (common home network). Prepare:
- IP Address: 192.168.1.100 (avoid conflicts)
- Subnet Mask: 255.255.255.0 (or /24)
- Gateway: 192.168.1.1 (router IP, check via ip route)
- DNS: 8.8.8.8 (Google) or 114.114.114.114 (China)
Step 2: Configure Static IP (CentOS Example)
CentOS uses ifcfg files for network card configuration:
# 1. Edit the network card config file (assume eth0)
vi /etc/sysconfig/network-scripts/ifcfg-eth0
Config File Content:
TYPE=Ethernet
BOOTPROTO=static # Manual configuration (not DHCP)
ONBOOT=yes # Auto-start on boot
IPADDR=192.168.1.100
NETMASK=255.255.255.0 # Subnet mask
GATEWAY=192.168.1.1 # Gateway
DNS1=8.8.8.8
DNS2=114.114.114.114
Step 3: Restart Network Service
systemctl restart network # Restart network (CentOS/RHEL)
Ubuntu User Configuration
Ubuntu uses netplan (18.04+):
# 1. Edit config file (network card name: enp0s3 or eth0, check via ip addr)
sudo vi /etc/netplan/01-netcfg.yaml
Config File Content:
network:
version: 2
renderer: networkd
ethernets:
enp0s3: # Network card name
dhcp4: no # Disable DHCP
addresses: [192.168.1.100/24] # IP/subnet mask
gateway4: 192.168.1.1 # Gateway
nameservers:
addresses: [8.8.8.8, 114.114.114.114] # DNS
Apply Configuration:
sudo netplan apply # Take effect immediately (no service restart needed)
V. Verify Configuration: Don’t Skip This Step!¶
After configuration, verify success:
- Check IP:
ip addrto confirm IP, subnet mask, and gateway are correct. - Test Connectivity:
-ping 127.0.0.1: Local loopback (must work).
-ping 192.168.1.100: Local IP reachable.
-ping 192.168.1.1: Gateway reachable (check IP/gateway if failed).
-ping www.baidu.com: External connectivity (requires DNS). - Test DNS:
nslookup www.baidu.com # Returns IP if DNS works
VI. Common Issues & Solutions¶
- IP Conflict: If
pingfails with “Host unreachable,” the IP may be occupied—change to a new IP. - Can’t Ping Gateway: Verify
GATEWAYmatches the router, or check physical connection (VM users: confirm virtual network settings). - Firewall Blocking Ping: CentOS blocks ICMP by default; allow it:
firewall-cmd --add-service=icmp --permanent
firewall-cmd --reload
Summary¶
The core of Linux network configuration is understanding the four elements: IP, subnet mask, gateway, and DNS. Progress from dynamic to static configuration. Practice commands like ip addr, ping, and nmcli to master configuration. When stuck, check IP, routes, and use ping for verification—systematic troubleshooting works!
(Newbies: Practice hands-on! Remember to save config files to avoid data loss.)