如何在Ubuntu Linux上安装curl命令

时间:2020-01-09 10:39:19  来源:igfitidea点击:

如何解决bash: curl: command not found问题?

如何在Ubuntu Linux版本17.10/19.04/16.04上解决此问题/18.04/20.04 LTS?

curl命令是在Linux或类Unix系统上使用FTP,HTTP,HTTPS,SCP,SFTP,SMB和其他受支持协议从服务器下载文件或数据或向服务器传输文件/数据的工具。

为Ubuntu Linux安装cURL

在Ubuntu Linux上安装cURL的过程如下:

  • 更新您的Ubuntu服务器,运行:``sudo apt update && sudo apt upgrade`
  • 接下来,安装cURL,执行:``sudo apt install curl`
  • 通过运行以下命令在Ubuntu上验证curl的安装:``curl --version`

让我们通过示例详细查看所有命令。

Ubuntu安装curl命令

首先更新系统,并获取Ubuntu的最新稳定curl版本:

$ sudo apt update
$ sudo apt upgrade

执行以下apt命令或apt-get命令:

$ sudo apt install curl

输出示例:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libcurl4
The following NEW packages will be installed:
  curl libcurl4
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 373 kB of archives.
After this operation, 1,036 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libcurl4 amd64 7.58.0-2ubuntu3.7 [214 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 curl amd64 7.58.0-2ubuntu3.7 [159 kB]
Fetched 373 kB in 2s (150 kB/s)
Selecting previously unselected package libcurl4:amd64.
(Reading database ... 164985 files and directories currently installed.)
Preparing to unpack .../libcurl4_7.58.0-2ubuntu3.7_amd64.deb ...
Unpacking libcurl4:amd64 (7.58.0-2ubuntu3.7) ...
Selecting previously unselected package curl.
Preparing to unpack .../curl_7.58.0-2ubuntu3.7_amd64.deb ...
Unpacking curl (7.58.0-2ubuntu3.7) ...
Setting up libcurl4:amd64 (7.58.0-2ubuntu3.7) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up curl (7.58.0-2ubuntu3.7) ...

验证

运行以下命令:

$ curl --version

用法

安装后,您可以按以下方式使用它来查看标题:

$ curl -I https://www.google.co.in/
$ curl -I https://www.theitroad.local/

输出示例:

HTTP/2 200 
date: Mon, 29 Jul 2019 13:13:08 GMT
content-type: text/html; charset=UTF-8
set-cookie: __cfduid=d5292058141c28e3cda2d9501688cff531564405988; expires=Tue, 28-Jul-20 13:13:08 GMT; path=/; domain=.theitroad.local; HttpOnly; Secure
vary: Accept-Encoding
strict-transport-security: max-age=15552000
x-whome: l-cbz02
cf-cache-status: HIT
age: 102433
x-content-type-options: nosniff
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 4fdf59334ab9dcb5-MAA

或使用curl本身从服务器下载文件:

$ curl -o output.file http://server1.theitroad.local/bar.foo.txt

您可以使用curl命令恢复中断的下载,如下所示:

$ curl -L -O -C - http://ftp.ussg.iu.edu/linux/centos/latest/isos/file.iso

在访问URL时更改用户代理:

$ curl -A "USER-AGENT" url
$ curl -A 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0' https://www.theitroad.local

传递HTTP引用也很容易:

$ curl --referer https://www.theitroad.local https://www.theitroad.com

在此示例中,使用curl命令上载.doc文件(HTTP POST请求):

$ curl -F 'file_name=@/home/Hyman/Documents/sales.doc https://server1.theitroad.local/mycloud/