Linux curl 命令完全指南:从入门到精通

Linux curl 命令完全指南:从入门到精通

Someone Lv5

一、curl 是什么

curl(Client URL)是一个功能强大的命令行网络工具,支持 HTTP、HTTPS、FTP、SFTP、SMTP 等多种协议。它被广泛应用于 API 调试、文件传输、网络诊断等场景,是 Linux 运维和开发人员必备的工具之一。

Daniel Stenberg 于 1997 年创建了 curl,如今它已成为全球最广泛使用的网络工具——几乎每个 Linux 发行版都预装了 curl,Windows 10/11 也内置了它。

二、安装与基本使用

2.1 安装

大多数 Linux 发行版默认已安装 curl,如需手动安装:

1
2
3
4
5
6
7
8
9
10
11
# Debian/Ubuntu
sudo apt install curl -y

# CentOS/RHEL/Rocky
sudo dnf install curl -y

# Alpine
apk add curl

# macOS(Homebrew)
brew install curl

验证安装:

1
curl --version

2.2 最简单的用法

1
2
3
4
5
6
7
8
# 获取网页内容
curl https://example.com

# 获取 HTTP 响应头
curl -I https://example.com

# 静默模式(不显示进度信息)
curl -s https://api.github.com

三、常用选项详解

以下是 curl 最常用的选项速查表:

选项长选项说明
-o--output将输出写入文件
-O--remote-name用远程文件名保存
-L--location跟随重定向
-i--include输出中包含响应头
-I--head仅获取响应头
-k--insecure允许不安全的 SSL 连接
-u--userHTTP 基本认证用户名:密码
-v--verbose显示详细通信过程
-s--silent静默模式
-S--show-error静默模式下仍显示错误
-w--write-out请求完成后输出额外信息
-x--proxy使用代理
-C--continue-at断点续传
-m--max-time最大请求时间(秒)
--connect-timeout连接超时时间(秒)
--retry失败重试次数
--limit-rate限速(字节/秒)

四、HTTP 请求定制

4.1 指定请求方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# GET 请求(默认)
curl https://api.example.com/users

# POST 请求
curl -X POST https://api.example.com/users

# PUT 请求
curl -X PUT https://api.example.com/users/1

# DELETE 请求
curl -X DELETE https://api.example.com/users/1

# PATCH 请求
curl -X PATCH https://api.example.com/users/1

4.2 发送请求体

1
2
3
4
5
6
7
8
9
10
11
# URL 编码表单数据(Content-Type: application/x-www-form-urlencoded)
curl -X POST -d "name=Alice&email=alice@example.com" https://api.example.com/users

# JSON 数据(需要指定 Content-Type)
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name":"Alice","email":"alice@example.com"}' \
https://api.example.com/users

# 从文件读取请求体
curl -X POST -H "Content-Type: application/json" -d @data.json https://api.example.com/users

4.3 自定义请求头

1
2
3
4
5
6
7
8
# 设置单个 Header
curl -H "Authorization: Bearer token123" https://api.example.com/protected

# 设置多个 Headers
curl -H "Authorization: Bearer token123" \
-H "Accept: application/json" \
-H "User-Agent: MyApp/1.0" \
https://api.example.com/protected
1
2
3
4
5
6
7
8
# 发送 Cookie
curl -b "session_id=abc123; user=alice" https://example.com/profile

# 从文件读取 Cookie
curl -b cookies.txt https://example.com/profile

# 将服务器返回的 Cookie 保存到文件(Cookie Jar)
curl -c cookies.txt https://example.com/login -d "username=admin&password=secret"

五、文件下载与上传

5.1 下载文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 保存为指定文件名
curl -o myfile.zip https://example.com/files/large.zip

# 使用远程文件名保存
curl -O https://example.com/files/large.zip

# 断点续传(假设上次下载到 50%)
curl -C - -o myfile.zip https://example.com/files/large.zip

# 限制下载速度(100KB/s)
curl --limit-rate 100K -O https://example.com/files/large.zip

# 显示下载进度条
curl -# -O https://example.com/files/large.zip

5.2 上传文件

1
2
3
4
5
6
7
8
# 表单文件上传(模拟 <input type="file">)
curl -F "file=@/path/to/photo.jpg" https://example.com/upload

# 指定表单字段名和 MIME 类型
curl -F "avatar=@photo.jpg;type=image/jpeg" https://example.com/upload

# FTP 上传
curl -T localfile.txt ftp://ftp.example.com/upload/

六、认证与 HTTPS

6.1 HTTP 基本认证

1
2
3
4
5
6
7
8
9
# 方式一:-u 选项
curl -u username:password https://api.example.com/protected

# 方式二:URL 中嵌入凭据
curl https://username:password@api.example.com/protected

# 方式三:使用 ~/.netrc 文件(更安全)
# 文件内容:machine api.example.com login username password password
curl -n https://api.example.com/protected

6.2 Bearer Token 认证

1
2
# OAuth2 或 JWT 认证
curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..." https://api.example.com/protected

6.3 SSL/TLS 相关

1
2
3
4
5
6
7
8
9
10
11
# 跳过证书验证(仅用于测试,不安全)
curl -k https://self-signed.example.com

# 指定客户端证书
curl --cert client.crt --key client.key https://api.example.com

# 指定 CA 证书路径
curl --cacert /path/to/ca-bundle.crt https://api.example.com

# 查看 SSL 连接详细信息
curl -v https://example.com 2>&1 | grep -E "SSL|TLS|certificate"

七、API 调试实战

7.1 REST API 测试场景

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 1. 获取用户列表(GET)
curl -s https://jsonplaceholder.typicode.com/users | head -20

# 2. 创建新用户(POST)
curl -s -X POST \
-H "Content-Type: application/json" \
-d '{"name":"Alice","username":"alice","email":"alice@example.com"}' \
https://jsonplaceholder.typicode.com/users

# 3. 更新用户(PUT)
curl -s -X PUT \
-H "Content-Type: application/json" \
-d '{"name":"Alice Updated"}' \
https://jsonplaceholder.typicode.com/users/1

# 4. 部分更新(PATCH)
curl -s -X PATCH \
-H "Content-Type: application/json" \
-d '{"email":"newemail@example.com"}' \
https://jsonplaceholder.typicode.com/users/1

# 5. 删除用户(DELETE)
curl -s -X DELETE https://jsonplaceholder.typicode.com/users/1

7.2 响应分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 查看完整的请求-响应交互过程
curl -v https://api.github.com

# 仅查看响应头
curl -s -D - https://api.github.com -o /dev/null

# 获取响应时间统计
curl -s -w "\n\n========== 时间统计 ==========\n\
DNS 解析: %{time_namelookup}s\n\
TCP 连接: %{time_connect}s\n\
TLS 握手: %{time_appconnect}s\n\
首字节: %{time_starttransfer}s\n\
总耗时: %{time_total}s\n\
下载速度: %{speed_download} bytes/s\n\
HTTP 状态码: %{http_code}\n" \
https://api.github.com -o /dev/null

7.3 配合 jq 处理 JSON 响应

1
2
3
4
5
6
7
8
9
10
11
# 安装 jq
sudo apt install jq -y

# 提取特定字段
curl -s https://api.github.com/repos/curl/curl | jq '.stargazers_count'

# 格式化输出
curl -s https://jsonplaceholder.typicode.com/users | jq '.[] | {name, email, phone}'

# 过滤条件
curl -s https://jsonplaceholder.typicode.com/users | jq '.[] | select(.name | contains("Lea"))'

八、进阶用法

8.1 多请求并发

1
2
3
4
5
6
# 安装并行版本
sudo apt install parallel -y

# 并发请求多个 URL
echo -e "https://api.github.com\nhttps://httpbin.org/get\nhttps://jsonplaceholder.typicode.com/todos/1" | \
parallel -j 3 "curl -s -o /dev/null -w '%{http_code} %{url_effective}\n' {}"

8.2 使用代理

1
2
3
4
5
6
7
8
# HTTP 代理
curl -x http://proxy.example.com:8080 https://httpbin.org/get

# SOCKS5 代理(常用于 SSH 隧道)
curl --socks5 localhost:1080 https://httpbin.org/get

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

8.3 使用配置文件

curl 支持从配置文件读取选项,避免每次输入冗长的参数:

1
2
3
4
5
6
7
8
9
10
11
# 创建配置文件 ~/.curlrc
cat > ~/.curlrc << 'EOF'
--connect-timeout 10
--max-time 30
--retry 3
--retry-delay 2
--user-agent "MyCurl/1.0"
EOF

# 之后每次 curl 都会自动应用这些选项
curl https://example.com

8.4 发送邮件(SMTP)

1
2
3
4
5
curl --mail-from sender@example.com \
--mail-rcpt recipient@example.com \
--upload-file mail.txt \
smtp://smtp.example.com:587 \
--user sender@example.com:password

九、常见问题排查

问题可能原因解决方案
curl: (6) Could not resolve hostDNS 解析失败检查网络连接和 DNS 配置(/etc/resolv.conf
curl: (7) Failed to connect目标服务器不可达检查端口是否开放:nc -zv host port
curl: (28) Connection timed out连接超时增加 --connect-timeout 或检查防火墙
curl: (35) SSL connection errorTLS/SSL 握手失败使用 -v 查看详情,检查证书有效性
curl: (56) Recv failure连接被重置可能是防火墙拦截或服务端异常关闭
curl: (60) SSL certificate problem证书无效或自签名测试时用 -k,生产需安装正确证书
curl: (92) HTTP/2 stream 0 was not closed cleanlyHTTP/2 协议错误尝试 --http1.1 降级到 HTTP/1.1

十、安全最佳实践

  1. 避免在命令行直接输入密码——使用 ~/.netrc 文件或环境变量
  2. 生产环境不要使用 -k 跳过证书验证——这会暴露于 MITM 攻击
  3. 传输敏感数据必须使用 HTTPS——curl 默认支持,但注意 -k 会降低安全性
  4. 日志中过滤敏感信息——不要在日志中记录 Authorization 头的 token 值
  5. 合理设置超时和重试——防止脚本因网络问题卡死
  6. 注意文件权限——保存 API 令牌或 Cookie 的文件应设置 600 权限

十一、命令速查表

场景命令
简单请求curl https://example.com
POST JSONcurl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL
带 Tokencurl -H "Authorization: Bearer TOKEN" URL
下载文件curl -O URL
上传文件curl -F "file=@/path" URL
查看响应头curl -I URL
调试输出curl -v URL
性能测量curl -w "时间统计..." URL
断点续传curl -C - -o file URL
代理请求curl -x http://proxy:port URL
跟随重定向curl -L URL
带 Cookiecurl -b "key=value" URL

总结

curl 虽然只是一个命令行工具,但它的功能极其丰富。无论是日常的 API 调试、文件传输,还是深入的网络故障排查,curl 都能胜任。掌握 curl 的核心用法不仅能提高工作效率,还能帮你更好地理解 HTTP 协议和网络通信机制。建议把本文的速查表保存下来,在实际工作中多练习方能熟练运用。


本文由AI辅助生成,内容仅供参考

  • 标题: Linux curl 命令完全指南:从入门到精通
  • 作者: Someone
  • 创建于 : 2026-06-09 13:29:00
  • 更新于 : 2026-06-18 08:39:57
  • 链接: https://demo-blog.qusite.cn/2026-06-09-curl-command-guide/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。