The curl Command in Linux
Last Updated: 2025-01-01
2 min read
Overview
curl (Client URL) is a command-line tool for transferring data using various protocols, most commonly HTTP and HTTPS. It’s invaluable for testing web applications, checking server responses, and downloading files directly from the terminal.
Basic Syntax
curl [options] [URL]
Common Usage Examples
Fetch a webpage
curl https://example.com
This outputs the raw HTML to the terminal.
Download a file
curl -O https://example.com/backup.tar.gz
-O saves the file with its original filename. To specify a custom name:
curl -o myfile.tar.gz https://example.com/backup.tar.gz
Follow redirects
Many URLs redirect (301/302). By default, curl does not follow them:
curl -L https://example.com
-L tells curl to follow redirects until it reaches the final destination.
Checking HTTP Headers
View response headers only
curl -I https://example.com
Sample output:
HTTP/2 200
content-type: text/html; charset=UTF-8
server: nginx
cache-control: max-age=3600
View headers AND body
curl -i https://example.com
This is useful for debugging caching, content-type, or security headers.
Making POST Requests
Send form data
curl -X POST -d "username=admin&password=secret" https://example.com/login
Send JSON data
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name": "John", "email": "john@example.com"}' \
https://api.example.com/users
Useful Flags Reference
| Flag | Description |
|---|---|
-O | Save file with original filename |
-o FILE | Save output to a specified file |
-L | Follow redirects |
-I | Fetch headers only (HEAD request) |
-i | Include response headers in output |
-X METHOD | Specify HTTP method (GET, POST, PUT, DELETE) |
-d DATA | Send data in a POST request |
-H HEADER | Add a custom header |
-u user:pass | HTTP basic authentication |
-k | Allow insecure SSL connections (skip certificate verification) |
-s | Silent mode — suppress progress bar |
-v | Verbose — show full request/response details |
-w FORMAT | Write output format after completion |
--max-time N | Set maximum time in seconds for the request |
-C - | Resume a partially downloaded file |
Practical Hosting Examples
Test if your site is responding
curl -sI https://yourdomain.com | head -5
Check your server’s external IP
curl ifconfig.me
Measure response time
curl -o /dev/null -s -w "Time: %{time_total}s\nHTTP Code: %{http_code}\n" https://yourdomain.com
Download and extract in one step
curl -L https://example.com/archive.tar.gz | tar -xzf -
Test an API endpoint with authentication
curl -u apiuser:apikey https://api.example.com/v1/status
Check SSL certificate details
curl -vI https://yourdomain.com 2>&1 | grep -E "subject:|expire|issuer:"
Tips
- Use
-v(verbose) when debugging unexpected responses — it shows the full request and response cycle including headers and SSL handshake. - Combine
-s(silent) with-o /dev/nullwhen you only care about status codes or timing, not the body. - If you get SSL errors, don’t default to
-kin production. Instead, investigate and fix the certificate issue.
Tags:
ssh
linux
curl
http
networking