检查物理连接
ip link show # 查看网卡状态
ethtool eth0 # 检查网卡状态和速度
验证IP配置
ip addr show # 查看IP地址配置
ip route show # 查看路由表
测试DNS解析
dig example.com # 测试DNS解析
nslookup example.com
检查防火墙设置
iptables -L -n # 查看iptables规则
firewall-cmd --list-all # 查看firewalld配置
检查服务是否监听
netstat -tulnp # 查看监听端口
ss -tulnp # 更现代的替代命令
测试端口连通性
telnet 192.168.1.1 80 # 测试TCP端口
nc -zv 192.168.1.1 80 # 使用netcat测试
检查防火墙规则
iptables -L -n --line-numbers # 查看详细规则
检查SELinux状态
getenforce # 查看SELinux状态
setenforce 0 # 临时关闭(仅用于测试)
基本网络测试
ping -c 10 8.8.8.8 # 测试基本连通性和延迟
traceroute 8.8.8.8 # 跟踪路由路径
mtr 8.8.8.8 # 更高级的路由跟踪工具
检查带宽使用
iftop -i eth0 # 实时查看带宽使用情况
nload eth0 # 另一种带宽监控工具
调整TCP参数
sysctl -a | grep net.ipv4.tcp # 查看当前TCP参数
# 临时调整示例
sysctl -w net.ipv4.tcp_window_scaling=1
sysctl -w net.core.rmem_max=16777216
检查路由表
ip route show # 查看完整路由表
route -n # 传统方式查看路由
添加/删除路由
ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0
ip route del 192.168.2.0/24
检查策略路由
ip rule list # 查看路由策略
检查DNS配置
cat /etc/resolv.conf # 查看DNS服务器配置
测试DNS解析
dig @8.8.8.8 example.com # 指定DNS服务器测试
host example.com
检查本地hosts文件
cat /etc/hosts # 查看本地主机名映射
清除DNS缓存
# 如果使用systemd-resolved
systemd-resolve --flush-caches
检查当前连接数
ss -s # 查看连接统计
netstat -an | wc -l # 计算总连接数
调整文件描述符限制
ulimit -n # 查看当前限制
# 临时提高限制
ulimit -n 65536
# 永久修改需编辑/etc/security/limits.conf
调整内核参数
sysctl -w net.ipv4.tcp_max_syn_backlog=8192
sysctl -w net.core.somaxconn=8192
检测MTU问题
ping -M do -s 1472 8.8.8.8 # 测试MTU(1472+28=1500)
调整MTU大小
ip link set eth0 mtu 1400 # 临时修改MTU
# 永久修改需编辑网络配置文件
检查IPv6配置
ip -6 addr show # 查看IPv6地址
ip -6 route show # 查看IPv6路由
测试IPv6连通性
ping6 ipv6.google.com
traceroute6 ipv6.google.com
禁用IPv6(如需要)
sysctl -w net.ipv6.conf.all.disable_ipv6=1
sysctl -w net.ipv6.conf.default.disable_ipv6=1
tcpdump - 网络抓包分析
tcpdump -i eth0 -nn port 80 # 捕获80端口流量
tcpdump -i eth0 -nn host 8.8.8.8 # 捕获特定主机流量
wireshark/tshark - 图形化/命令行抓包分析
tshark -i eth0 -f "port 80" -w capture.pcap
strace - 跟踪系统调用
strace -f -e trace=network -p <PID> # 跟踪进程的网络调用
conntrack - 连接跟踪
conntrack -L # 查看NAT连接跟踪表
通过以上方法和工具,可以诊断和解决Linux系统中大多数TCP/IP网络问题。对于复杂问题,可能需要结合多种工具和方法进行分析。