Linux網路總是不通?一文搞懂子網掩碼、閘道器配置的所有坑
引言
第一章:網路基礎理論
1.1 IP地址與子網掩碼的關係
-
• A類:1.0.0.0 – 126.255.255.255,預設子網掩碼255.0.0.0 -
• B類:128.0.0.0 – 191.255.255.255,預設子網掩碼255.255.0.0 -
• C類:192.0.0.0 – 223.255.255.255,預設子網掩碼255.255.255.0
1.2 CIDR表示法
-
• /24 等價於 255.255.255.0 -
• /16 等價於 255.255.0.0 -
• /8 等價於 255.0.0.0
1.3 閘道器的作用
第二章:Linux網路配置檔案詳解
2.1 網路配置檔案結構
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network
/etc/resolv.conf
/etc/network/interfaces
/etc/netplan/(Ubuntu 18.04+)
/etc/resolv.conf
2.2 RedHat/CentOS網路配置
# 基本配置
DEVICE=eth0 # 網絡卡裝置名
TYPE=Ethernet # 連線型別
ONBOOT=yes# 開機啟動
BOOTPROTO=static # 靜態IP配置
# IP地址配置
IPADDR=192.168.1.100 # IP地址
NETMASK=255.255.255.0 # 子網掩碼
PREFIX=24 # 網路字首(可選,與NETMASK二選一)
GATEWAY=192.168.1.1 # 預設閘道器
# DNS配置
DNS1=8.8.8.8 # 首選DNS
DNS2=8.8.4.4 # 備用DNS
# 高階配置
HWADDR=00:50:56:12:34:56 # MAC地址
USERCTL=no # 普通使用者控制
NM_CONTROLLED=no # NetworkManager控制
DEFROUTE=yes# 預設路由
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=dhcp
HWADDR=00:50:56:12:34:56
2.3 Debian/Ubuntu網路配置
# 迴環介面
auto lo
iface lo inet loopback
# 靜態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
dns-search example.com
# DHCP配置
auto eth1
iface eth1 inet dhcp
# 多IP配置
auto eth0:0
iface eth0:0 inet static
address 192.168.1.101
netmask 255.255.255.0
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]
search: [example.com]
dhcp4:false
第三章:子網掩碼深入解析
3.1 子網掩碼的二進位制表示
IP地址: 192.168.1.100 = 11000000.10101000.00000001.01100100
子網掩碼: 255.255.255.0 = 11111111.11111111.11111111.00000000
網路地址: 192.168.1.0 = 11000000.10101000.00000001.00000000
廣播地址: 192.168.1.255 = 11000000.10101000.00000001.11111111
3.2 子網劃分例項
原網路:192.168.1.0/24 (256個地址)
新掩碼:/26 (64個地址每個子網)
子網1:192.168.1.0/26 (192.168.1.1 - 192.168.1.62)
子網2:192.168.1.64/26 (192.168.1.65 - 192.168.1.126)
子網3:192.168.1.128/26 (192.168.1.129 - 192.168.1.190)
子網4:192.168.1.192/26 (192.168.1.193 - 192.168.1.254)
3.3 VLSM(可變長子網掩碼)
# 伺服器網段(需要30個地址)
192.168.1.0/27 # 掩碼255.255.255.224
# 工作站網段(需要100個地址)
192.168.1.128/25 # 掩碼255.255.255.128
# 點對點鏈路(需要2個地址)
192.168.1.252/30 # 掩碼255.255.255.252
第四章:閘道器配置與路由管理
4.1 預設閘道器配置
# 新增預設閘道器
route add default gw 192.168.1.1
# 或使用ip命令
ip route add default via 192.168.1.1
# 刪除預設閘道器
route del default gw 192.168.1.1
ip route del default via 192.168.1.1
# RedHat/CentOS
echo"GATEWAY=192.168.1.1" >> /etc/sysconfig/network-scripts/ifcfg-eth0
# Debian/Ubuntu
echo"gateway 192.168.1.1" >> /etc/network/interfaces
4.2 靜態路由配置
# 訪問10.0.0.0/8網路透過192.168.1.254閘道器
route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254
# 使用ip命令
ip route add 10.0.0.0/8 via 192.168.1.254
# 指定出口介面
ip route add 10.0.0.0/8 via 192.168.1.254 dev eth0
# /etc/sysconfig/network-scripts/route-eth0
10.0.0.0/8 via 192.168.1.254
172.16.0.0/16 via 192.168.1.253
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
up route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254
down route del -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.254
4.3 路由表管理
# 傳統命令
route -n
netstat -rn
# 現代命令
ip route show
ip route show table main
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
-
• U:路由有效 -
• G:透過閘道器 -
• H:目標是主機 -
• D:動態路由 -
• M:修改路由
第五章:網路診斷與故障排除
5.1 網路連通性測試
# 測試本地網路
ping 192.168.1.1
# 測試外網連通性
ping 8.8.8.8
ping www.google.com
# 測試特定埠
telnet 192.168.1.1 80
nc -zv 192.168.1.1 80
5.2 路由追蹤
# 追蹤資料包路徑
traceroute 8.8.8.8
tracepath 8.8.8.8
# 使用mtr進行持續監控
mtr 8.8.8.8
5.3 網路配置檢查
# 檢視介面資訊
ifconfig
ip addr show
# 檢視介面統計
ip -s link show
# 檢查網路服務狀態
systemctl status networking # Debian/Ubuntu
systemctl status network # RedHat/CentOS
5.4 常見故障排除
-
1. 檢查物理連線:
# 檢查網線連線狀態
ethtool eth0
-
2. 檢查網路配置:
# 驗證IP配置
ip addr show eth0
# 檢查路由表
ip route show
-
3. 檢查DNS解析:
# 測試DNS解析
nslookup www.google.com
dig www.google.com
-
4. 檢查防火牆:
# 檢視防火牆狀態
iptables -L
systemctl status firewalld
第六章:高階網路配置
6.1 網路繫結(Bonding)
# 建立繫結介面配置
# /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
TYPE=Bond
BONDING_MASTER=yes
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
BONDING_OPTS="mode=1 miimon=100"
# 配置從屬介面
# /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
6.2 VLAN配置
# 建立VLAN介面
# /etc/sysconfig/network-scripts/ifcfg-eth0.100
DEVICE=eth0.100
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
VLAN=yes
6.3 橋接配置
# 建立橋接介面
# /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
ONBOOT=yes
DELAY=0
# 配置橋接成員
# /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
BRIDGE=br0
第七章:網路安全配置
7.1 防火牆配置
# 允許本地迴環
iptables -A INPUT -i lo -j ACCEPT
# 允許已建立的連線
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允許SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允許HTTP和HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 預設拒絕
iptables -P INPUT DROP
7.2 網路訪問控制
# /etc/hosts.allow
sshd: 192.168.1.0/24
httpd: ALL
# /etc/hosts.deny
ALL: ALL
第八章:效能最佳化與監控
8.1 網路效能調優
# /etc/sysctl.conf
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 65536 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_congestion_control = bbr
net.core.netdev_max_backlog = 5000
8.2 網路監控
# 使用iftop監控
iftop -i eth0
# 使用nethogs監控程序流量
nethogs eth0
# 使用ss檢視連線狀態
ss -tuln
第九章:自動化網路配置
9.1 指令碼化網路配置
#!/bin/bash
# network-config.sh
INTERFACE="eth0"
IP_ADDRESS="192.168.1.100"
NETMASK="255.255.255.0"
GATEWAY="192.168.1.1"
DNS1="8.8.8.8"
DNS2="8.8.4.4"
# 檢查系統型別
if [ -f /etc/redhat-release ]; then
# RedHat/CentOS配置
cat > /etc/sysconfig/network-scripts/ifcfg-$INTERFACE << EOF
DEVICE=$INTERFACE
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=$IP_ADDRESS
NETMASK=$NETMASK
GATEWAY=$GATEWAY
DNS1=$DNS1
DNS2=$DNS2
EOF
systemctl restart network
elif [ -f /etc/debian_version ]; then
# Debian/Ubuntu配置
cat > /etc/network/interfaces << EOF
auto lo
iface lo inet loopback
auto $INTERFACE
iface $INTERFACE inet static
address $IP_ADDRESS
netmask $NETMASK
gateway $GATEWAY
dns-nameservers $DNS1 $DNS2
EOF
systemctl restart networking
fi
9.2 Ansible網路配置
---
-name:Configurenetworkinterface
hosts:servers
become:yes
vars:
interface:eth0
ip_address:192.168.1.100
netmask:255.255.255.0
gateway:192.168.1.1
tasks:
-name:Configurenetworkinterface(RedHat/CentOS)
template:
src:ifcfg-interface.j2
dest:"/etc/sysconfig/network-scripts/ifcfg-{{ interface }}"
when:ansible_os_family=="RedHat"
notify:restartnetwork
-name:Configurenetworkinterface(Debian/Ubuntu)
template:
src:interfaces.j2
dest:/etc/network/interfaces
when:ansible_os_family=="Debian"
notify:restartnetworking
handlers:
-name:restartnetwork
service:
name:network
state:restarted
-name:restartnetworking
service:
name:networking
state:restarted
第十章:雲環境網路配置
10.1 AWS網路配置
# 配置彈性IP
aws ec2 associate-address --instance-id i-1234567890abcdef0 --public-ip 203.0.113.12
# 配置安全組
aws ec2 authorize-security-group-ingress \
--group-id sg-12345678 \
--protocol tcp \
--port 80 \
--cidr 0.0.0.0/0
10.2 Docker網路配置
# 建立自定義網路
docker network create --driver bridge \
--subnet=192.168.100.0/24 \
--gateway=192.168.100.1 \
mynetwork
# 啟動容器並指定網路
docker run -d --name web \
--network mynetwork \
--ip 192.168.100.10 \
nginx
結語








