掌握curl Linux系统必备的网络工具实战教程

Lear 2025-06-05 10:00:00
Categories: Tags:

掌握curl:Linux系统必备的网络工具实战教程


Linux系统中的curl命令使用详解


目录在这里嗷









curl概述


官方文档:https://curl.se/docs/
GitHub地址:https://github.com/curl/curl


curl全称Client URL,是一个命令行工具和库,用于通过 URL 传输数据,支持 100+ 协议(HTTP/HTTPS、FTP、SMTP、POP3、RTSP、DICT 等)。


其作用主要是发送网络请求、接收响应、支持文件上传 / 下载、处理认证、代理、Cookie、重定向等复杂网络操作。


其以轻量简洁、跨平台(Linux、Windows、Mac)、脚本友好(适合自动化和 API 测试)等特点深受广大程序员的喜爱。


curl应用场景







curl的基本使用


安装


| | |
| — | — |
| | # linux debian系列 |
| | apt install -y curl |
| | |
| | # linux CentOS系列 |
| | yum install -y curl |


curl的语法


| | |
| — | — |
| | # 其中options是选项参数,url表示请求的路径 |
| | curl [options] [url] |


常用选项参数


请求控制选项



| | |
| — | — |
| | curl -X GET https://www.baidu.com |



| | |
| — | — |
| | curl -H “content-type: application/json; charset=utf-8” \ |
| | -H “Authorization: Bearer TOKEN” \ |
| | https://api.example.com |



| | |
| — | — |
| | curl -d “name=John&age=30” https://api.example.com/submit |



| | |
| — | — |
| | curl -D headers.txt https://www.baidu.com |



| | |
| — | — |
| | curl -L http://www.baidu.com |



| | |
| — | — |
| | curl -b “sessionid=12345” https://example.com |



| | |
| — | — |
| | curl -c cookies.txt https://example.com/login |


数据传输选项



| | |
| — | — |
| | curl -o download.zip https://example.com/file.zip |



| | |
| — | — |
| | curl -O https://example.com/file.zip |



| | |
| — | — |
| | curl -d “name=John&age=30” https://api.example.com/submit |



| | |
| — | — |
| | curl -F “file=@local.jpg” https://api.example.com/upload |



| | |
| — | — |
| | curl -T report.pdf ftp://ftp.example.com/upload/ |


输出与调试选项



| | |
| — | — |
| | curl -v https://www.baidu.com |



| | |
| — | — |
| | curl -s https://example.com > content.txt |



| | |
| — | — |
| | curl -sS https://invalid-url.com |



| | |
| — | — |
| | curl –trace trace.log https://example.com |


其它高级选项



| | |
| — | — |
| | curl –proxy 127.0.0.1:8080 https://example.com |



| | |
| — | — |
| | curl -k https://self-signed.example.com |



| | |
| — | — |
| | curl -u admin:password123 https://auth.example.com |



| | |
| — | — |
| | curl –ntlm -u domain\\user:password https://intranet.example.com |



| | |
| — | — |
| | curl –limit-rate 100k https://example.com/big-file.zip |



| | |
| — | — |
| | curl –retry 5 https://flaky-api.example.com |



| | |
| — | — |
| | curl –range 0-1024 https://example.com/large-file.zip |



| | |
| — | — |
| | curl -x https://example.com |


常用案例



| | |
| — | — |
| | # 作用:请求 192.0.2.1,但告诉服务器 “我是 fake.example.com” |
| | curl -H “Host: fake.example.com” http://192.0.2.1 |



| | |
| — | — |
| | # 短选项 -o,指定输出文件名 |
| | curl -o filename.html https://example.com/page.html |
| | |
| | # 大写 -O,使用 URL 中的文件名 |
| | curl -O https://example.com/filename.zip |