网络问题是VPS使用中最常见的困扰。搬瓦工VPS提供多条线路(CN2 GT、CN2 GIA等),遇到连接慢、丢包、DNS解析失败等问题时,需要借助网络诊断工具定位原因。本文整理9个常用的Linux网络工具,从基础到进阶逐一讲解。
ping 是最基础的网络工具,通过发送 ICMP 包来测试目标是否可达以及网络延迟。
# 基本使用 ping google.com # 指定发送次数 ping -c 4 google.com # 指定间隔(秒) ping -i 0.5 google.com # 指定数据包大小 ping -s 1024 google.com
关键指标:
如果国内 ping 不通但国外正常,大概率是 IP 被封。可参考更换IP教程处理。
traceroute 显示数据包从源到目标经过的每一跳路由,用于定位哪个节点出问题。
# 安装 apt install traceroute -y # Ubuntu/Debian yum install traceroute -y # CentOS # 基本使用 traceroute google.com # 使用TCP模式(部分网络屏蔽ICMP) traceroute -T google.com # 使用ICMP模式 traceroute -I google.com
输出中 * * * 表示该节点不回应,不一定是故障;如果连续多个节点超时,则该段路由可能有问题。
mtr 结合了 ping 和 traceroute 的功能,持续发送探测包并实时显示每一跳的延迟和丢包率,是诊断线路质量最好的工具。
# 安装 apt install mtr-tiny -y # Ubuntu/Debian yum install mtr -y # CentOS # 实时模式 mtr google.com # 报告模式(发送100个包后输出报告) mtr -r -c 100 google.com # 同时显示IP和域名 mtr -b google.com
mtr 报告关键字段:
| 字段 | 说明 |
|---|---|
| Loss% | 丢包率,最重要的指标 |
| Snt | 发送的探测包数量 |
| Last | 最近一次延迟 |
| Avg | 平均延迟 |
| Best | 最低延迟 |
| Wrst | 最高延迟 |
| StDev | 标准差(越小越稳定) |
在向搬瓦工客服反馈网络问题时,附上双向 mtr 报告(本地到VPS和VPS到本地)会更有效。更多关于mtr的使用可参考MTR路由追踪教程。
dig 是功能最全面的 DNS 查询工具,用于排查域名解析问题。
# 安装(通常自带,没有则安装) apt install dnsutils -y # Ubuntu/Debian yum install bind-utils -y # CentOS # 查询A记录 dig example.com A # 查询MX记录 dig example.com MX # 查询所有记录 dig example.com ANY # 指定DNS服务器查询 dig @8.8.8.8 example.com # 简洁输出 dig +short example.com # 追踪DNS解析过程 dig +trace example.com
nslookup 比 dig 简单,适合快速查询域名解析结果。
# 查询域名 nslookup example.com # 指定DNS服务器 nslookup example.com 8.8.8.8 # 查询特定记录类型 nslookup -type=MX example.com
ss 是 netstat 的替代工具,速度更快,是查看端口监听和连接状态的首选。
# 查看所有TCP监听端口 ss -tlnp # 查看所有TCP连接 ss -tnp # 查看所有UDP端口 ss -ulnp # 统计各状态的连接数 ss -s # 查看特定端口的连接 ss -tnp | grep :80
参数说明:
| 参数 | 说明 |
|---|---|
-t | TCP连接 |
-u | UDP连接 |
-l | 仅显示监听状态 |
-n | 不解析服务名(显示端口号) |
-p | 显示进程信息 |
-a | 显示所有连接 |
netstat 是经典的网络工具,部分新系统已不再默认安装,但仍然广泛使用。
# 安装
apt install net-tools -y # Ubuntu/Debian
yum install net-tools -y # CentOS
# 查看监听端口
netstat -tlnp
# 查看所有连接
netstat -anp
# 统计各连接状态
netstat -n | awk '/^tcp/ {print $6}' | sort | uniq -c | sort -rn
tcpdump 是命令行抓包工具,可以捕获和分析网络数据包,是排查复杂网络问题的终极工具。
# 抓取指定接口的所有流量 tcpdump -i eth0 # 抓取指定端口的流量 tcpdump -i eth0 port 80 # 抓取指定主机的流量 tcpdump -i eth0 host 192.168.1.1 # 抓取并保存到文件(可用Wireshark分析) tcpdump -i eth0 -w capture.pcap # 只抓取TCP SYN包(新连接) tcpdump -i eth0 'tcp[tcpflags] & (tcp-syn) != 0' # 限制抓包数量 tcpdump -i eth0 -c 100 port 443
注意:tcpdump 需要 root 权限运行。抓包时注意数据隐私,不要长时间抓取全量流量。
curl 不仅能下载文件,还是测试HTTP服务和API的利器。
# 查看HTTP响应头
curl -I https://bwh81.net
# 查看完整请求和响应过程
curl -v https://bwh81.net
# 测量连接各阶段耗时
curl -o /dev/null -s -w "DNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTLS: %{time_appconnect}s\nTotal: %{time_total}s\n" https://bwh81.net
# 指定请求方法和数据
curl -X POST -d "key=value" https://example.com/api
# 跟随重定向
curl -L https://example.com
# 查看外网IP
curl ifconfig.me
| 问题场景 | 建议使用工具 | 诊断思路 |
|---|---|---|
| VPS无法访问 | ping + mtr | 先ping确认是否可达,mtr定位丢包节点 |
| 网站打不开 | curl + ss | curl测试HTTP响应,ss确认Web服务是否监听 |
| 域名不解析 | dig + nslookup | 检查DNS记录是否正确配置 |
| 连接速度慢 | mtr + curl | mtr查线路质量,curl测量各阶段耗时 |
| 端口不通 | ss + tcpdump | ss确认服务监听,tcpdump检查数据包是否到达 |
| IP是否被封 | ping(国内外对比) | 国内不通国外通,基本确认被封 |
Tip: 购买搬瓦工VPS时使用优惠码 NODESEEK2026 可享 6.77% 折扣,详见优惠码使用教程。方案汇总见搬瓦工全部在售方案。