Linux之curl常用参数介绍
文章目录
- 什么是 curl 命令
- curl 的主要特点
- 基本语法结构
- 参数说明
- 常用选项参数详解
- 1. 基本请求控制
- 2. 输出控制
- 3. 认证与安全
- 4. 其他实用选项
- 使用案例
- 1.最简单的命令
- 2.组合命令
什么是 curl 命令
curl(Client URL)是一个强大的命令行工具,用于在 Linux/Unix 系统中传输数据。它支持多种协议,包括 HTTP、HTTPS、FTP、SFTP 等,是开发者和系统管理员日常工作中不可或缺的工具。
curl 的主要特点
- 多协议支持:支持几乎所有主流网络协议
- 无界面操作:纯命令行工具,适合脚本和自动化任务
- 功能丰富:支持文件上传下载、表单提交、Cookie 处理等
- 跨平台:在 Linux、macOS、Windows 上均可使用
基本语法结构
curl 命令的基本语法格式如下:
curl [options] [URL...]
参数说明
options
:各种可选参数,用于控制 curl 的行为URL
:要访问的一个或多个网址
常用选项参数详解
1. 基本请求控制
选项 | 说明 | 示例 |
---|---|---|
-X | 指定 HTTP 方法 | curl -X POST https://example.com |
-d | 发送 POST 数据 | curl -d "name=John" https://example.com |
-G | 将 -d 数据作为 GET 参数发送 | curl -G -d "q=keyword" https://search.com |
-H | 添加请求头 | curl -H "Content-Type: application/json" https://api.com |
2. 输出控制
选项 | 说明 | 示例 |
---|---|---|
-o | 将输出保存到文件 | curl -o output.html https://example.com |
-O | 使用远程文件名保存 | curl -O https://example.com/file.zip |
-s | 静默模式(不显示进度) | curl -s https://api.com/data.json |
-v | 显示详细通信过程 | curl -v https://example.com |
3. 认证与安全
选项 | 说明 | 示例 |
---|---|---|
-u | 用户名密码认证 | curl -u user:pass https://secure.com |
-k | 忽略 SSL 证书验证 | curl -k https://self-signed.com |
--cacert | 指定 CA 证书 | curl --cacert cert.pem https://secure.com |
4. 其他实用选项
选项 | 说明 | 示例 |
---|---|---|
-L | 跟随重定向 | curl -L https://short.url |
-I | 只获取头部信息 | curl -I https://example.com |
--limit-rate | 限制传输速度 | curl --limit-rate 100K https://largefile.com |
使用案例
1.最简单的命令
后面直接跟url就可以了
# curl https://example.com
<!doctype html><html lang="en"><head><title>Example Domain</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>body{background:#eee;width:60vw;margin:15vh auto;font-family:system-ui,sans-serif}h1{font-size:1.5em}div{opacity:0.8}a:link,a:visited{color:#348}</style><body><div><h1>Example Domain</h1><p>This domain is for use in documentation examples without needing permission. Avoid use in operations.<p><a href="https://iana.org/domains/example">Learn more</a></div></body></html># curl -I https://example.com
HTTP/1.1 200 OK
Content-Type: text/html
ETag: "bc2473a18e003bdb249eba5ce893033f:1760028122.592274"
Last-Modified: Thu, 09 Oct 2025 16:42:02 GMT
Cache-Control: max-age=86000
Date: Fri, 10 Oct 2025 09:15:21 GMT
Connection: keep-alive
Alt-Svc: h3=":443"; ma=93600
2.组合命令
curl -X GET -H "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36" -u username:pwd -I https://www.yansheng.fun/yan-family/
-x:指定 HTTP 方法,默认好像用的HEAD
-H:指定请求头信息,User-Agent不设置默认用curl,例如:curl/7.29.0
-u:指定访问用户和密码,对于部分设置了需要密码访问的网站需要设置
-I:只获取头部信息
我这里用我的网站测试,有对部分访问做了限制。
# 不加请求头
# curl -I https://www.yansheng.fun
HTTP/1.1 403 Forbidden
Server: nginx
Date: Fri, 10 Oct 2025 09:18:21 GMT
Content-Type: text/html
Content-Length: 146
Connection: keep-alive#nginx日志,因为我在nginx做了限制,禁止了curl访问,所以这里返回403
175.1.1.1 - - [2025-10-10T17:18:21+08:00] "HEAD / HTTP/1.1" 403 0 "-" curl/7.29.0 - 0.000 - - -# 加请求头
# curl -X GET -H "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36" -I https://www.yansheng.fun
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 10 Oct 2025 09:24:42 GMT
Content-Type: text/html
Content-Length: 197000
Last-Modified: Wed, 16 Jul 2025 08:51:39 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "6877681b-30188"
Accept-Ranges: bytes#nginx日志
175.178.108.117 - - [2025-10-10T17:46:03+08:00] "GET / HTTP/1.1" 200 114429 "-" Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 - 0.001 - - -# 访问需要密码的网址,没带-u参数
#curl -X GET -H "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36" -I https://www.yansheng.fun/yan-family/
HTTP/1.1 401 Unauthorized
Server: nginx
Date: Fri, 10 Oct 2025 09:46:48 GMT
Content-Type: text/html
Content-Length: 574
Connection: keep-alive
WWW-Authenticate: Basic realm="Please input password"#nginx日志
175.178.108.117 - username [2025-10-10T17:46:48+08:00] "GET /yan-family/ HTTP/1.1" 401 574 "-" Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 - 0.000 - - -
参考:https://www.runoob.com/linux/linux-comm-curl.html