Mastering curl on Ubuntu 24: Essential Commands and Use Cases
Whether you're a developer, system administrator, or a Linux enthusiast, curl is an indispensable tool when it comes to interacting with web resources directly from your terminal. On Ubuntu 24, mastering curl
can simplify your workflow for downloading files, testing APIs, or troubleshooting network problems without relying on a browser or heavy GUI applications.
In this comprehensive guide, you'll learn everything from the basics of installing curl to advanced commands and real-world use cases that will boost your productivity.
What is curl?
curl
(short for Client URL) is a command-line utility designed to transfer data to or from a server using a variety of protocols, including HTTP, HTTPS, FTP, SCP, and more. It supports a plethora of options that allow fine control over requests — making it a favorite tool for developers and sysadmins alike.
Originally released in 1997, curl has grown into one of the most versatile command-line tools available for Linux distributions like Ubuntu.
Installing curl on Ubuntu 24
Ubuntu 24 typically ships with curl pre-installed, but it's easy to check and install if necessary.
To check if curl is installed, open your terminal and type:
curl --version
You should see version information if it's installed. If not, run:
sudo apt update
sudo apt install curl
This installs curl and any required dependencies.
Basic curl Usage
Let's start with the basics.
1. Fetch a web page
The simplest curl command fetches a webpage's content and prints it to your terminal:
curl https://example.com
This will output the HTML source code of the website.
2. Save output to a file
To save the output instead of printing it, use the -o
flag:
curl -o homepage.html https://example.com
Now the website's HTML is saved as homepage.html
on your disk.
Essential curl Commands & Use Cases
Here are some common but powerful ways to use curl on Ubuntu 24.
1. Download files efficiently
Use the -O
flag (capital O) to download and save a file with its original name:
curl -O https://example.com/archive.tar.gz
This downloads and saves the file as archive.tar.gz
.
You can also resume interrupted downloads with -C -
:
curl -C - -O https://example.com/largefile.zip
This is handy if your connection drops during a big download.
2. Show HTTP response headers only
Sometimes you want to inspect server response headers:
curl -I https://example.com
This command returns headers like HTTP status, content-type, server info, and more — useful for debugging or checking if redirects are in place.
3. Follow redirects automatically
If a website redirects you, add -L
to follow the redirect chain:
curl -L http://example.com
Without -L
, curl will show only the redirect response, not the final page.
4. Pass query parameters with GET requests
If you need to call an API endpoint with query parameters, just include them in the URL:
curl "https://jsonplaceholder.typicode.com/users"
This public test API returns a list of fake users in JSON format — ideal for demos and testing.
You can also test with httpbin.org
, which echoes your query parameters:
curl "https://httpbin.org/get?active=true&sort=desc"
Make sure to quote URLs with special characters or ampersands.
5. Use Basic Authentication
If the server requires a username and password, use -u
:
curl -u username:password https://httpbin.org/basic-auth/username/password
This works with HTTPBin's basic auth test endpoint.
6. Set timeouts for slow connections
You can limit how long curl waits with:
curl --max-time 15 https://example.com
This prevents curl from hanging indefinitely on slow or unresponsive servers.
7. Send a POST request with data
To send data (e.g., form fields or JSON), use -d
with the appropriate content type:
curl -X POST -H "Content-Type: application/json" \
-d '{"name": "Alice", "email": "alice@example.com"}' \
https://httpbin.org/post
Advanced curl Tips
Verbose output for debugging
Use -v
to get detailed information about the request and response, including headers and SSL info:
curl -v https://example.com
This helps troubleshoot issues like SSL handshake failures or connection resets.
Using cookies
Save cookies from a session:
curl -c cookies.txt https://httpbin.org/cookies/set?name=value
Send cookies back in subsequent requests:
curl -b cookies.txt https://httpbin.org/cookies
This is helpful when automating login and session-based workflows.
Upload files with FTP
Curl supports uploading files with FTP or SFTP:
curl -T file.txt ftp://ftp.example.com/ --user username:password
Customize user-agent
Sometimes servers block unknown clients. You can spoof your browser's user agent like this:
curl -A "Mozilla/5.0 (X11; Ubuntu; Linux x86_64)" https://example.com
Real-World Use Cases for curl
API Testing
Developers often use curl to quickly test RESTful APIs during development:
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://httpbin.org/bearer
You can chain requests, send different HTTP methods, and inspect raw responses.
For learning API testing, see this curl API tutorial.
Automated Backups and Downloads
Use curl in scripts to automate downloading databases, website backups, or remote files:
#!/bin/bash
curl -O https://example.com/latest-backup.tar.gz
Schedule this script with cron jobs to run daily.
Troubleshooting Network Issues
By combining verbose output and header inspection, curl helps diagnose connection problems or unexpected server responses.
Common curl Errors and How to Fix Them
SSL certificate errors
Error:
curl: (60) SSL certificate problem: unable to get local issuer certificate
Fix: If you trust the server, bypass SSL verification (not recommended for production):
curl -k https://example.com
Or install the missing CA certificates:
sudo apt install ca-certificates
sudo update-ca-certificates
For more on SSL errors, see curl SSL troubleshooting.
Connection refused or timeout
If curl can't reach the server, check your network, firewall, and proxy settings. Also, verify the URL is correct.
Summary and Next Steps
You've now seen how curl
can:
- Fetch webpages and save files
- Inspect server headers
- Follow redirects
- Send GET and POST requests
- Handle authentication and cookies
- Debug requests with verbose mode
- Automate downloads and API testing
With these skills on Ubuntu 24, you can confidently use curl for a wide range of tasks—whether troubleshooting network issues, interacting with APIs, or automating workflows.
If you want to level up further, consider learning:
How to use jq
to parse JSON output from curlwget
for more advanced download optionshttpie
— a user-friendly HTTP client alternative to curl