从 DNS 到端口网络问题排查指南

随笔2026-04-202 分钟dns网络排查命令行故障排查

按顺序排查:DNS -> 连通性 -> 路由 -> 服务 -> 端口。把 example.com 换成你的域名或 IP。


默认起手式

dig example.com +short
ping -c 4 example.com        # Windows: ping -n 4 example.com
curl -I https://example.com
nc -zv example.com 443
现象下一步
dig 没 IP先处理 DNS(对比公共 DNS、清缓存),别急着查服务
ping 不通但 curl 正常常见:对方禁 ICMP,ping 不能当结论
curlHTTP/...说明至少已到服务层;再看状态码
nc refused端口没监听或服务拒绝
nc timed out更像防火墙/安全组/链路问题
很慢、时好时坏traceroute / mtr

命令速查

DNS:dig / nslookup

dig example.com +short
dig @8.8.8.8 example.com +short
dig @1.1.1.1 example.com +short

nslookup example.com
nslookup example.com 8.8.8.8

清缓存:

# macOS
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

# Linux(常见)
sudo systemctl restart systemd-resolved

# Windows
ipconfig /flushdns

连通性:ping

ping -c 4 example.com        # macOS / Linux
ping -n 4 example.com        # Windows

路由:traceroute / mtr(慢、丢包时用)

traceroute example.com       # macOS / Linux
tracert example.com          # Windows

mtr -n -c 100 example.com

服务:curl

curl -I https://example.com
curl -I -L http://example.com
curl -v https://example.com
curl -I --resolve example.com:443:93.184.216.34 https://example.com

API(按需):

curl -H "Authorization: Bearer <token>" https://api.example.com/user
curl -X POST -H "Content-Type: application/json" -d '{"name":"Tom"}' https://api.example.com/users

状态码:

含义
200成功
301 / 302重定向
4xx请求/权限类问题
5xx服务端或网关问题

端口:nc(优先)/ telnet / nmap

nc -zv example.com 443
nc -zv -w 2 example.com 443

telnet example.com 80

nmap -p 80,443 example.com

nmap 不要随意扫他人机器。

本机:网卡与监听

ifconfig                     # macOS / 部分 Linux
ip addr                      # Linux
ipconfig                     # Windows

lsof -i :8080
ss -tlnp                     # Linux
netstat -an | grep LISTEN

公网出口 / 内网 IP

curl -s ifconfig.me
curl -s checkip.amazonaws.com

# 走了 VPN/代理 时,不同方式看到的出口 IP 可能不一致,属正常。
# 下面是国内的ip检测小工具,移动端可以直接通过网页访问
curl -s https://www.epoos.com/ip | grep user_ip | grep -oE '[0-9.]+'
# 查询内网ip
ifconfig | awk '/inet / && !/127.0.0.1/ {print $2}' | head -n1

易混点(各一句)

  • ping 不通不等于站点挂了,用 curl / nc 看业务端口。
  • 404 多半是路径错,网络通常已通。
  • refusedtimeout 含义不同:前者像“没人接”,后者像“路上被拦或到不了”。