主流 Linux 发行版网络管理命令的详细使用指南
一、基础网络配置工具(新旧对比)
# 传统工具 ifconfig(需安装 net-tools)
$ sudo apt install net-tools # Debian系
$ sudo yum install net-tools # RHEL系
$ ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe12:3456 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:12:34:56 txqueuelen 1000 (Ethernet)
RX packets 123456 bytes 987654321 (987.6 MB)
TX packets 654321 bytes 1234567890 (1.2 GB)
# 现代替代 iproute2 工具集
$ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:12:34:56 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 83457sec preferred_lft 83457sec
inet6 fe80::20c:29ff:fe12:3456/64 scope link
valid_lft forever preferred_lft forever
二、套接字监控工具
# ss 命令(替代 netstat)
$ ss -tulnp
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
users:(("sshd",pid=1234,fd=3))
tcp LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
users:(("master",pid=4567,fd=13))
udp UNCONN 0 0 0.0.0.0:68 0.0.0.0:*
users:(("dhclient",pid=7890,fd=6))
# 查看特定进程连接
$ ss -tap | grep nginx
ESTAB 0 0 192.168.1.100:443 203.0.113.5:62741 users:(("nginx",pid=1122,fd=14))
三、网络连接分析工具
# lsof 网络相关用法
$ lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 1122 root 6u IPv4 12345 0t0 TCP *:http (LISTEN)
nginx 1123 www-data 6u IPv4 12345 0t0 TCP *:http (LISTEN)
# 查看指定进程的网络连接
$ lsof -p 1234 -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1234 root 3u IPv4 23456 0t0 TCP *:ssh (LISTEN)
四、路由管理工具
# 传统 route 命令
$ route -n
Kernel IP routing table
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
# 现代 ip route 命令
$ ip route show
default via 192.168.1.1 dev eth0 proto static
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100
五、网络诊断工具
# traceroute(需安装 traceroute)
$ traceroute -n 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
1 192.168.1.1 1.234 ms 1.128 ms 1.045 ms
2 10.100.0.1 5.678 ms 5.543 ms 5.489 ms
3 203.0.113.254 10.123 ms 10.045 ms 9.987 ms
...
# mtr 实时诊断(需安装 mtr)
$ mtr -n 8.8.8.8
My traceroute [v0.93]
Keys: Help Display mode Restart statistics Order of fields quit
Host Loss% Snt Last Avg Best Wrst StDev
1. 192.168.1.1 0.0% 10 1.2 1.3 1.0 2.1 0.3
2. 10.100.0.1 0.0% 10 5.6 5.5 5.1 6.2 0.4
3. 203.0.113.254 0.0% 10 10.1 9.9 9.5 10.5 0.3
六、DNS 相关工具
# dig 命令(需安装 bind-utils/dnsutils)
$ dig +short A example.com
93.184.216.34
# 反向解析
$ dig -x 8.8.8.8 +short
dns.google.
# 查询特定DNS记录
$ dig example.com MX +short
10 mail.example.com.
七、高级网络监控
# nethogs 按进程监控流量(需安装)
$ sudo nethogs eth0
PID User Program Sent Received
1234 www-data nginx 123KB/s 456KB/s
5678 mysql mysqld 12KB/s 23KB/s
# iptraf-ng 全屏监控(需安装)
$ sudo iptraf-ng
[实时显示各接口流量统计]
八、网络绑定与桥接
# 查看网络接口绑定状态
$ cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)
Bonding Mode: IEEE 802.3ad Dynamic link aggregation
Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
...
# 桥接管理
$ brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29123456 no eth0
veth-abcd123
九、网络时间协议(NTP)
# 查看NTP同步状态
$ timedatectl
Local time: Tue 2023-08-22 14:30:00 CST
Universal time: Tue 2023-08-22 06:30:00 UTC
RTC time: Tue 2023-08-22 06:30:00
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
# 手动同步时间
$ sudo chronyc -a makestep
200 OK
十、防火墙管理(不同发行版)
# RHEL/CentOS (firewalld)
$ sudo firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
services: ssh dhcpv6-client
ports: 80/tcp 443/tcp
...
# Debian/Ubuntu (ufw)
$ sudo ufw status
Status: active
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
重要说明:
权限需求:大部分网络监控命令需要 root 权限
工具安装:
- Debian系:apt install iproute2 net-tools lsof traceroute dnsutils
- RHEL系:yum install iproute net-tools lsof traceroute bind-utils
- Arch系:pacman -S iproute2 net-tools lsof traceroute bind
现代替代方案推荐:
# ifconfig → ip addr / ip link
# netstat → ss
# route → ip route
# arp → ip neigh网络服务管理:
# systemd 系统
$ systemctl restart NetworkManager # RHEL/Fedora
$ systemctl restart networking # Debian
$ systemctl restart systemd-networkd # Arch网络配置文件位置:
- RHEL系:/etc/sysconfig/network-scripts/
- Debian系:/etc/network/interfaces
- NetworkManager:/etc/NetworkManager/system-connections/