Linux: 网络基础——ip / curl / ssh 与网络诊断
Linux 网络命令是排查"为什么连不上"的利器。从查看 IP 到测试端口连通性,从 DNS 解析到 HTTP 请求,这些命令涵盖了网络排查的全过程。
📋 前置知识:需要先掌握以下内容
- 第3课:终端入门
1. 你将学到
- ip addr/route 查看网络
- ping/mtr 连通性测试
- curl/wget HTTP 请求
- ss/netstat 端口监听
- dig DNS 查询
2. 一个 "网站打不开" 的故事
(1) 痛点:部署了服务但外网访问不了
小明部署了一个 Node.js 服务在 3000 端口,用浏览器访问 http://server-ip:3000 却一直超时。
他先本地验证:
curl http://localhost:3000
# ✅ 正常返回 HTML
然后从外网验证:
# 检查端口是否在监听
ss -tlnp | grep 3000
# LISTEN 0 128 127.0.0.1:3000 0.0.0.0:* ← 只在本地监听!
# Node.js 绑定了 127.0.0.1 而不是 0.0.0.0
(2) 修复绑定地址
小明把 server.listen(3000, '127.0.0.1') 改为 server.listen(3000, '0.0.0.0'),重启后外网访问成功。
PermitRootLogin no 禁止 root 直接 SSH 登录,改用普通用户 + sudo 提权。同时禁用密码登录(PasswordAuthentication no),仅使用密钥认证。
(3) 收益:系统化的排查思路
小明学到了排查问题的顺序:先本地 curl 确认服务正常 → ss 检查监听地址 → 检查防火墙 → curl 外网验证。
3. 知识点讲解
(1) ip——网络配置查看
# 查看 IP 地址
ip addr show
# 或简写
ip a
# 查看路由表
ip route
# 或简写
ip r
# 查看网络接口状态
ip link show
# 启用/禁用接口
sudo ip link set eth0 up
sudo ip link set eth0 down
(2) ping/mtr——连通性测试
# ping(测试网络连通性)
ping google.com # 一直 ping,Ctrl+C 停止
ping -c 4 google.com # 只 ping 4 次
ping -c 4 -i 2 google.com # 2 秒间隔
ping -c 10 -q google.com # 只输出摘要
# mtr(结合 traceroute 和 ping)
mtr google.com # 持续追踪路由路径
mtr -r -c 10 google.com # 报告模式(10 次后输出)
(3) curl/wget——HTTP 请求
# curl(HTTP 请求)
curl https://example.com # GET 请求
curl -I https://example.com # 只看响应头
curl -o page.html https://example.com # 保存到文件
> 💡 **提示**:`curl -v`(verbose 模式)是调试 HTTP 问题的利器——它会显示完整的请求头、响应头和 TLS 握手过程,帮助你定位是 DNS 问题、连接超时、证书错误还是重定向问题。当 `curl` 请求失败时,第一反应应该是加 `-v` 查看详情。
curl -L http://httpbin.org/redirect/3 # 跟随重定向
curl -k https://self-signed.com # 跳过 SSL 验证
curl -X POST -d '{"key":"value"}' -H "Content-Type: application/json" https://api.example.com
# wget(文件下载)
wget https://example.com/file.zip # 下载文件
wget -c https://example.com/big.zip # 断点续传
wget -r -np https://example.com/docs/ # 递归下载
(4) ss/netstat——端口监听
# ss(推荐,更快)
ss -tlnp # TCP 监听端口
ss -ulnp # UDP 监听端口
ss -tlnp | grep 80 # 过滤端口 80
ss -tuna # 所有 TCP/UDP 连接
# netstat(传统,需安装)
netstat -tlnp
netstat -i # 网络接口统计
(5) dig——DNS 查询
# dig
dig example.com # 完整 DNS 查询
dig +short example.com # 只输出 IP
dig example.com A # 查询 A 记录
dig example.com MX # 查询邮件记录
dig @8.8.8.8 example.com # 使用指定 DNS 服务器
# 其他 DNS 工具
nslookup example.com # 简单查询
host example.com # 更简洁
ℹ️ 说明:
dig、nslookup、host都能查询 DNS 记录,但输出格式不同——dig输出最详细(包含查询耗时、服务器等),nslookup较简洁,host最精简(只有 IP)。排查 DNS 问题时用dig,快速查看 IP 时用host或dig +short。
▶ 示例:查看本机网络配置
# 查看所有 IP 地址
ip -br addr
# lo UNKNOWN 127.0.0.1/8
# eth0 UP 192.168.1.100/24
# 查看默认网关
ip route | grep default
# default via 192.168.1.1 dev eth0
# 查看 DNS
cat /etc/resolv.conf
# nameserver 8.8.8.8
# nameserver 1.1.1.1
输出:
TEXT 📖 仅展示lo UNKNOWN 127.0.0.1/8 eth0 UP 192.168.1.100/24 default via 192.168.1.1 dev eth0 nameserver 8.8.8.8 nameserver 1.1.1.1
▶ 示例:网络连通性排查
# 1. 先 ping 网关(确认局域网通)
ping -c 2 192.168.1.1
# 2. 再 ping 外网(确认互联网通)
ping -c 2 8.8.8.8
# 3. DNS 解析(确认域名解析正常)
dig +short google.com
# 4. 端口连通性(确认服务端口开放)
nc -zv google.com 80
# Connection to google.com (142.250.80.14) 80 port [tcp/http] succeeded!
# 5. HTTP 请求(确认服务正常)
curl -I https://example.com
输出:
TEXT 📖 仅展示PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data. 64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.423 ms --- 192.168.1.1 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=12.3 ms --- 8.8.8.8 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss 142.250.80.14 Connection to google.com (142.250.80.14) 80 port [tcp/http] succeeded! HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Server: ECS (dcb/7F84)
▶ 示例:curl 测试 API
# GET 请求
curl https://api.github.com/users/octocat
# 查看响应头
curl -I https://httpbin.org
# POST JSON 数据
curl -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d '{"name":"Alice","role":"developer"}'
# 带认证的请求
curl -u "username:token" https://api.github.com/user
# 测试 HTTP 状态码
curl -s -o /dev/null -w "%{http_code}" https://example.com
输出:
TEXT 📖 仅展示{"login":"octocat","id":583231,"avatar_url":"https://avatars.githubusercontent.com/u/583231?v=4"} HTTP/1.1 200 OK Server: gunicorn/19.9.0 Content-Type: application/json { "json": { "name": "Alice", "role": "developer" } } 200
▶ 示例:端口排查
# 查看所有监听中的 TCP 端口
ss -tlnp
# 查看端口 3306(MySQL)是否在监听
ss -tlnp | grep 3306
# 查看指定进程的端口
ss -tlnp | grep nginx
# 测试远程端口是否开放
echo "测试 80 端口..."
timeout 3 bash -c "echo >/dev/tcp/example.com/80" && echo "开放" || echo "关闭"
# 查看连接状态
ss -tuna | grep ESTAB
输出:
TEXT 📖 仅展示State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:80 0.0.0.0:* LISTEN 0 128 0.0.0.0:443 0.0.0.0:* LISTEN 0 128 *:3306 *:* LISTEN 0 511 *:80 *:* users:(("nginx",pid=1234,fd=6)) 开放 ESTAB 0 0 192.168.1.100:22 10.0.0.50:52341 ESTAB 0 0 192.168.1.100:443 172.16.0.5:49832
▶ 示例:下载文件
# wget 下载
wget -c https://releases.ubuntu.com/22.04/ubuntu-22.04.3-live-server-amd64.iso
# curl 下载
curl -L -o ubuntu.iso https://releases.ubuntu.com/22.04/ubuntu-22.04.3-live-server-amd64.iso
# 下载并解压
curl -L https://github.com/user/repo/archive/main.tar.gz | tar -xz
输出:
TEXT 📖 仅展示--2026-07-07 10:23:01-- https://releases.ubuntu.com/22.04/ubuntu-22.04.3-live-server-amd64.iso Resolving releases.ubuntu.com... 185.125.190.37 HTTP request sent, awaiting response... 200 OK Length: 2014838784 (1.9G) [application/x-iso9660-image] Saving to: 'ubuntu-22.04.3-live-server-amd64.iso' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 45% 891M 0 0 891M 0 0 12.3M 0:00:02 0:00:01 0:00:01 12.3M
▶ 综合示例:网络故障排查脚本
#!/bin/bash
# net-diag.sh - 网络诊断工具
TARGET="${1:-google.com}"
echo "========================================"
echo " 网络诊断报告"
echo " 时间: $(date)"
echo "========================================"
echo ""
echo "1. 本机网络配置"
echo "----------------"
ip -br addr 2>/dev/null || ifconfig
echo ""
echo "2. 默认网关"
echo "-----------"
ip route | grep default
echo ""
echo "3. DNS 服务器"
echo "--------------"
cat /etc/resolv.conf | grep nameserver
echo ""
echo "4. DNS 解析 ($TARGET)"
echo "----------------------"
dig +short "$TARGET" 2>/dev/null || nslookup "$TARGET" 2>/dev/null || host "$TARGET"
echo ""
echo "5. 网络连通性"
echo "--------------"
ping -c 4 -q "$TARGET" 2>&1 | tail -3
echo ""
echo "6. HTTP 响应"
echo "-------------"
curl -s -o /dev/null -w "HTTP %{http_code}, 耗时 %{time_total}s\n" "https://$TARGET" --connect-timeout 5
echo ""
echo "7. 路由追踪"
echo "------------"
mtr -r -c 3 "$TARGET" 2>/dev/null | tail -5 || echo "mtr 未安装"
echo ""
echo "8. 监听端口"
echo "-----------"
ss -tlnp 2>/dev/null | head -10 || netstat -tlnp 2>/dev/null | head -10
echo ""
echo "========================================"
❓ 常见问题
Q: ping 不通一定是网络问题吗? A: 不一定。很多服务器防火墙禁 ping(ICMP)。ping 不通但 HTTP 能访问是完全正常的。替代方案:
nc -zv host port或curl -I测试具体服务端口。
Q: curl 和 wget 有什么区别? A: curl 功能更丰富(支持所有 HTTP 方法、自定义头、JSON 数据),适合 API 测试。wget 下载功能更强(递归下载、断点续传、镜像站点)。日常 API 测试用 curl,下载文件用 wget。
Q: ss 和 netstat 哪个更好? A: \1
Q: 端口 80 和 443 有什么区别? A: 80 是 HTTP(明文),443 是 HTTPS(加密)。现代网站基本上只用 443。如果
curl http://...不行,试试curl https://...。
Q: SSH 连接慢怎么办? A: 常见原因:1)DNS 反向查询(配置
UseDNS no)。2)GSSAPI 认证(配置GSSAPIAuthentication no)。3)网络延迟。快速修复:在~/.ssh/config中加入UseDNS no、GSSAPIAuthentication no。
📖 小节
ip addr/ip route查看 IP 和路由ping -c 4测试连通性、mtr路由追踪curl -I看响应头、curl -X POST -d '{}'发请求ss -tlnp查看监听端口dig +short快速 DNS 查询
📝 作业
- 基础题(难度⭐):用
ip -br addr查看本机 IP 和网络接口,然后用ping -c 4 google.com测试网络并分析结果 - 进阶题(难度⭐⭐):用
ss -tlnp查看本机所有监听端口,然后用curl -I https://example.com查看 HTTP 响应头,用dig +short github.com查询 GitHub 的 IP 地址 - 挑战题(难度⭐⭐⭐):写一个网络诊断脚本,自动检测本机 IP、网关、DNS、连通性、HTTP 响应和监听端口,输出格式化报告,当检测到异常时用颜色高亮警告