Linux网络命令详解 – ping/curl/wget/netstat网络排查指南
a
admin
作者
2026-07-15
发布日期
约 11 分钟
阅读时间
Linux 网络命令是排查网络问题、监控流量的必备工具。无论是服务器运维还是日常开发,这几个命令都要熟练。
ping:测试网络连通性
# 基础测试
ping example.com
# 指定次数(4次后停止)
ping -c 4 example.com
# 指定包大小
ping -s 1000 example.com
# 指定超时时间(秒)
ping -W 5 example.com
# 持续 ping 并记录时间戳
ping -i 1 example.com | while read line; do echo "$(date): $line"; done
实战:测试服务器到 DNS 服务器的连通性:
ping -c 4 8.8.8.8
注意:有些服务器禁 ICMP,ping 不通不代表服务不可用,要用 telnet 或 curl 测试端口。
curl:HTTP 请求
功能强大的 HTTP 客户端,调试接口必备。
# GET 请求
curl https://example.com
# 只看响应头
curl -I https://example.com
# 显示详细通信过程(调试连接问题)
curl -v https://example.com
# 跟随重定向
curl -L https://example.com
# POST 请求
curl -X POST https://example.com/api
# 发送 JSON 数据
curl -X POST https://example.com/api \
-H "Content-Type: application/json" \
-d '{"name":"test","value":1}'
# 发送表单数据
curl -X POST https://example.com/form \
-d "name=test&email=test@example.com"
# 带认证
curl -u user:password https://example.com/api
# 带自定义 Header
curl -H "Authorization: Bearer token123" https://example.com/api
# 下载文件
curl -o file.zip https://example.com/file.zip
# 限制请求时间(秒)
curl --max-time 10 https://example.com
# 只返回 HTTP 状态码
curl -o /dev/null -s -w "%{http_code}\n" https://example.com
实战:检查网站的 SSL 证书信息:
curl -vI https://www.sslmianfei.com 2>&1 | grep -E "subject|issuer|expire"
wget:下载文件
比 curl 更适合大文件下载,支持断点续传。
# 基础下载
wget https://example.com/file.zip
# 指定保存文件名
wget -O myfile.zip https://example.com/file.zip
# 断点续传
wget -c https://example.com/large_file.iso
# 后台下载
wget -b https://example.com/large_file.iso
# 限速下载(避免占满带宽)
wget --limit-rate=1M https://example.com/file.zip
# 递归下载整个网站(慎用)
wget -r -l 2 https://example.com/
# 镜像网站
wget --mirror https://example.com/
netstat / ss:网络连接查看
查看网络连接、端口监听状态。
# 查看所有连接
netstat -an
# 查看监听端口
netstat -tlnp
# 查看指定端口
netstat -tlnp | grep :80
# 查看所有 TCP 连接
netstat -ant
# 查看连接并显示进程
netstat -tnp
# 统计各状态连接数
netstat -an | awk '/tcp/ {print $NF}' | sort | uniq -c
ss 是 netstat 的现代替代品,速度更快:
# 查看监听端口
ss -tlnp
# 查看指定端口
ss -tlnp | grep :80
# 查看所有 TCP 连接
ss -ant
# 查看已建立连接
ss -tn state established
telnet:端口连通性测试
# 测试端口是否开放
telnet example.com 80
# 成功:进入交互界面(按 Ctrl+] 退出)
# 失败:connection refused
更现代的替代方案:
# nc (netcat)
nc -zv example.com 80
# 批量测试多个端口
for port in 80 443 8080; do nc -zv example.com $port; done
traceroute:追踪路由
查看数据包到目标服务器的路径,排查网络延迟和中断位置。
# 追踪路由
traceroute example.com
# 指定最大跳数
traceroute -m 15 example.com
# 指定每跳探测次数
traceroute -q 1 example.com
更现代的替代:mtr,结合 ping 和 traceroute,实时显示:
# 安装
apt install mtr
# 使用(实时刷新)
mtr example.com
# 报告模式
mtr --report example.com
dig / nslookup:DNS 查询
# 查询 A 记录
dig example.com
# 查询指定记录类型
dig example.com MX
dig example.com CNAME
dig example.com TXT
# 指定 DNS 服务器查询
dig @8.8.8.8 example.com
# 只看 IP 地址
dig +short example.com
# 反向查询
dig -x 8.8.8.8
# nslookup 用法
nslookup example.com
nslookup example.com 8.8.8.8
实战:检查域名的 DNS 是否已生效:
dig +short example.com
# 等输出IP和你在DNS服务商配置的一致就说明生效了
ifconfig / ip:网络接口
# 查看所有网络接口
ifconfig -a
# 查看指定接口
ifconfig eth0
# 现代 Linux 推荐 ip 命令
ip addr show
ip addr show eth0
# 启用/禁用接口
ip link set eth0 up
ip link set eth0 down
# 查看路由表
ip route
route -n
实用组合示例
查看当前服务器的外网 IP
curl ifconfig.me
# 或
curl ip.sb
检查网站是否正常
# 检查 HTTP 状态码
curl -o /dev/null -s -w "%{http_code}\n" https://www.sslmianfei.com
# 检查 SSL 证书有效期
echo | openssl s_client -servername www.sslmianfei.com -connect www.sslmianfei.com:443 2>/dev/null | openssl x509 -noout -dates
统计并发连接数
ss -ant | grep ESTAB | wc -l
找出连接最多的 IP
ss -ant | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head -10
总结
| 命令 | 作用 | 典型场景 |
|——|——|———|
| ping | 连通性测试 | 排查能否访问 |
| curl | HTTP 请求 | 调试接口 |
| wget | 下载文件 | 下载大文件 |
| ss/netstat | 网络连接 | 查端口占用 |
| telnet/nc | 端口测试 | 查端口开放 |
| dig | DNS 查询 | 查域名解析 |
| traceroute | 追踪路由 | 查网络中断点 |
| ip | 网络接口 | 配置网卡 |
网络出问题的排查顺序:ping 测连通 → dig 查 DNS → telnet/nc 测端口 → curl 测 HTTP → traceroute 查路由。
—
本文到此结束
—