Linux(Centos 7.6)命令详解:curl

1.命令作用

curl是一款用于与服务器进行数据传输的工具,即可以从服务器获取数据,也可以向服务器发送数据,且支持多种协议(curl  is  a  tool to transfer data from or to a server, using one of the supported protocols)

2.命令语法

Usage: curl [options...] <url>

3.参数详解

注意:(H)表示仅支持 HTTP/HTTPS,(F)表示仅支持FTP,url是远程服务器链接地址或文件地址

OPTION:

  • -C, --continue-at OFFSET,从给定的偏移量处继续/重新开始之前的文件传输;"-C -"自动确定如何继续进行数据传输的起始位置(断点续传),偏移量是指在将源文件传输至目标文件之前需要跳过的确切字节数
  • -d, --data DATA,通过POST请求将指定数据发送至HTTP服务器,其方式与用户在填写 HTML 表单并点击提交按钮时浏览器的操作相同
  • -F, --form CONTENT,这能让curl模拟一个已填写完毕的表单,即用户已按下提交按钮的情况 (H)
  • -H, --header LINE,传送给服务器的自定义头部信息 (H)
  • -h, --help,显示此帮助信息
  • -k, --insecure,允许连接未加密证书的SSL网站 (H)
  • -L, --location,遵循重定向指示 (H)
  • --limit-rate RATE,将传输速度限制为此速率
  • -O, --remote-name,将输出内容写入一个与"远程文件"同名的文件中
  • -o, --output FILE,将输出写入指定文件,而非标准输出
  • -s, --silent,静音模式;不输出任何内容
  • -T, --upload-file FILE,将文件传输至目标位置
  • -u, --user USER[:PASSWORD],服务器用户及密码
  • -v, --verbose,让操作过程更加详细说明
  • -X, --request COMMAND,指定服务器进行通信时使用的自定义请求方法(默认为 GET),可以指定PUT、DELETE、GET、HEAD、POST等请求方法

4.常用用例

4.1.远程服务文件查看与下载

## 1.没有任何参数,直接打印Centos-7.repo文件内容
[root@node2 ~]# curl http://mirrors.aliyun.com/repo/Centos-7.repo    
# CentOS-Base.repo
... ...                      # 文件内容比较多,省略显示,方便看效果
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[root@node2 ~]# 

## 2.-O参数将标准输出写入到文件,文件名称与远程服务里的文件名称一致
[root@node2 ~]# curl -O http://mirrors.aliyun.com/repo/Centos-7.repo 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2523  100  2523    0     0    544      0  0:00:04  0:00:04 --:--:--   544
[root@node2 ~]# ll Centos-7.repo 
-rw-r--r-- 1 root root 2523 Jul 14 17:39 Centos-7.repo
[root@node2 ~]# 

## 3.-o参数指定将标准输出写入到文件的文件名称
[root@node2 ~]# curl -o centos7.repo http://mirrors.aliyun.com/repo/Centos-7.repo 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2523  100  2523    0     0    550      0  0:00:04  0:00:04 --:--:--   550
[root@node2 ~]# ll centos7.repo 
-rw-r--r-- 1 root root 2523 Jul 14 17:40 centos7.repo
[root@node2 ~]# 

## 4.写入文件中断后断点续传
[root@node2 ~]# curl -o test11.txt http://192.168.118.193/dir1/test1.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
 17 1058M   17  190M    0     0   410M      0  0:00:02 --:--:--  0:00:02  410M^C
[root@node2 ~]# ^C                                                                 # ctrl+c断开文件传输
[root@node2 ~]# curl -C - -o test11.txt http://192.168.118.193/dir1/test1.txt      # 自动从上面断开的地方继续传输文件数据
** Resuming transfer from byte position 403791872
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  673M  100  673M    0     0   410M      0  0:00:01  0:00:01 --:--:--  410M
[root@node2 ~]# curl -C - -o test11.txt http://192.168.118.193/dir1/test1.txt      # 文件已经传输完成,再次执行结果都是0
** Resuming transfer from byte position 1109772939
  % 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: (33) HTTP server doesn't seem to support byte ranges. Cannot resume.
[root@node2 ~]# 

4.2.下载限制带宽

# centos6.7测试下,只能限制在17770及以下,单位是字节
# man curl中可以看到指定K、M、G单位,但实际测试不成功
[root@node2 ~]# curl --limit-rate 2048 -o test12.txt http://192.168.118.193/dir1/test1.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0 1058M    0 22272    0     0   1913      0   6d 17h  0:00:11   6d 17h  1543

4.3.上传文件

4.3.1.查看httpd的dav模块

[root@node2 conf.modules.d]# httpd -M | grep dav
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.118.193. Set the 'ServerName' directive globally to suppress this message
 dav_module (shared)
 dav_fs_module (shared)
 dav_lock_module (shared)
[root@node2 conf.modules.d]# 

# /etc/httpd/conf/httpd.conf文件将"ServerName www.example.com:80"注释取消,再次查看dav

[root@node2 conf.modules.d]# httpd -M | grep dav
 dav_module (shared)
 dav_fs_module (shared)
 dav_lock_module (shared)
[root@node2 conf.modules.d]# 
==========================================================================================
对应的模块可在/etc/httpd/conf.modules.d目录查看,dav模块对应文件是00-dav.conf

4.3.2.httpd服务修改配置

# vi /etc/httpd/conf/httpd.conf      # 修改配置文件以下内容
DocumentRoot "/var/www/html"         # 以下添加DAV相关配置

DAVLockDB "/var/www/html/uploads/Dav.lock"
<Directory "/var/www/html/uploads">
    Dav on
    Allow from all
</Directory>

4.3.3.httpd创建上传目录

[root@node2 ~]# mkdir /var/www/html/uploads/
[root@node2 ~]# chown -R apache:apache /var/www/html/uploads/

4.3.4.curl上传文件

[root@node2 ~]# curl -T tfile.txt http://192.168.118.193:80/uploads/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>201 Created</title>
</head><body>
<h1>Created</h1>
<p>Resource /uploads/tfile.txt has been created.</p>
</body></html>
[root@node2 ~]# 
=======================================或者=========================================
[root@node2 ~]# curl -X PUT -T tfile.txt http://192.168.118.193:80/uploads/    # 上传文件
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>201 Created</title>
</head><body>
<h1>Created</h1>
<p>Resource /uploads/tfile.txt has been created.</p>
</body></html>
[root@node2 ~]# 
[root@node2 ~]# ll /var/www/html/uploads/                 # 上传后对应目录会生成以下三个文件
total 4
-rw-r--r-- 1 apache apache  0 Jul 15 15:28 Dav.lock.dir
-rw-r--r-- 1 apache apache  0 Jul 15 15:28 Dav.lock.pag
-rw-r--r-- 1 apache apache 21 Jul 15 15:28 tfile.txt
[root@node2 ~]# 

4.4.httpd服务访问

httpd服务配置方法见Linux(Centos 7.6)yum源配置第二章节;httpd服务配置后一般是通过浏览器比较更直观观察,如下图:

当没有浏览器时,也可通过crul来查看

# 可以看到该http服务下面有dir2/目录和test1.txt文件
[root@node2 dir1]# curl http://192.168.118.193/dir1/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /dir1</title>
 </head>
 <body>
<h1>Index of /dir1</h1>
  <table>
   <tr><th valign="top"><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr>
   <tr><th colspan="5"><hr></th></tr>
<tr><td valign="top"><img src="/icons/back.gif" alt="[PARENTDIR]"></td><td><a href="/">Parent Directory</a>       </td><td>&nbsp;</td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="dir2/">dir2/</a>                  </td><td align="right">2025-07-14 17:57  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/text.gif" alt="[TXT]"></td><td><a href="test1.txt">test1.txt</a>              </td><td align="right">2025-07-14 17:57  </td><td align="right"> 30M</td><td>&nbsp;</td></tr>
   <tr><th colspan="5"><hr></th></tr>
</table>
</body></html>
[root@node2 dir1]# 

4.5.指定用户密码

4.5.1.查看httpd的cgi模块

[root@node2 ~]# httpd -M | grep cgi
 proxy_fcgi_module (shared)
 proxy_scgi_module (shared)
 cgi_module (shared)                      # cgi模块
[root@node2 ~]# 

4.5.2. httpd服务修改配置

# vi /etc/httpd/conf.d/welcome.conf
# 去除-Indexes中的"-",可以直接写到端口访问文件系统(http://192.168.118.193:9000/)
<LocationMatch "^/+$">
    Options -Indexes                     # "Options -Indexes"改为"Options Indexes"
    ErrorDocument 403 /.noindex.html
</LocationMatch>


# vi /etc/httpd/conf/httpd.conf 
DocumentRoot "/var/www/html"

<Directory "/var/www/html/uploads">
    Options Indexes FollowSymLinks
    AllowOverride Authconfig
    AuthType Basic
    AuthName "Login Notice"
    AuthUserFile /etc/httpd/users/.htpasswd
    Require user admin testuser1
</Directory>

4.5.3.httpd服务重启

[root@node2 ~]# systemctl restart httpd.service

 4.5.4.httpd创建用户

[root@node2 ~]# mkdir /etc/httpd/users
[root@node2 ~]# cd /etc/httpd/users/
[root@node2 users]# htpasswd -m -c /etc/httpd/users/.htpasswd admin
New password: 
Re-type new password: 
Adding password for user admin
[root@node2 users]# htpasswd -m /etc/httpd/users/.htpasswd testuser1
New password: 
Re-type new password: 
Adding password for user testuser1
[root@node2 users]# 

4.5.5.httpd服务重启

[root@node2 ~]# systemctl restart httpd.service

4.5.6.curl下载文件

# 直接下载,下载看不出问题,实际看下载文件,内容提示需要用户名和密码
[root@node2 ~]# curl -O http://192.168.118.193:80/uploads/tfile.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   381  100   381    0     0   270k      0 --:--:-- --:--:-- --:--:--  372k
[root@node2 ~]# cat tfile.txt 
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>
[root@node2 ~]# 

# 指定用户名密码下载,内容正确
[root@node2 ~]# curl -u admin:123456 -O http://192.168.118.193:80/uploads/tfile.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    21  100    21    0     0  14593      0 --:--:-- --:--:-- --:--:-- 21000
[root@node2 ~]# cat tfile.txt 
This is testfile.txt
[root@node2 ~]# 

4.6.其他用例

4.6.1.指定请求方式将数据发送HTTP服务器

curl -d "key1=value1&key2=value2" https://httpbin.org/post
curl -X POST -d "key1=value1&key2=value2" https://httpbin.org/post

4.6.2. 指定请求方式将文件内容发送HTTP服务器

curl -F "file=@test.txt" https://httpbin.org/post
curl -X POST -F "file=@test.txt" https://httpbin.org/post

4.6.3.操作过程输出更多信息

curl -v http://example.com/api

 4.6.4.静默输出(无效果)

curl -s http://example.com/api

4.6.5.传送给服务器的自定义头部信息

curl -H "This is header1" http://example.com/api

4.6.6.遵循重定向指示

curl -L http://example.com

4.6.7.允许连接未加密证书的SSL网站

curl -k http://example.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆是浪个

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值