ping - 基本延迟测试
ping -c 10 example.com
traceroute/mtr - 路径追踪
mtr --report example.com
tcptraceroute - TCP路径追踪
tcptraceroute example.com 80
tcpdump - 网络包分析
tcpdump -i eth0 -nn -w capture.pcap
netstat/ss - 连接状态检查
ss -tulnp
# 增大TCP窗口大小
echo "net.ipv4.tcp_window_scaling = 1" >> /etc/sysctl.conf
# 启用TCP时间戳
echo "net.ipv4.tcp_timestamps = 1" >> /etc/sysctl.conf
# 调整TCP缓冲区大小
echo "net.ipv4.tcp_rmem = 4096 87380 16777216" >> /etc/sysctl.conf
echo "net.ipv4.tcp_wmem = 4096 65536 16777216" >> /etc/sysctl.conf
# 启用快速回收TIME_WAIT套接字
echo "net.ipv4.tcp_tw_recycle = 1" >> /etc/sysctl.conf
echo "net.ipv4.tcp_tw_reuse = 1" >> /etc/sysctl.conf
# 应用更改
sysctl -p
# 检查当前设置
ethtool eth0
# 禁用节能模式
ethtool --set-eee eth0 eee off
# 启用GRO和LRO
ethtool -K eth0 gro on
ethtool -K eth0 lro on
# 调整队列长度
ifconfig eth0 txqueuelen 10000
# 安装tc工具
apt-get install iproute2 # Debian/Ubuntu
yum install iproute # RHEL/CentOS
# 限制特定端口的带宽(示例)
tc qdisc add dev eth0 root handle 1: htb default 30
tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbit
tc class add dev eth0 parent 1:1 classid 1:10 htb rate 50mbit ceil 100mbit
tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dport 80 0xffff flowid 1:10
# 使用更快的DNS服务器
echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "nameserver 8.8.4.4" >> /etc/resolv.conf
# 安装本地缓存DNS服务器(dnsmasq)
apt-get install dnsmasq
systemctl enable dnsmasq
systemctl start dnsmasq
内核网络栈分析
cat /proc/net/snmp
cat /proc/net/netstat
中断亲和性设置
# 查看中断分布
cat /proc/interrupts
# 设置CPU亲和性
echo 1 > /proc/irq/XX/smp_affinity
NIC offloading检查
ethtool -k eth0
内核网络栈跟踪
perf probe --add tcp_v4_do_rcv
perf record -a -g -e probe:tcp_v4_do_rcv sleep 10
通过以上方法,可以系统地诊断和解决Linux系统中的网络延迟问题。建议从简单的基础检查开始,逐步深入排查复杂问题。