CURL Linux Command : Learning By Example

Published:28 September 2022 - 5 min. read

Being an IT engineer comes with various tasks to perform, such as checking network connectivity, downloading files, or setting authentications. Usually, these tasks require different tools. Well, not with cURL. The cURL Linux utility helps with most things an IT engineer needs.

In this tutorial, you’ll learn about every command-line feature cURL provides.

Read on and level up your skills with cURL!

Prerequisites

This tutorial comprises step-by-step demonstrations. To follow along, ensure you have the following:

  • A Windows XP+ PC – This tutorial uses Windows 10 Enterprise edition.
  • A user account on the remote Linux SSH host with sudo permissions.

Installing cURL Linux (Ubuntu)

Like any other tool, before taking advantage of cURL, you first have to install it on your machine. You can install cURL on any *nix-based operating system, but it is most popularly used on Ubuntu.

cURL is a command-line utility that allows you to perform tons of tasks as follows:

  • Display the status of various network configurations of Windows machines or Linux servers
  • Transfer data to or from servers
  • Supports multiple protocols, such as HTTP, FTP, IMAP, POP3, SCP, and SFTP.

To install cURL on your Ubuntu machine:

1. Connect to your Ubuntu host with your favorite SSH client.

2. Run the following commands, which don’t provide output but create a directory named ~/install_curl_demo and switch to that directory. This directory will hold all resources you’ll create in this tutorial.

mkdir ~/install_curl_demo
cd ~/install_curl_demo

3. Next, run the below apt update command to update your system package index.

This command ensures the apt package manager can find the proper source when downloading any dependent packages.

sudo apt update 
Updating the Ubuntu machine with the latest packages

4. Now, run the apt install command below to install cURL on your machine.

sudo apt install curl
Installing cURL on the Ubuntu machine
Installing cURL on the Ubuntu machine

5. Finally, run the below command to check the cURL’s version installed on your machine. Doing so ensures that you’ve successfully installed cURL.

curl --version
Checking cURL’s version installed
Checking cURL’s version installed

Authenticating Websites

Different protocols curl support, such as FILE, FTP, HTTP, HTTPS, POP3, SCP, SFTP, TELNET, etc., and you don’t need to work with any user interaction.

If you need to authenticate to any website using the curl command, you will need to specify the user and password in the URL itself. But if you set the user name, cURL will prompt a password.

Run the below curl command, replacing [email protected]:password with your username and password and the website URL with yours to authenticate.


curl -u [email protected]:password --basic <https://wordpress.com/posts/automateinfra.com> 
Authenticating user with password
Authenticating user with password

Downloading Files With Original or Changed Name

Downloading files or packages may be part of your daily routine as an admin. If so, cURL will surely do you a favor. The curl command, appended with either the -O or -o options, lets you download files while keeping the original or setting a different name.

Run the below commands to download the NGINX package with the original name (-O) as (*nginx-1.20.2.tar.gz*) and with a different name (-o) as *mytar.gz*.

# Downloads the NGINX package with the original name nginx-1.20.2.tar.gz
curl -O <http://nginx.org/download/nginx-1.20.2.tar.gz>
# Downloads the NGINX package with the new name mytar.gz
curl -o mytar.gz <http://nginx.org/download/nginx-1.20.2.tar.gz>
Downloading the NGINX package
Downloading the NGINX package

Now, run the following command to download the NGINX package named mytar2.gz. But this time, you’ll only see a progress bar instead of seeing lots of details in the output.

curl -# -o mytar.gz <http://nginx.org/download/nginx-1.20.2.tar.gz>
Downloading the NGINX package but only showing a progress bar
Downloading the NGINX package but only showing a progress bar

Lastly, run the ls command below to list all files in the current directory.

ls

Below, you can verify that nginx-1.20.2.tar.gz, mytar.gz, and mytar2.gz files exist.

Verifying downloaded NGINX packages
Verifying downloaded NGINX packages

Setting Connection Timeout

Fast-loading websites are always ideal, and cURL has a way to test how fast a user can connect to your website. How? By setting a connection timeout, where the curl command tries to connect to a website within a specific timeframe.

Setting a connection timeout lets you determine how long a server is taking to reply to a data request you make.

Run the below command to try and connect to the adamtheautomator.com site for 20 seconds before timing out the request.

curl --connect-timeout 20 -I adamtheautomator.com

You can see below that the -I option lets you include headers in the website’s information output.

Connecting to a website with a connection timeout
Connecting to a website with a connection timeout

Retrieving Data Using the GET Request

You’ve seen setting connection timeout provides information about a website in the “Setting Connection Timeout” section. But if that information is not enough, consider sending a GET request.

Run the curl command below to use an HTTP GET request (–get) and retrieve data from the https://adamtheautomator.com website.

curl --get <https://adamtheautomator.com>

Below, you can see the actual source of the adamtheautomator.com website.

Retrieving data using the Get request
Retrieving data using the Get request

Sending a POST Request

Instead of getting data, you can also send data by sending a POST request with the curl command. The -d parameter allows you to send the specified data in a POST request to the HTTP server like a browser does when a user has filled in an HTML form.

Run the following curl command to send a POST request to the ptsv2.com site.

curl -d "name=curl" <https://ptsv2.com/t/hello>
Sending the POST request on a website
Sending the POST request on a website

Now, navigate to https://reqbin.com/ on your web browser to test if the POST was successful.

Below, you’ll notice the Status shows (200 (OK), which signifies the request was sent successfully.

Verifying the POST request
Verifying the POST request

Searching Word Definitions

Did you know you can also use cURL like a dictionary? Yes! cURL lets you search word definitions by using the DICT protocol. Many IT-related terms can be searched using the DICT protocol, such as clock, ubuntu, RDP, etc.

The syntax of the curl command using the DICT protocol is as follows:

curl [protocol:[dictionary_URL]:[word]

Run each command below to search for word definitions from the dictionary available on the public website dict.org.

# Search word definitions for ubuntu
curl dict://dict.org/d:ubuntu
# Search word definitions for clock
curl dict://dict.org/d:clock

As you can see below, the first word definition search for ubuntu couldn’t find a relevant answer and returned no match. But the second one returned a couple of definitions for the word clock.

Searching the word definition
Searching the word definition

Testing Website HTTP2 Protocol

The curl command is undoubtedly suitable for downloading, uploading, and authenticating websites. But another thing making curl a great tool is that you can check whether a particular URL supports the new HTTP/2 protocol.

Run the following curl command to verify if the https://adamtheautomator.com/ website supports HTTPS protocol (–http2).

curl -I --http2 -s <https://adamtheautomator.com/> | grep HTTP

You’ll see an HTTP/2 200 response in the output like the one below, which signifies the site uses the HTTP2 protocol.

Testing Website HTTP2 protocol using Curl Command
Testing Website HTTP2 protocol using Curl Command

Conclusion

cURL, while a free tool, comes with many features. And in this tutorial, you’ve experienced different first-hand examples of using cURL in Linux. Whether downloading files, authenticating websites, sending POST or GET requests, etc., cURL won’t disappoint you.

Why not build on this newfound knowledge? Perhaps learn how to transfer files, and run commands and scripts using the curl command? Or discover cURL API commands?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!