CentOS curl 使用指南
背景介绍

curl
是一个强大的命令行工具,用于与服务器之间传输数据,它支持多种协议,包括 HTTP、HTTPS、FTP 等,本文将详细介绍如何在 CentOS 上安装和使用curl
,涵盖从基础到高级的各个方面。
一、CentOS 上安装 curl
安装步骤
在 CentOS 8 上安装curl
非常简单,只需运行以下命令:
- sudo dnf install curl
安装完成后,可以通过输入curl
来验证它是否成功安装:
- curl
输出应该像这样:
- curl: try 'curl help' or 'curl manual' for more information
验证安装
确保curl
已正确安装,可以通过查看版本信息:

- curl version
输出示例:
- curl 7.68.0 (x86_64redhatlinuxgnu) libcurl/7.68.0 OpenSSL/1.1.1d zlib/1.2.11 libidn2/2.3.0 libpsl/0.20.2 (+libidn2/2.3.0) nghttp2/1.40.0 librtmp/2.3
- ReleaseDate: 20200108
- Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
- Features: AsynchDNS IDN IPv6 Largefile GSSAPI Kerberos SPNEGSS SPN
二、基本用法
使用curl
最常见的场景是获取网页内容,要获取 Google 主页内容,可以使用以下命令:
- curl http://www.google.com
这将在终端中显示 Google 主页的 HTML 内容。
你可以使用o
或output
选项将网页内容保存到文件中:
- curl o google.html http://www.google.com
这样会将 Google 主页的 HTML 内容保存到当前目录下的google.html
文件中。

发送 POST 请求
curl
也可以用来发送 POST 请求,要发送一个包含表单数据的 POST 请求,可以使用d
或data
选项:
- curl d "name=John&age=30" http://example.com/form
三、高级特性
自定义请求头
使用H
或header
选项可以自定义 HTTP 请求头,这在某些需要特定头信息的 API 请求中非常有用:
- curl H "ContentType: application/json" d '{"name":"John", "age":30}' http://example.com/api
跟随重定向
默认情况下,curl
不会跟随 HTTP 重定向,但你可以使用L
或location
选项来启用这一功能:
- curl L http://example.com/redirect
显示响应头
使用i
或include
选项,curl
会显示响应头和内容,这在调试和了解服务器响应时非常有用:
- curl i http://www.google.com
输出示例:
- HTTP/1.1 200 OK
- Date: Thu, 13 Feb 2020 22:01:04 GMT
- Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2kfips
- StrictTransportSecurity: maxage=31536000
- XFrameOptions: SAMEORIGIN
- XXssProtection: 1; mode=block
- XContentTypeOptions: nosniff
- ReferrerPolicy: sameorigin
- LastModified: Thu, 06 Feb 2020 17:21:08 GMT
- ETag: "542159deb7fadfdfd"
- AcceptRanges: bytes
- ContentLength: 21537
- ContentType: text/html; charset=UTF8
- <!doctype html>...</body></html>
四、常见问题及解答(FAQs)
Q1:如何使用代理服务器?
A1:你可以使用x
或proxy
选项指定代理服务器,通过 SOCKS5 代理访问网站:
- curl x socks5h://localhost:9050 http://www.example.com
Q2:如何显示下载进度?
A2:使用progressbar
选项,curl
会在下载或上传时显示一个进度条:
- curl progressbar http://example.com/largefile.zip