Linux网络排查命令大全|一文搞定网络问题

服务器网络问题是最头疼的,连不上、丢包、延迟高...

这篇整理常用的网络排查命令,收藏备用。


一、基础连通性检测

ping

# 基本ping
ping 192.168.1.1

# 指定次数
ping -c 4 192.168.1.1

# 指定间隔(秒)
ping -i 0.5 192.168.1.1

# 指定包大小
ping -s 1400 192.168.1.1

常见问题:

  • Destination Host Unreachable:目标不可达,检查路由
  • Request timeout:超时,可能被防火墙拦截

telnet

# 测试端口连通性
telnet 192.168.1.1 80
telnet 192.168.1.1 3306

nc (netcat)

# 测试TCP端口
nc -zv 192.168.1.1 80

# 测试UDP端口
nc -zuv 192.168.1.1 53

# 扫描端口范围
nc -zv 192.168.1.1 1-1000

curl

# 测试HTTP
curl -I http://192.168.1.1

# 显示详细信息
curl -v http://192.168.1.1

# 测试连接时间
curl -o /dev/null -s -w "DNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTotal: %{time_total}s\n" http://example.com

二、路由追踪

traceroute

# 追踪路由
traceroute 8.8.8.8

# 使用ICMP(需要root)
traceroute -I 8.8.8.8

# 使用TCP
traceroute -T -p 80 8.8.8.8

mtr(推荐)

结合了ping和traceroute,实时显示:

# 安装
yum install mtr  # CentOS
apt install mtr  # Ubuntu

# 使用
mtr 8.8.8.8

# 报告模式
mtr -r -c 100 8.8.8.8

输出:

HOST                   Loss%   Snt   Last   Avg  Best  Wrst StDev
1. gateway             0.0%   100    0.5   0.6   0.4   1.2   0.1
2. 10.0.0.1            0.0%   100    5.2   5.1   4.8   6.0   0.3
3. 8.8.8.8             0.0%   100   10.1  10.2   9.8  11.5   0.4

三、DNS排查

nslookup

# 查询域名
nslookup example.com

# 指定DNS服务器
nslookup example.com 8.8.8.8

# 查询特定记录类型
nslookup -type=mx example.com

dig(推荐)

# 查询A记录
dig example.com

# 查询所有记录
dig example.com ANY

# 指定DNS服务器
dig @8.8.8.8 example.com

# 简洁输出
dig +short example.com

# 追踪解析过程
dig +trace example.com

检查本地DNS配置

cat /etc/resolv.conf

四、端口和连接

ss(推荐,替代netstat)

# 查看所有TCP连接
ss -t

# 查看监听端口
ss -tln

# 查看带进程信息
ss -tlnp

# 查看特定端口
ss -tlnp | grep 8080

# 统计连接状态
ss -s

netstat

# 查看所有连接
netstat -an

# 查看监听端口
netstat -tlnp

# 统计连接状态
netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,state[key]}'

lsof

# 查看端口被谁占用
lsof -i :8080

# 查看进程的网络连接
lsof -i -p 1234

五、抓包分析

tcpdump

# 抓取指定接口
tcpdump -i eth0

# 抓取指定主机
tcpdump host 192.168.1.1

# 抓取指定端口
tcpdump port 80

# 抓取并保存
tcpdump -i eth0 -w capture.pcap

# 读取抓包文件
tcpdump -r capture.pcap

# 组合条件
tcpdump -i eth0 'host 192.168.1.1 and port 80'

# 显示详细内容
tcpdump -i eth0 -A port 80

常用过滤表达式

# 指定源/目的
tcpdump src 192.168.1.1
tcpdump dst 192.168.1.1

# 指定协议
tcpdump icmp
tcpdump tcp
tcpdump udp

# 组合
tcpdump 'tcp port 80 and host 192.168.1.1'

六、网络流量监控

iftop

# 安装
yum install iftop

# 使用
iftop -i eth0

# 显示端口
iftop -P

nethogs

按进程显示流量:

# 安装
yum install nethogs

# 使用
nethogs eth0

iptraf-ng

# 安装
yum install iptraf-ng

# 使用
iptraf-ng

七、网络配置

ip命令(推荐)

# 查看IP地址
ip addr
ip a

# 查看路由表
ip route
ip r

# 查看邻居表(ARP)
ip neigh

# 添加/删除IP
ip addr add 192.168.1.100/24 dev eth0
ip addr del 192.168.1.100/24 dev eth0

# 启用/禁用网卡
ip link set eth0 up
ip link set eth0 down

ifconfig(旧命令)

ifconfig
ifconfig eth0

route

# 查看路由表
route -n

# 添加路由
route add -net 10.0.0.0/8 gw 192.168.1.1

# 删除路由
route del -net 10.0.0.0/8

八、防火墙排查

iptables

# 查看规则
iptables -L -n
iptables -L -n -v

# 查看NAT规则
iptables -t nat -L -n

# 临时清空规则(测试用)
iptables -F

firewalld(CentOS 7+)

# 查看状态
firewall-cmd --state

# 查看开放端口
firewall-cmd --list-ports

# 开放端口
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --reload

# 关闭防火墙(测试用)
systemctl stop firewalld

九、实战案例

案例1:服务无法访问

# 1. 检查服务是否启动
ss -tlnp | grep 8080

# 2. 检查防火墙
iptables -L -n | grep 8080
firewall-cmd --list-ports

# 3. 本地测试
curl localhost:8080

# 4. 远程测试
telnet 服务器IP 8080

案例2:网络延迟高

# 1. ping测试基础延迟
ping -c 100 目标IP

# 2. mtr看每一跳延迟
mtr 目标IP

# 3. 抓包分析
tcpdump -i eth0 host 目标IP

案例3:DNS解析慢

# 1. 测试DNS响应时间
time dig example.com

# 2. 对比不同DNS
time dig @8.8.8.8 example.com
time dig @114.114.114.114 example.com

# 3. 检查本地DNS配置
cat /etc/resolv.conf

十、远程网络排查

服务器在机房,网络问题经常需要实时排查。

我用星空组网工具把本地和服务器连起来,直接SSH上去操作:

ssh root@192.168.188.10
tcpdump -i eth0 port 80
mtr 目标地址

实时抓包、看延迟,比让机房同事帮忙操作高效多了。tcpdump抓的包还能直接scp到本地用Wireshark分析。


命令速查表

场景 命令
测试连通性 ping, telnet, nc
路由追踪 traceroute, mtr
DNS排查 dig, nslookup
端口查看 ss -tlnp, netstat -tlnp
抓包分析 tcpdump
流量监控 iftop, nethogs
网络配置 ip addr, ip route
防火墙 iptables -L, firewall-cmd

有问题评论区交流~


posted @ 2025-12-09 11:06  花宝宝  阅读(254)  评论(0)    收藏  举报