Linux CURL 教程

curl 是 Linux 下最强大的网络调试工具之一,支持 HTTP、HTTPS、FTP、SMTP 等数十种协议。本文从入门到进阶,帮你彻底掌握 curl。


一、基础用法

1. 发送 GET 请求

bash 复制代码
# 最简单的 GET 请求
curl https://api.example.com/users

# 显示响应头
curl -i https://api.example.com/users

# 只显示响应头
curl -I https://api.example.com/users

2. 发送 POST 请求

bash 复制代码
# 表单数据 (application/x-www-form-urlencoded)
curl -X POST -d "name=John&age=25" https://api.example.com/users

# JSON 数据 (application/json)
curl -X POST -H "Content-Type: application/json" \
  -d '{"name":"John","age":25}' \
  https://api.example.com/users

# 从文件读取数据
curl -X POST -d @data.json https://api.example.com/users

3. 常用请求方法

bash 复制代码
curl -X PUT https://api.example.com/users/1
curl -X DELETE https://api.example.com/users/1
curl -X PATCH https://api.example.com/users/1

二、高级功能

1. 请求头操作

bash 复制代码
# 自定义多个 Header
curl -H "Authorization: Bearer token123" \
     -H "User-Agent: MyApp/1.0" \
     https://api.example.com/data

# 设置 Referer
curl -e "https://google.com" https://example.com

2. 处理 Cookies

bash 复制代码
# 保存 Cookie 到文件
curl -c cookies.txt https://example.com/login

# 发送 Cookie
curl -b "name=value" https://example.com
curl -b cookies.txt https://example.com/dashboard

3. 处理重定向

bash 复制代码
# 自动跟随重定向
curl -L https://example.com

# 限制最大重定向次数
curl -L --max-redirs 5 https://example.com

4. 认证

bash 复制代码
# 基本认证
curl -u username:password https://api.example.com

# Bearer Token
curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com

# API Key (Query 参数)
curl "https://api.example.com?api_key=YOUR_KEY"

三、调试利器

1. 详细输出

bash 复制代码
# 显示请求和响应的完整细节
curl -v https://example.com

# 更详细的输出(包含协议细节)
curl --trace - https://example.com
curl --trace-ascii - https://example.com

2. 查看耗时

bash 复制代码
# 显示每个阶段的耗时
curl -w "@curl-format.txt" -o /dev/null -s https://example.com

# 创建 curl-format.txt
cat > curl-format.txt << EOF
     time_namelookup:  %{time_namelookup}s\n
        time_connect:  %{time_connect}s\n
     time_appconnect:  %{time_appconnect}s\n
    time_pretransfer:  %{time_pretransfer}s\n
       time_redirect:  %{time_redirect}s\n
  time_starttransfer:  %{time_starttransfer}s\n
                     ----------\n
          time_total:  %{time_total}s\n
EOF

# 单行命令版本
curl -w "DNS解析: %{time_namelookup}s\n连接: %{time_connect}s\n传输: %{time_starttransfer}s\n总耗时: %{time_total}s\n" \
  -o /dev/null -s https://example.com

3. 限制带宽和超时

bash 复制代码
# 限制下载速度(1MB/s)
curl --limit-rate 1M https://example.com/file.zip

# 连接超时(秒)
curl --connect-timeout 5 https://example.com

# 总超时(秒)
curl --max-time 30 https://example.com

# 重试机制
curl --retry 3 --retry-delay 2 https://unstable-api.com

四、文件操作

1. 下载文件

bash 复制代码
# 下载并保存为原文件名
curl -O https://example.com/file.zip

# 下载并指定新文件名
curl -o myfile.zip https://example.com/file.zip

# 断点续传
curl -C - -O https://example.com/largefile.zip

# 同时下载多个文件
curl -O https://example.com/file1.zip -O https://example.com/file2.zip

2. 上传文件

bash 复制代码
# 上传文件(multipart/form-data)
curl -F "file=@/path/to/local/file.txt" https://example.com/upload

# 上传多个文件
curl -F "file1=@/path/to/file1.txt" -F "file2=@/path/to/file2.txt" \
  https://example.com/upload

# 上传并指定文件名
curl -F "file=@localfile.txt;filename=remotename.txt" \
  https://example.com/upload

五、网络诊断实战

1. 检查 API 健康状态

bash 复制代码
# 检查 HTTP 状态码
curl -o /dev/null -s -w "%{http_code}\n" https://api.example.com/health

# 检查 SSL 证书
curl -vI https://example.com 2>&1 | grep -E "subject|expire"

# 测试 HTTPS 连接
curl --insecure https://self-signed.badssl.com  # 跳过证书验证
curl --cert /path/to/client.crt https://example.com  # 客户端证书

2. 代理和隧道

bash 复制代码
# HTTP 代理
curl -x proxy.example.com:8080 https://api.example.com

# 带认证的代理
curl -x user:pass@proxy.example.com:8080 https://api.example.com

# SOCKS5 代理
curl --socks5 localhost:9050 https://check.torproject.org

3. 性能测试

bash 复制代码
# 并发请求测试
for i in {1..10}; do
  curl -s -o /dev/null -w "请求 $i: %{time_total}s\n" \
    https://api.example.com &
done
wait

# 使用 GNU parallel 并发
seq 1 100 | parallel -j 10 "curl -s -o /dev/null -w '%{time_total}\n' https://api.example.com"

4. 调试网络问题

bash 复制代码
# 检查 IP 地址和端口连通性
curl -v telnet://192.168.1.1:22

# 测试 DNS 解析
curl --dns-servers 8.8.8.8 https://example.com

# 指定本地接口
curl --interface eth0 https://example.com

# 绑定本地 IP
curl --local-port 5000-6000 https://example.com

六、高级技巧

1. 解析域名(绕过 DNS)

bash 复制代码
# 将域名解析到指定 IP
curl --resolve example.com:80:127.0.0.1 http://example.com
curl --resolve example.com:443:192.168.1.100 https://example.com

2. 输出格式化

bash 复制代码
# 输出 JSON 并格式化(需要 jq)
curl -s https://api.example.com/users | jq '.'

# 输出到文件并显示进度
curl -o file.zip -L https://example.com/file.zip

3. 构造复杂请求

bash 复制代码
# 使用文件中的数据模板
curl -X POST -H "Content-Type: application/json" \
  -d "$(envsubst < template.json)" \
  https://api.example.com/users

# 发送原始二进制数据
curl --data-binary @image.jpg https://example.com/upload

4. 测试限流和重试

bash 复制代码
# 模拟慢请求
curl --limit-rate 1k --max-time 60 https://example.com

# 检查重试机制
curl --retry 5 --retry-delay 1 --retry-max-time 30 \
  https://unstable-service.com

七、实用别名和函数

添加到 ~/.bashrc~/.zshrc

bash 复制代码
# 获取 HTTP 状态码
alias httpcode='curl -o /dev/null -s -w "%{http_code}\n"'

# 获取响应时间
alias http-time='curl -o /dev/null -s -w "DNS: %{time_namelookup}s\n连接: %{time_connect}s\n传输: %{time_starttransfer}s\n总耗时: %{time_total}s\n"'

# 查看请求头
alias headers='curl -sI'

# 保存完整请求响应
function curldump() {
  curl -v "$1" 2>&1 | tee "curl-debug-$(date +%Y%m%d-%H%M%S).log"
}

# 测试本地服务
function test-local() {
  curl -H "Host: $1" "http://127.0.0.1:${2:-80}"
}

# 批量测试 URL
function bulk-curl() {
  while read url; do
    echo -n "$url: "
    curl -o /dev/null -s -w "%{http_code}\n" "$url"
  done < "$1"
}

八、常见问题排查

1. SSL 证书错误

bash 复制代码
# 忽略证书验证(仅测试用)
curl -k https://self-signed.com

# 使用特定 CA 证书
curl --cacert /path/to/ca.pem https://example.com

2. 连接被拒绝

bash 复制代码
# 检查端口是否开放
curl -v telnet://example.com:80

# 检查防火墙
curl --interface lo http://localhost:8080

3. 慢速响应

bash 复制代码
# 分段计时
curl -w "@curl-format.txt" https://slow-api.com

# 检查 DNS 解析
dig example.com +short

九、快速参考卡片

场景 命令
GET 请求 curl https://api.com
POST JSON curl -X POST -H "Content-Type: application/json" -d '{"key":"val"}' https://api.com
显示响应头 curl -i https://api.com
跟随重定向 curl -L https://bit.ly/xxx
下载文件 curl -O https://file.com/file.zip
详细调试 curl -v https://api.com
超时设置 curl --connect-timeout 5 --max-time 30 https://api.com
认证 curl -u user:pass https://api.com
Cookie curl -b cookies.txt -c cookies.txt https://api.com

掌握 curl 可以让你快速定位 80% 的网络问题。建议从基础命令开始,逐步尝试高级功能,形成自己的调试流程。遇到复杂问题时,-v 参数是你的最佳朋友!

相关推荐
Lumos_7772 小时前
Linux -- 共享内存
java·linux·运维
李日灐2 小时前
<5> Linux 开发工具:包管理 + Vim 实操 + GCC 编译流程 + 静态与动态链接详解
linux·运维·服务器·面试·vim·gcc
我也不曾来过12 小时前
传输层协议UDP和TCP
linux·网络·udp
molihuan2 小时前
最新VMware Ubuntu 1分钟极速安装 植物人教程
linux·ubuntu
sdm0704272 小时前
深刻理解进程信号
linux·运维·服务器
Simonhans2 小时前
Linux安装Bun
linux·bun
数智化精益手记局2 小时前
什么是安全生产?解读安全生产的基本方针与核心要求
大数据·运维·人工智能·安全·信息可视化·自动化·精益工程
70asunflower2 小时前
Ubuntu `tree` 命令完全指南:让目录结构一目了然
linux·数据库·ubuntu
老四啊laosi2 小时前
【Linux系统】16. 进程程序替换
linux·exec·程序替换