Ubuntu 22.04 curl 安装与命令参数详解

在 Ubuntu 22.04 上安装 curl 并逐参数实测常用 HTTP 命令输入输出,涵盖 GET/HEAD/POST/PUT/上传、响应头、保存文件、断点续传与分段、超时限速重试、HTTPS/证书、认证、自定义头与 -w 变量统计。

Linux 网络诊断与调试curlLinux命令行网络诊断2026/10/06

Ubuntu 22.04 curl 安装与命令参数详解

目标:在 Ubuntu 22.04 上安装 curl,并逐参数实测常用 HTTP 命令的输入与输出,涵盖 GET/HEAD/POST/PUT/上传、响应头、保存文件、断点续传与分段、超时限速重试、HTTPS/证书、认证、自定义请求头与 -w 变量统计。本文命令与输出均为 Ubuntu 22.04(curl 7.81.0)实测回显。

环境信息

  • 操作系统:Ubuntu 22.04 LTS
  • curl 版本:curl 7.81.0(x86_64-pc-linux-gnu,libcurl/7.81.0,OpenSSL/3.0.2,nghttp2/1.43.0)
  • 内网 HTTP 靶机:172.22.0.2:8000(python3 http,index.html 18 字节、file1m.bin 1MB)与 172.22.0.3:8000
  • 外网站点:http://www.baidu.com、https://www.baidu.com(Server: bfe)
  • 保留/不可达网段:192.0.2.1(DOCUMENTATION 测试网段,无响应)
  • 说明:内网靶机为 Python SimpleHTTP/0.6,不支持 Range 分段、不支持 POST/PUT/上传,相关条目按真实行为如实记录

工具概述

curl 是一个功能全面的命令行 URL 传输工具,基于 libcurl,支持 HTTP/HTTPS/FTP/FTPS 等十余种协议。除基础 GET/POST 外,还提供响应头、保存文件、断点续传、限速、超时、重试、证书校验、认证及自定义请求头等能力,配合 -w 输出变量可做脚本化请求耗时与连通性统计,是网络诊断的核心工具。本文针对 Ubuntu 22.04 实测。

安装

在线安装(apt)

sudo apt update
sudo apt install -y curl

安装后验证:

curl --version
curl 7.81.0 (x86_64-pc-linux-gnu) libcurl/7.81.0 OpenSSL/3.0.2 zlib/1.2.11 brotli/1.0.9 zstd/1.4.8 libidn2/2.3.2 libpsl/0.21.0 (+libidn2/2.3.2) libssh/0.9.6/openssl/zlib nghttp2/1.43.0 librtmp/2.3 OpenLDAP/2.5.20
Release-Date: 2022-01-05
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets zstd

离线安装(.deb 包)

# 在可联网机器下载(含 libcurl4 libnghttp2-14 等依赖)
cd /opt/netdiag-offline
apt-get download curl libcurl4 libnghttp2-14

# 目标机离线安装
sudo dpkg -i *.deb

安装后同样用 curl --version 验证版本。

命令实测

版本信息(--version)

用途:完整输出版本、依赖库、支持的协议与特性。

curl --version
curl 7.81.0 (x86_64-pc-linux-gnu) libcurl/7.81.0 OpenSSL/3.0.2 zlib/1.2.11 brotli/1.0.9 zstd/1.4.8 libidn2/2.3.2 libpsl/0.21.0 (+libidn2/2.3.2) libssh/0.9.6/openssl/zlib nghttp2/1.43.0 librtmp/2.3 OpenLDAP/2.5.20
Release-Date: 2022-01-05
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets zstd

基础 GET 请求

用途:直接请求 URL,显示进度条并输出响应体。实测 GET /index.html:进度条与响应体 netdiag test page。

curl http://172.22.0.2:8000/index.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    18  100    18    0     0  10830      0 --:--:-- --:--:-- --:--:-- 18000
netdiag test page

只取响应头(-I,HEAD 请求)

用途:-I 发送 HEAD 请求,只显示响应头,不取正文。

curl -I http://172.22.0.2:8000/index.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0    18    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.10.12
Date: Tue, 06 Oct 2026 04:54:46 GMT
Content-type: text/html
Content-Length: 18
Last-Modified: Tue, 06 Oct 2026 04:23:17 GMT

头 + 体一起输出(-i)

用途:-i 同时输出响应头与响应体。

curl -i http://172.22.0.2:8000/index.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    18  100    18    0     0  13100      0 --:--:-- --:--:-- --:--:-- 18000
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.10.12
Date: Tue, 06 Oct 2026 04:54:49 GMT
Content-type: text/html
Content-Length: 18
Last-Modified: Tue, 06 Oct 2026 04:23:17 GMT

netdiag test page

静默模式(-s)

用途:不显示进度条与错误信息,只输出响应体。

curl -s http://172.22.0.2:8000/index.html
netdiag test page

静默但保留错误(-S -s)

用途:-s 连报错都吞掉;加 -S 后出错时仍打印错误。注:中文主机名会先在 IDNA 转换阶段失败(错误码 3);换 ASCII 的不存在域名后可看到典型解析失败(错误码 6)。

curl -S -s http://不存在名字/
curl -s http://nonexistent-host-xyz123/
curl -S -s http://nonexistent-host-xyz123/
# curl -S -s http://不存在名字/
curl: (3) Failed to convert 不存在名字 to ACE; could not convert string to UTF-8
(退出码 3)

# curl -s http://nonexistent-host-xyz123/ (纯 -s:无输出)
(退出码 6)

# curl -S -s http://nonexistent-host-xyz123/
curl: (6) Could not resolve host: nonexistent-host-xyz123
(退出码 6)

详细过程(-v)

用途:显示连接、请求头、响应头全过程。输出较长,head -25 截断。

curl -v http://172.22.0.2:8000/index.html 2>&1 | head -25
*   Trying 172.22.0.2:8000...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to 172.22.0.2 (172.22.0.2) port 8000 (#0)
> GET /index.html HTTP/1.1
> Host: 172.22.0.2:8000
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Server: SimpleHTTP/0.6 Python/3.10.12
< Date: Tue, 06 Oct 2026 04:55:09 GMT
< Content-type: text/html
< Content-Length: 18
< Last-Modified: Tue, 06 Oct 2026 04:23:17 GMT
<
{ [18 bytes data]
100    18  100    18    0     0   8196      0 --:--:-- --:--:-- --:--:-- 18000
* Closing connection 0
netdiag test page

保存到指定文件(-o)

用途:-o 将响应写入指定文件。

curl -o /tmp/curl-index.html http://172.22.0.2:8000/index.html && head -1 /tmp/curl-index.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    18  100    18    0     0  13698      0 --:--:-- --:--:-- --:--:-- 18000
netdiag test page

按远程文件名保存(-O)

用途:保存到当前目录,文件名与 URL 末段一致。

curl -O http://172.22.0.2:8000/index.html && ls -l index.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    18  100    18    0     0  13729      0 --:--:-- --:--:-- --:--:-- 18000
-rw-r--r-- 1 root root 18 Oct  6 04:55 index.html

下载并输出统计(-w 格式变量)

用途:%{speed_download}、%{time_total}、%{http_code} 分别给出速度、总耗时与状态码。

curl -O http://172.22.0.2:8000/file1m.bin -w '下载速度: %{speed_download} bytes/s\n总耗时: %{time_total}s\nHTTP: %{http_code}\n'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1024k  100 1024k    0     0   295M      0 --:--:-- --:--:-- --:--:--  500M
下载速度: 309954478 bytes/s
总耗时: 0.003383s
HTTP: 200

断点续传(-C -)

用途:-C - 自动从已下载位置续传。实测:靶机 SimpleHTTP 不支持 Range(尝试 curl -r 0-500000 时仍返回 200 全量 1048576 字节,截断失败),故改用 truncate 截出 500000 字节的半截文件;随后 -C - 续传被服务器拒绝,报错码 33(如实记录)。

curl -s -o file1m.bin http://172.22.0.2:8000/file1m.bin && truncate -s 500000 file1m.bin && ls -l file1m.bin
curl -C - -O http://172.22.0.2:8000/file1m.bin
-rw-r--r-- 1 root root 500000 Oct  6 04:55 file1m.bin
** Resuming transfer from byte position 500000
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0 1024k    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume.

分段请求(-r)

用途:-r 0-99 请求前 100 字节;靶机忽略 Range 头返回 200 全量,故 wc -c 统计到完整 1048576 字节(如实记录)。

curl -s -r 0-99 http://172.22.0.2:8000/file1m.bin | wc -c
1048576

跟随重定向(-L)

用途:-L 跟随 HTTP 重定向;%{url_effective} 显示最终落地 URL,%{http_code} 为最终状态码。

curl -L -s -o /dev/null -w '%{url_effective} %{http_code}\n' http://www.baidu.com
http://www.baidu.com/ 200

忽略证书校验(-k,HTTPS)

用途:-k 跳过证书校验(自签或证书不匹配场景)。

curl -k -I https://www.baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.1 200 OK
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Length: 0
Content-Type: text/html
Pragma: no-cache
Server: bfe
Date: Tue, 06 Oct 2026 04:55:37 GMT

正常 HTTPS 请求

用途:证书校验通过时结果与 -k 相同。

curl -I https://www.baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.1 200 OK
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Length: 0
Content-Type: text/html
Pragma: no-cache
Server: bfe
Date: Tue, 06 Oct 2026 04:55:37 GMT

自定义请求头(-H)

用途:-H 追加自定义请求头;用 -v 抓取发出的请求头验证(服务器不回显请求头)。

curl -H 'X-Netdiag: test' -v http://172.22.0.2:8000/index.html 2>&1 | grep -i x-netdiag
> X-Netdiag: test

自定义 User-Agent(-A)

用途:-A 覆盖默认 UA;服务器响应与默认无异,用 -v 验证 UA 确已生效。

curl -A 'netdiag/1.0' -I http://172.22.0.2:8000/index.html
curl -A 'netdiag/1.0' -v http://172.22.0.2:8000/index.html 2>&1 | grep -i '^> User-Agent'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0    0    0    0    0     0      0      0 --:--:-- --:--:-- --:--:--     0HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.10.12
Date: Tue, 06 Oct 2026 04:55:44 GMT
Content-type: text/html
Content-Length: 18
Last-Modified: Tue, 06 Oct 2026 04:23:17 GMT

  0    18    0    0    0     0      0 --:--:-- --:--:-- --:--:--     0

# -v 抓取的请求头:
> User-Agent: netdiag/1.0

伪造 Referer(-e)

用途:-e 设置 Referer 请求头。

curl -e 'http://ref.example.com' -I http://172.22.0.2:8000/index.html
curl -e 'http://ref.example.com' -v http://172.22.0.2:8000/index.html 2>&1 | grep -i '^> Referer'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0    18    0    0    0     0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.10.12
Date: Tue, 06 Oct 2026 04:55:44 GMT
Content-type: text/html
Content-Length: 18
Last-Modified: Tue, 06 Oct 2026 04:23:17 GMT

# -v 抓取的请求头:
> Referer: http://ref.example.com

POST 请求(-X POST -d)

用途:-X POST -d 提交表单数据;靶机 http.server 不支持 POST,返回 501 错误页(如实记录)。

curl -X POST -d 'name=netdiag&ver=1' http://172.22.0.2:8000/index.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   515  100   497  100    18   379k  14062 --:--:-- --:--:-- --:--:--  502k
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <title>Error response</title>
    </head>
    <body>
        <h1>Error response</h1>
        <p>Error code: 501</p>
        <p>Message: Unsupported method ('POST').</p>
        <p>Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation.</p>
    </body>
</html>

带数据的 GET(-X GET -d)

用途:GET 方法靶机支持,带请求体也照常返回 200。

curl -X GET -d 'name=netdiag&ver=1' -s -o /dev/null -w '%{http_code}\n' http://172.22.0.2:8000/index.html
200

PUT 请求(-X PUT -d)

用途:PUT 同样不被靶机支持,返回 501(如实记录)。

curl -X PUT -d 'x=1' http://172.22.0.2:8000/index.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   499  100   496  100     3   423k   2620 --:--:-- --:--:-- --:--:--  487k
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <title>Error response</title>
    </head>
    <body>
        <h1>Error response</h1>
        <p>Error code: 501</p>
        <p>Message: Unsupported method ('PUT').</p>
        <p>Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation.</p>
    </body>
</html>

PUT 提交 JSON(-X PUT -H -d)

用途:JSON 体同样被 501 拒绝(如实记录)。

curl -X PUT -H 'Content-Type: application/json' -d '{"a":1}' http://172.22.0.2:8000/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   503  100   496  100     7   439k   6357 --:--:-- --:--:-- --:--:--  491k
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <title>Error response</title>
    </head>
    <body>
        <h1>Error response</h1>
        <p>Error code: 501</p>
        <p>Message: Unsupported method ('PUT').</p>
        <p>Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation.</p>
    </body>
</html>

上传文件(-T)

用途:-T 以 PUT 方式上传,靶机不支持,返回 501(如实记录)。

curl -T /tmp/curl-index.html http://172.22.0.2:8000/upload
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
 96   514  100   496    0     0   362k      0 --:--:-- --:--:-- --:--:--  484k
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <title>Error response</title>
    </head>
    <body>
        <h1>Error response</h1>
        <p>Error code: 501</p>
        <p>Message: Unsupported method ('PUT').</p>
        <p>Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation.</p>
    </body>
</html>

基础认证(-u)

用途:-u user:pass 携带 HTTP Basic 凭据。服务器未配置认证、不返回 401 挑战,凭据不影响结果,直接 200(如实记录)。

curl -u user:pass -I http://172.22.0.2:8000/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0    18    0    0    0     0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.10.12
Date: Tue, 06 Oct 2026 04:55:58 GMT
Content-type: text/html
Content-Length: 18
Last-Modified: Tue, 06 Oct 2026 04:23:17 GMT

手工绑定域名(--resolve)

用途:将 netdiag.test:8000 强制解析到靶机 IP,绕过系统 DNS。

curl --resolve netdiag.test:8000:172.22.0.2 -I http://netdiag.test:8000/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0    18    0     0    0     0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.10.12
Date: Tue, 06 Oct 2026 04:56:02 GMT
Content-type: text/html
Content-Length: 18
Last-Modified: Tue, 06 Oct 2026 04:23:17 GMT

连接超时(--connect-timeout)

用途:192.0.2.1 为保留测试网段,无响应;2 秒连接超时后报错码 28。

curl --connect-timeout 2 http://192.0.2.1/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
curl: (28) Connection timeout after 2000 ms
(退出码 28)

限速 + 总超时(-m + --limit-rate)

用途:--limit-rate 10k 把 1MB 文件拖到约 100 秒才能下完,-m 3 总时长 3 秒先到,中途超时报错(如实记录)。

curl -m 3 http://172.22.0.2:8000/file1m.bin --limit-rate 10k -o /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  3 1024k    3 40754    0     0  13585      0  0:01:17  0:00:02  0:01:15 13584
curl: (28) Operation timed out after 3000 milliseconds with 40754 out of 1048576 bytes received
(退出码 28)

限速下载(--limit-rate)

用途:限 100k/s,%{speed_download} 实测约 106KB/s。

curl --limit-rate 100k -o /dev/null -w '%{speed_download}\n' http://172.22.0.2:8000/file1m.bin
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1024k  100 1024k    0     0   103k      0  0:00:09  0:00:09 --:--:--  110k
106335

请求压缩(--compressed)

用途:--compressed 请求压缩内容并自动解压;实测响应头出现 Content-Encoding: gzip。

curl --compressed -I -H 'Accept-Encoding: gzip' https://www.baidu.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0HTTP/1.1 200 OK
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Encoding: gzip
Content-Length: 23
Content-Type: text/html
Pragma: no-cache
Server: bfe
Date: Tue, 06 Oct 2026 04:56:24 GMT

强制 IPv4 / IPv6(-4 / -6)

用途:-4 正常;环境无 IPv6 出口,-6 连接失败(如实记录,错误码 7)。

curl -4 -I http://www.baidu.com
curl -6 -I http://www.baidu.com
# curl -4:
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0    0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.1 200 OK
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Length: 0
Content-Type: text/html
Pragma: no-cache
Server: bfe
Date: Tue, 06 Oct 2026 04:56:24 GMT

# curl -6:
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0    0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Couldn't connect to server
(退出码 7)

代理不可达(-x)

用途:-x 指定代理;指向 127.0.0.1 的 1 端口,立即被拒(错误码 7)。

curl -x http://127.0.0.1:1 http://www.baidu.com -m 2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0    0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (7) Failed to connect to 127.0.0.1 port 1 after 0 ms: Connection refused
(退出码 7)

失败重试(--retry)

用途:--retry 2 --retry-delay 1 共发起 3 次(1 + 2 重试),每次均 2 秒超时,最终仍失败(错误码 28)。

curl --retry 2 --retry-delay 1 http://192.0.2.1/ -m 2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0    0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
curl: (28) Connection timed out after 2000 milliseconds
Warning: Problem : timeout. Will retry in 1 seconds. 2 retries left.
  0     0    0    0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
curl: (28) Connection timed out after 2000 milliseconds
Warning: Problem : timeout. Will retry in 1 seconds. 1 retries left.
  0     0    0    0    0     0      0      0 --:--:--  0:00:02 --:--:--     0
curl: (28) Connection timed out after 2001 milliseconds
(退出码 28)

各阶段耗时(-w 计时变量)

用途:%{time_connect} 为 TCP 连接、%{time_starttransfer} 为首字节、%{time_total} 为总耗时(秒)。

curl -s -w '%{time_connect} %{time_starttransfer} %{time_total}\n' -o /dev/null http://www.baidu.com
0.033525 0.060037 0.060107

对端信息与证书校验结果

用途:%{remote_ip}:%{remote_port} 为实际连接的对端,%{ssl_verify_result} 为 0 表示证书校验通过。

curl -s -w '%{remote_ip}:%{remote_port} %{ssl_verify_result}\n' -o /dev/null https://www.baidu.com
110.242.70.57:443 0

版本差异标注

  • Ubuntu 22.04(本采集):curl 7.81.0(Release-Date 2022-01-05),OpenSSL 3.0.2,特性含 brotli/zstd/IDN/PSL/HSTS/alt-svc,协议含 gophers、mqtt、scp、sftp、ldap、ldaps、rtmp 等。
  • 超时报错措辞:7.81 为 curl: (28) Connection timeout after 2000 ms;较旧的 7.61(如 CentOS 8)为 curl: (28) Connection timed out after 2000 milliseconds。
  • curl --help:7.81 默认输出分类精简帮助并提示 --help all;7.61 是传统完整选项列表。
  • 进度条格式、-w 变量、-s/-S 行为在 7.6x/8.x 大体一致。

帮助输出

Ubuntu 的 curl 7.81 默认输出分类精简帮助并提示 --help all:

curl --help | head -40
Usage: curl [options...] <url>
 -d, --data <data>          HTTP POST data
 -f, --fail                 Fail silently (no output at all) on HTTP errors
 -h, --help <category>      Get help for commands
 -i, --include              Include protocol response headers in the output
 -o, --output <file>        Write to file instead of stdout
 -O, --remote-name          Write output to a file named as the remote file
 -s, --silent               Silent mode
 -T, --upload-file <file>   Transfer local FILE to destination
 -u, --user <user:password> Server user and password
 -A, --user-agent <name>    Send User-Agent <name> to server
 -v, --verbose              Make the operation more talkative
 -V, --version              Show version number and quit

This is not the full help, this menu is stripped into categories.
Use "--help category" to get an overview of all categories.
For all options use the manual or "--help all".

常见问题

  1. curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume.:靶机 SimpleHTTP 不支持 Range,-C - 续传被拒。该场景应确认服务端是否支持 Range(查看响应头是否含 Accept-Ranges)。
  2. curl: (3) Failed to convert 不存在名字 to ACE:中文/非 ASCII 主机名在 IDNA 转换阶段失败,错误码 3;请使用合法 ASCII 域名或 IP。
  3. POST/PUT/上传返回 501 Unsupported method:属于服务端(python3 http.server 仅实现 GET/HEAD)的限制而非 curl 问题;换支持的方法测试即可。
  4. curl: (28) 超时:区分连接超时(--connect-timeout)与总超时(-m);对无响应的目标可用 -w 打印各阶段耗时定位在连接、首字节还是传输阶段。
  5. curl: (7) 连接被拒/失败:多为端口未监听、被防火墙拦截或代理不可达;结合 curl -v 查看连接是否建立。

参考链接

  • curl 官方文档:https://curl.se/
  • curl 手册:man curl
  • libcurl 变量说明:man curl_easy(-%{...} 输出变量)