How-to

The curl Command, a Working Guide

curl talks to servers in dozens of protocols and shows exactly what happened. These are the habits and flags that make it dependable in daily admin work.

What is curl?

A command-line tool for transferring data with URLs, built on the libcurl library. It speaks HTTP, HTTPS, FTP, SFTP, SCP, SMTP, IMAP, MQTT and more, and it runs on every operating system you are likely to meet. Package managers, installers and monitoring agents use it constantly, so it is present on every Linux image we install. Ubuntu 24.04 ships curl 8.5 and Debian 13 ships curl 8.14; both support everything below.

How do I fetch and save?

curl https://api.example.net/status               # print to the terminal
curl -O https://downloads.example.org/tool-3.2.tar.gz    # save under the remote name
curl -o /tmp/status.json https://api.example.net/status  # save under a chosen name
curl -C - -O https://downloads.example.org/big.iso       # resume an interrupted download

For many or very large files, wget's defaults may suit better; the comparison is in wget vs curl.

Which flags make scripts reliable?

FlagEffect
-sNo progress meter
-SStill show errors when silent
-f / --fail-with-bodyNon-zero exit on HTTP 400 and above
-LFollow redirects
--max-time 20Give up after 20 seconds in total
--connect-timeout 5Give up connecting after 5 seconds
--retry 3 --retry-delay 2Retry transient failures

A dependable default for scripts is curl -fsSL --max-time 30 --retry 3. Without -f, a 500 error page is treated as success and saved as if it were the file you wanted; without a timeout, a hung server can block a cron job for hours.

How do I see where time goes?

The -w option prints variables after the transfer. A timing breakdown answers "is it DNS, the network or the application?":

curl -sS -o /dev/null -w '@-' https://shop.example.net/ <<'EOF'
lookup:   %{time_namelookup}s
connect:  %{time_connect}s
tls:      %{time_appconnect}s
ttfb:     %{time_starttransfer}s
total:    %{time_total}s  code %{http_code}  size %{size_download}
EOF

A high ttfb with fast connect and TLS points at the application or database. A high connect points at distance or the network; compare cities with The ping command.

How do I debug a request?

-v shows the connection, TLS handshake, request headers and response headers. --trace-ascii - adds the full bodies. -I fetches only headers. Most HTTP mysteries, such as wrong redirects, missing compression or caching surprises, become obvious in verbose output. Header work is covered in Headers with curl, and certificate errors in curl and certificate errors.

How do I test a site on a new machine?

curl -sSI --resolve shop.example.net:443:203.0.113.25 https://shop.example.net/

--resolve overrides DNS for one request while keeping the real host name for TLS and virtual hosts. Use it to confirm the new box serves the right site with the right certificate before you change DNS, then repeat it afterwards against the old address to confirm nothing still depends on it.

Can curl limit its own speed?

Yes: --limit-rate 20M caps the transfer at about 20 MB per second. On a machine with a 10 or 25 Gbps port, a big download can otherwise saturate the link and slow everything else. It also helps keep large pushes from production within the monthly outgoing allowance of 15 TB per machine, or 5 TB in Singapore.

Which other jobs does curl do well?

  • Sending requests with bodies and authentication: Send POST requests with curl.
  • Keeping sessions across requests: Cookies with curl.
  • Following and auditing redirects: Follow redirects with curl.
  • Uploading to SFTP servers: curl -T report.csv -u mirror: --key ~/.ssh/mirror_ed25519 sftp://198.51.100.12/incoming/, when your curl build includes SSH support.
  • Parallel transfers with -Z, for example curl -Z -O url1 -O url2 -O url3.

Which exit codes should scripts watch?

curl returns 0 on success, 6 when the host name cannot be resolved, 7 when the connection fails, 22 for HTTP errors with -f, 28 on a timeout and 60 for certificate problems. Logging the code alongside the URL turns a vague "download failed" into an exact cause.

Keep Reading

Headers with curl

Show response headers, send or remove request headers, pick single header values and test caching, compression and virtual hosts with curl.

Open

Send POST requests with curl

POST forms, JSON, files and raw bodies with curl, choose the right content type and read the response status for scripts.

Open

wget vs curl

Two download tools with different strengths: wget for recursive and resumable fetching, curl for talking to APIs in any protocol.

Open

Set a curl user agent

Change or remove the User-Agent header curl sends, test how your own server treats different clients and read agents in your logs.

Open

FAQ

Quick Answers

Fail on HTTP errors, silent mode, show errors anyway and follow redirects: a reliable combination for scripts.

Use -O to keep the remote name or -o with your own file name.

Use -w with variables such as %{time_starttransfer} and %{time_total}.

Use --connect-timeout for connecting and --max-time for the whole transfer.

Yes. -Z runs transfers in parallel.