curl Command in Linux: Usage and Examples

By 

Updated on

11 min read

Using the curl command in Linux to transfer data and query APIs

When you need to download a file from the terminal, test an API endpoint, or check why a website is not responding, curl is usually the first tool to reach for. It is a command-line utility for transferring data from or to a server, designed to work without user interaction, and it supports protocols including HTTP, HTTPS, SCP , SFTP , and FTP . With curl, you can resume transfers, limit bandwidth, use proxies, authenticate to servers, and much more.

In this guide, we cover curl with real examples and the options you will use most often.

Installing Curl

The curl package is pre-installed on most Linux distributions today.

To check whether the curl package is installed on your system, open your terminal, type curl, and press Enter. If you have curl installed, the system will print curl: try 'curl --help' or 'curl --manual' for more information. Otherwise, you will see something like curl command not found.

If curl is not installed, you can easily install it using the package manager of your distribution.

Install Curl on Ubuntu and Debian

Terminal
sudo apt update
sudo apt install curl

Install Curl on Fedora, RHEL, and Derivatives

Terminal
sudo dnf install curl

How to Use Curl

The syntax for the curl command is as follows:

txt
curl [options] [URL...]

In its simplest form, when invoked without any option, curl displays the specified resource to the standard output.

For example, to retrieve the example.com homepage, you would run:

Terminal
curl example.com

The command will print the source code of the example.com homepage in your terminal window.

If no protocol is specified, curl tries to guess the protocol you want to use, and it will default to HTTP.

Save the Output to a File

To save the result of the curl command, use either the -o or -O option.

Lowercase -o saves the file with a custom filename:

Terminal
curl -o output.html https://example.com/

Uppercase -O saves the file with its original filename from the URL:

Terminal
curl -O https://go.dev/dl/go1.26.5.linux-amd64.tar.gz

Download Multiple Files

To download multiple files at once, use multiple -O options, followed by the URL to the file you want to download.

In the following example, we are downloading the Arch Linux and Debian ISO files:

Terminal
curl -O https://geo.mirror.pkgbuild.com/iso/latest/archlinux-x86_64.iso \
     -O https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-13.5.0-amd64-netinst.iso

Resume a Download

You can resume a download by using the -C - option. This is useful if your connection drops during the download of a large file, and instead of starting the download from scratch, you can continue the previous one.

For example, if you are downloading the Ubuntu 24.04 ISO file using the following command:

Terminal
curl -O https://releases.ubuntu.com/noble/ubuntu-24.04.4-live-server-amd64.iso

and suddenly your connection drops, you can resume the download with:

Terminal
curl -C - -O https://releases.ubuntu.com/noble/ubuntu-24.04.4-live-server-amd64.iso

Get the HTTP Headers of a URL

HTTP headers are colon-separated key-value pairs containing information such as User-Agent, content type, and encoding. Headers are passed between the client and the server with the request or the response.

Use the -I option to fetch only the HTTP headers of the specified resource:

Terminal
curl -I https://www.ubuntu.com/
output
HTTP/2 301
server: nginx/1.14.0 (Ubuntu)
date: Sat, 11 Jul 2026 14:19:08 GMT
content-type: text/html
content-length: 162
location: https://ubuntu.com/
strict-transport-security: max-age=15724800

Each line is a header field. The first line shows the HTTP version and status code.

Save the Response Headers to a File

If you want to inspect the headers later, use -D to save them to a file:

Terminal
curl -D headers.txt https://www.ubuntu.com/ -o /dev/null

Test if a Website Supports HTTP/2

To check whether a particular URL supports the HTTP/2 protocol , fetch the HTTP headers with -I along with the --http2 option:

Terminal
curl -I --http2 -s https://linuxize.com/ | grep HTTP

The -s option tells curl to run in silent mode and hide the progress meter and error messages.

If the remote server supports HTTP/2, curl prints HTTP/2 200:

output
HTTP/2 200

Otherwise, the response is HTTP/1.1 200:

output
HTTP/1.1 200 OK

If you have curl version 7.47.0 or newer, you do not need to use the --http2 option because HTTP/2 is enabled by default for all HTTPS connections.

You can test HTTP/3 the same way. If your curl build includes HTTP/3 support, replace --http2 with --http3 and look for HTTP/3 200 in the output.

Show Status Code and Timing

To see the status code and basic timing information without printing the response body, use -w with -o /dev/null:

Terminal
curl -sS -o /dev/null -w "status=%{http_code} time=%{time_total}s\n" https://linuxize.com/

Follow Redirects

By default, curl does not follow the HTTP Location headers.

If you try to retrieve the non-www version of google.com, you will notice that instead of getting the source of the page you will be redirected to the www version:

Terminal
curl -I google.com
output
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8

The 301 status and Location header show that google.com redirects to www.google.com. By default, curl stops here and does not follow the redirect.

The -L option instructs curl to follow any redirect until it reaches the final destination:

Terminal
curl -L google.com

Change the User-Agent

Sometimes when downloading a file, the remote server may be set to block the curl User-Agent or to return different contents depending on the visitor device and browser.

In situations like this, to emulate a different browser, use the -A option.

For example, to emulate Firefox, you would use:

Terminal
curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0" https://fedoraproject.org/

Specify a Maximum Transfer Rate

The --limit-rate option allows you to limit the data transfer rate. The value can be expressed in bytes, kilobytes with the k suffix, megabytes with the m suffix, and gigabytes with the g suffix.

In the following example, curl will download the Go binary and limit the download speed to 1 MB:

Terminal
curl --limit-rate 1m -O https://go.dev/dl/go1.26.5.linux-amd64.tar.gz

This option is useful to prevent curl from consuming all the available bandwidth.

Transfer Files via FTP

To access a protected FTP server with curl, use the -u option and specify the username and password as shown below:

Terminal
curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/

Once logged in, the command lists all files and directories in the user’s home directory.

You can download a single file from the FTP server using the following syntax:

Terminal
curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.tar.gz

To upload a file to the FTP server, use the -T followed by the name of the file you want to upload:

Terminal
curl -T newfile.tar.gz -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/
Warning
Credentials passed with -u are saved in your shell history and visible to other users in the process list. FTP also transmits them in plain text, so prefer SFTP or FTPS when the server supports it.

Send Cookies

Sometimes you may need to make an HTTP request with specific cookies to access a remote resource or to debug an issue.

By default, when requesting a resource with curl, no cookies are sent or stored.

To send cookies to the server, use the -b switch followed by a filename containing the cookies or a string.

For example, to send a session cookie with your request:

Terminal
curl -b "session_id=abc123" https://httpbin.org/cookies

You can also save cookies from a response and reuse them in subsequent requests:

Terminal
# Save cookies to a file
curl -c cookies.txt https://example.com/login

# Send saved cookies with the next request
curl -b cookies.txt https://example.com/dashboard

Request JSON Data

When you are working with APIs, set the Accept header and keep the output clean:

Terminal
curl -sS -H "Accept: application/json" https://api.github.com/repos/curl/curl

Send Basic Auth Credentials

If a server requires basic authentication, use -u with the username:

Terminal
curl -u "$API_USER" https://api.example.com/protected

When you omit the password, curl prompts you to enter it without displaying it in the terminal.

Using Proxies

curl supports different types of proxies, including HTTP, HTTPS, and SOCKS. To transfer data through a proxy server, use the -x (--proxy) option, followed by the proxy URL.

The following command downloads the specified resource using a proxy on 192.168.44.1 port 8888:

Terminal
curl -x 192.168.44.1:8888 https://www.linux.com/

If the proxy server requires authentication, use the -U (--proxy-user) option with the proxy username:

Terminal
curl -U "$PROXY_USER" -x 192.168.44.1:8888 https://www.linux.com/

With no password in the argument, curl prompts for the proxy password.

Send POST Requests

By default, curl sends GET requests. To send data with a POST request, use the -d option. When -d is present, curl switches to POST automatically, so there is no need to add -X POST:

Terminal
curl -d "name=linuxize&category=linux" https://httpbin.org/post

To send JSON data, set the Content-Type header:

Terminal
curl -H "Content-Type: application/json" \
  -d '{"name":"linuxize","category":"linux"}' \
  https://httpbin.org/post

You can also read data from a file using @:

Terminal
curl -H "Content-Type: application/json" -d @data.json https://httpbin.org/post

Set Timeouts

To prevent curl from hanging indefinitely, you can set timeouts:

Terminal
# Maximum time for the entire operation (30 seconds)
curl --max-time 30 https://example.com/

# Maximum time to establish a connection (10 seconds)
curl --connect-timeout 10 https://example.com/

# Combine both limits
curl --connect-timeout 10 --max-time 60 https://example.com/

Verbose Output

The -v option shows the full request and response exchange, including the TLS handshake, request headers, and response headers. It is the most useful flag for debugging connection issues:

Terminal
curl -v https://example.com/ -o /dev/null -s
output
* Host example.com:443 was resolved.
* IPv4: 172.66.147.243, 104.20.23.154
*   Trying 172.66.147.243:443...
* ALPN: curl offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN: server accepted h2
* Server certificate:
*  subject: CN=example.com
*  issuer: C=US; O=CLOUDFLARE, INC.; CN=Cloudflare TLS Issuing ECC CA 1
*  SSL certificate verify ok.
* Connected to example.com (172.66.147.243) port 443
> GET / HTTP/2
> Host: example.com
> User-Agent: curl/8.14.1
> Accept: */*
>
< HTTP/2 200
< content-type: text/html
< server: cloudflare

Lines starting with * are connection details from curl itself. Lines with > are the request headers sent to the server, and lines with < are the response headers coming back.

The -o /dev/null -s part discards the response body and hides the progress bar so only the debug output remains.

Troubleshooting

When a transfer fails, curl prints an error code that points to the stage where things broke down. These are the errors you are most likely to run into.

curl: (6) Could not resolve host
The DNS lookup failed. Check the URL for typos and confirm that name resolution works on your system, for example by testing another domain with dig or ping.

curl: (7) Failed to connect
The hostname resolved, but nothing accepted the connection. The service may be down, listening on a different port, or blocked by a firewall.

curl: (28) Operation timed out
The transfer exceeded the limit set with --connect-timeout or --max-time, or the server stopped responding. Raise the limit for slow servers or large downloads.

curl: (60) SSL certificate problem
curl cannot verify the server certificate. Check that the URL hostname is correct, your system clock is accurate, and your CA certificate bundle is current. The certificate may also be expired, self-signed, or issued for a different hostname. For local testing only, -k bypasses this error, but it disables certificate verification and should never be used for production connections.

curl: (22) The requested URL returned error
This appears only with the -f option and means the server responded with HTTP status 400 or above. To see the exact status code, run curl -sS -o /dev/null -w "%{http_code}\n" against the URL.

Quick Reference

For a printable quick reference, see the curl cheatsheet .

TaskCommand
Fetch a URLcurl example.com
Save to file (custom name)curl -o file.html example.com
Save to file (original name)curl -O https://example.com/file.tar.gz
Download multiple filescurl -O url1 -O url2
Resume a downloadcurl -C - -O url
Fetch HTTP headers onlycurl -I example.com
Follow redirectscurl -L example.com
Verbose outputcurl -v example.com
POST form datacurl -d "key=value" url
POST JSONcurl -H "Content-Type: application/json" -d '{}' url
Basic authcurl -u "$API_USER" url
Set timeoutcurl --max-time 30 url
Limit bandwidthcurl --limit-rate 1m -O url
Use a proxycurl -x proxy:port url
Change User-Agentcurl -A "agent string" url
Send cookiescurl -b "name=value" url
Silent with errorscurl -sS url

FAQ

What is the difference between curl and wget?
Both download files from the command line. curl supports more protocols, handles uploads, and is better suited for API work and scripting with pipes. wget is simpler for recursive downloads and mirroring websites. See the curl vs wget comparison for side-by-side examples.

How do I send a bearer token with curl?
Pass the token in an Authorization header:

Terminal
curl -H "Authorization: Bearer $API_TOKEN" https://api.example.com/data
Warning
Do not hardcode API tokens in scripts or commit them to version control. Store them in an environment variable or a local .env file that is ignored by Git.

Conclusion

The curl command handles everything from simple file downloads to API debugging with verbose output and authentication. For the full option list, run man curl or visit the curl documentation .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page