在Linux运维过程中经常使用脚本、命令来完成工作。下面整理一份 Linux 系统中curl命令最实用、高频、能直接上手的技巧,日常抓接口、测服务、下载、排错基本全覆盖。
一、最基础常用
1. 简单请求(默认 GET)
直接查看网页源码信息
curl https://www.baidu.com
2. 只看响应头(不看内容)
这样就可以直接看到网站的响应头部信息,状态码等。
curl -I https://www.baidu.com
3. 显示完整请求 + 响应过程(排错神器)
这个比较完整,如header信息,证书请求过程等。
curl -v https://www.baidu.com
4. 静默模式(不输出进度)
这个跟直接
curl -s https://www.baidu.com
二、下载文件
1. 保存为原文件名
curl -O https://example.com/file.zip
2. 自定义文件名
下载文件时可以直接另存为一个文件名
curl -o myfile.zip https://example.com/file.zip
3. 断点续传
就是支持断点下载,不担心手误关掉重来
curl -C - -O https://example.com/file.zip
4. 带进度条下载
curl -# -O https://example.com/file.zip
三、发送 POST / JSON 数据
1. 普通表单 POST
curl -X POST -d "name=test&age=18" https://example.com/api
2. 发送 JSON(最常用)
这个一般在测试API时用得多些。
curl -X POST \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"123456"}' \
https://example.com/login
3. 从文件读取 JSON 发送
curl -X POST -H "Content-Type: application/json" -d @data.json https://example.com/api
四、请求头 / Cookie / 认证
1. 自定义请求头
curl -H "Token: abc123" -H "User-Agent: MyClient" https://example.com
2. 携带 Cookie
curl -b "sessionid=abc123" https://example.com
3. 保存响应的 Cookie
curl -c cookies.txt https://example.com
4. Basic Auth 基础认证
curl -u user:password https://example.com
五、控制请求行为
1. 设置超时(秒)
curl --connect-timeout 5 --max-time 10 https://example.com
2. 跟随重定向(3xx)
curl -L https://example.com
3. 只输出 HTTP 状态码
curl -s -o /dev/null -w "%{http_code}\n" https://example.com
4. 禁用 SSL 证书校验(内网 / 自签名常用)
curl -k https://self-signed.example.com
六、查看本机外网 IP
curl ifconfig.me
curl ipinfo.io/ip
curl icanhazip.com
七、模拟浏览器访问
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" https://example.com
八、上传文件
curl -F "file=@/path/local.jpg" https://example.com/upload
九、调试常用组合(万能排错)
curl -v -L -k -o /dev/null https://example.com
最后一个获取域名的IP地址,虽然直接Ping也可以得出
curl --silent --output /dev/null --write-out "%{remote_ip}" https://www.58jb.com
如果知道了IP地址,还可以结合whois工具查看IP地址的详细信息
sudo apt install whois
whois 114.114.114.114 #这样可以看到该IP的详细信息