How to Install and Use curl in Linux

curl is a command-line tool for making requests across the internet. It can be used to either get data from or post data to a remote server. It works with the HTTP, HTTPS, SCP, SFTP and FTP protocols and can be configured to resume and run on limited bandwidth as well as make authenticated and proxied connections.

 

In this guide, we will learn how to install and use curl in the Linux command-line. We will also cover some practical examples that you may want to use curl for.

 

How to Install curl

curl comes with almost every Linux distribution so it is unlikely you will need to install it. To test if curl is installed type curl into your command-line

 

curl
curl: try 'curl --help' or 'curl --manual' for more information

 

You should see something like the above if curl is installed. If you get "command not found" or something along those lines you can install it by running the following commands on Ubuntu or Debian:

 

sudo apt update
sudo apt install curl

 

or on Fedora and CentOS:

 

sudo yum install curl

 

The curl Syntax

curl takes an optional set of options followed by the remote URL.

 

curl [options] [URL]

 

How to Perform an HTTP GET Request

The most basic way to use curl is by performing an HTTP GET request. To do that type curl followed by a domain name into the command-line.

 

curl https://www.example.com

 

Return the HTTP Response Headers

If you need to get the HTTP response headers from a GET request supply the -i flag before the domain.

 

curl -i https://www.example.com

 

To return the headers only, capitalise the -I flag.

 

curl -I https://www.example.com

 

Save the Output to a File

In some cases, we will want to download the contents of a remote address and save it to a file locally. This is done by adding the -o flag followed by the name of the file to save and finally the remote resource.

 

curl -o jquery.js https://code.jquery.com/jquery-3.5.1.min.js

 

To save a file while keeping its original name capitalise the -0 flag:

 

curl -O jquery.js https://code.jquery.com/jquery-3.5.1.min.js

 

Resume a Download

Internet connections go down from time to time and if you are in the middle of downloading a large file using curl the process will break. Fortunately, curl can resume the download when you are back online if you add the  -C - option.

 

curl -C - -O jquery.js https://code.jquery.com/jquery-3.5.1.min.js

 

Follow Redirects

If you don't know the canonical address for a domain you can add the -L option to follow redirects until it reaches the final destination.

 

curl -L www.example.com

 

Use a Custom User-Agent

Maybe the site you are trying to access only allows certain user-agents to connect, or you need to test your own systems based on different user-agents - curl can connect with a custom user-agent for those cases. Add the -A flag followed by the user-agent in "" (quotation marks).

 

curl -A "Mozilla/5.0 (Linux; Android 10; SM-A505U1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Mobile Safari/537.36 X-Middleton/1" www.example.com

 

Set Bandwidth Limit

Sometimes it may be necessary to limit the transfer speed of a curl download to save bandwidth for other tasks. To do this add the --limit-rate flag followed by the bandwidth limit. This can in b (bytes), k (kilobytes), m (megabytes) or g (gigabytes.)

 

curl --limit-rate 2m -O -L https://go.skype.com/mac.download

 

How to Perform an HTTP POST request

You can change the request method using the -X flag followed by the name of the request type. You could perform an HTTP POST like this:

 

curl -X POST https://www.skillsugar.com

 

To pass data with the POST request, use the -d flag and put the data inside "" (quotation marks).

 

curl -d "name=john&age=22" -X POST https://www.skillsugar.com

 

note - you can pass data from a file on your machine by using -d "@path-to-file.txt"

 

How to Perform an HTTP PUT request

A PUT request is done in the same way POST in curl

 

curl -X PUT https://www.skillsugar.com

 

How to use HTTP Authentication

You can access URL's that require HTTP authentication using the -u flag followed by your username and password separated by : (colon).

 

curl -u username:password https://www.skillsugar.com

 

Transfer Files Using FTP

As mentioned at the beginning, curl can work with multiple protocols. To use them we just have to supply the correct protocol in the address. Since FTP will almost certainly require authentication VIA a username and password let's also supply them in the example.

 

curl -u username:password ftp://ftp.skillsugar.com

 

After logging in you will see a list of files and directories. To download a file using FTP supply a path to it.

 

curl -u username:password ftp://ftp.skillsugar.com

 

Uploading is done by passing the name of the file you wish to upload after the -T flag.

 

curl -T archive.zip -u username:password ftp://ftp.skillsugar.com

 

Connect VIA Proxy

It is possible to transfer data VIA a proxy using curl. This is done by passing in the -x flag followed by the address and port of the proxy.

 

curl -x 25.3.206.95:8080 https://skillsugar.com

 

If you need to authenticate yourself do so with the -u flag followed by a : (colon) separated username and password.

 

curl -u username:password -x 25.3.206.95:8080 https://skillsugar.com

 

In some cases, you will need to supply a cookie to access data on a server. You can supply a cookie to send after the -b flag. This can either be a string or a file on your machine

 

curl -b "letmein=true" https://www.example.com

 

Conclusion

You now know how to use curl in a variety of different ways. curl can be super useful for testing web applications you are working on or for things such as programmatically uploading/downloading multiple files. It is certainly a program you should spend the time getting used to.

terminal