curl 命令详解

简介

curl 是一个基于 URL 语法 的命令行数据传输工具,支持 HTTP/HTTPS/FTP/TELNET 等多种协议。是最常用的 API 调试和网络排查工具之一。

命令解析:端口可达性测试

curl -v --connect-timeout 3 telnet://172.20.0.7:53
部分含义
curl基础命令
-v (—verbose)输出详细连接过程(DNS 解析、TCP 握手、TLS 握手、请求/响应头)
--connect-timeout 3连接超时 3 秒
telnet://172.20.0.7:53使用 telnet 协议建立裸 TCP 连接到 172.20.0.7:53(DNS 端口)

此命令等价于 nc -zv -w 3 172.20.0.7 53,都用于测试 TCP 端口是否开放。参阅 nc-netcat命令详解

常用参数

连接控制

参数说明示例
--connect-timeout <秒>连接超时--connect-timeout 5
--max-time <秒>整个传输最大时间--max-time 30
--retry <次数>失败重试次数--retry 3
-x/--proxy <地址>使用代理-x http://127.0.0.1:7890

输出与调试

参数说明示例
-v详细模式curl -v https://example.com
-i输出含响应头curl -i https://api.example.com
-I只发 HEAD 请求curl -I https://example.com
-s静默模式(不显示进度/错误)curl -s https://api.example.com
-S-s 配合只显错误curl -sS https://api.example.com
-w/--write-out输出格式化统计信息curl -w "\n%{http_code}\n" url

请求控制

参数说明示例
-X/--request <方法>指定 HTTP 方法-X POST / -X PUT
-H/--header添加请求头-H "Authorization: Bearer xxx"
-d/--data发送 POST 数据-d '{"key":"value"}'
-F/--form模拟表单提交-F "file=@photo.jpg"
-b/--cookie发送 Cookie-b "session=abc123"
-c/--cookie-jar保存返回的 Cookie-c cookies.txt
-A/--user-agent自定义 UA-A "Mozilla/5.0 ..."

认证与安全

参数说明示例
-u/--userHTTP 基本认证-u admin:password
-k/--insecure跳过 TLS 验证-k https://self-signed.local
--cacert指定 CA 证书--cacert /path/ca.pem
--cert客户端证书--cert client.p12:password

文件操作

参数说明示例
-o/--output保存到文件-o file.zip
-O用 URL 文件名保存-O https://example.com/file.zip
-C/--continue-at断点续传-C - -O https://example.com/bigfile.zip
--limit-rate限速--limit-rate 100K

核心调试指标(-w

curl -w "\n\
  time_namelookup:   %{time_namelookup}s\n\
  time_connect:      %{time_connect}s\n\
  time_appconnect:   %{time_appconnect}s\n\
  time_starttransfer: %{time_starttransfer}s\n\
  time_total:        %{time_total}s\n" \
  -o /dev/null -s https://example.com
变量含义排查方向
time_namelookupDNS 解析耗时DNS 服务器慢 / 配置问题
time_connectTCP 三次握手耗时网络延迟 / 防火墙
time_appconnectTLS 握手耗时(HTTPS)证书链 / 加密协商
time_starttransfer首字节到达时间(TTFB)服务器端性能
time_total总耗时综合评估

telnet:// vs http:// 的区别

telnet://host:porthttp://host:port
行为只建 TCP 连接,不发数据发送完整 HTTP 请求
用途测试端口可达性测试 HTTP 服务本身
退出方式等待 stdin 输入,需手动退出请求完成后自动退出

常见场景

API 调试

# GET + 查看响应头
curl -i https://api.github.com/users/octocat
 
# POST JSON
curl -X POST https://api.example.com/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"123456"}'
 
# 仅看状态码
curl -s -o /dev/null -w "%{http_code}" https://example.com

网络排查

# 端口可达性测试
curl -v --connect-timeout 3 telnet://172.20.0.7:53
 
# 测试代理
curl -v -x http://127.0.0.1:7890 https://www.google.com
 
# 接口性能分析(配合 -w)
curl -s -o /dev/null -w "DNS: %{time_namelookup}s | TCP: %{time_connect}s | TTFB: %{time_starttransfer}s | Total: %{time_total}s\n" https://api.example.com/health

文件操作

# 断点续传
curl -C - -O https://releases.ubuntu.com/22.04/ubuntu-22.04-desktop-amd64.iso
 
# 限速下载
curl --limit-rate 500K -O https://example.com/bigfile.zip
 
# 上传文件
curl -F "file=@/path/to/photo.jpg" -F "description=vacation" \
  https://api.example.com/upload

相关笔记