cUrls's option "-u"
Categories:
Mastering cURL's -u Option for HTTP Authentication

Learn how to use cURL's -u
(user) option to provide credentials for various HTTP authentication methods, including Basic and Digest authentication.
The curl
command-line tool is a powerful utility for transferring data with URLs. One of its most frequently used options is -u
(or --user
), which allows you to specify a username and password for server authentication. This is crucial when interacting with APIs, web services, or protected web pages that require credentials before granting access. Understanding how to correctly use this option is fundamental for many network-related tasks.
Basic HTTP Authentication with -u
The most common use case for the -u
option is Basic HTTP Authentication. In this method, the username and password are sent in plain text (Base64 encoded) with each request. While simple to implement, it's important to note that Basic authentication is not secure over unencrypted connections (HTTP) as credentials can be easily intercepted. Always use HTTPS when employing Basic authentication in production environments.
curl -u "username:password" https://api.example.com/data
Example of cURL with Basic HTTP Authentication
When you provide the username and password separated by a colon, cURL automatically handles the Base64 encoding and includes the Authorization
header in the request. If you omit the password, cURL will prompt you to enter it interactively, which is a good practice for security, especially in scripts where hardcoding passwords is undesirable.
curl -u "myuser" https://api.example.com/secure-resource
Enter host password for user 'myuser': ******
cURL prompting for password interactively
Understanding Authentication Flow
When a server requires authentication, it typically responds with a 401 Unauthorized
status code and a WWW-Authenticate
header, indicating the type of authentication required (e.g., Basic, Digest). cURL, upon receiving this, will retry the request with the provided credentials using the specified authentication method. If no method is explicitly specified, cURL will attempt Basic authentication by default.
sequenceDiagram participant Client participant Server Client->>Server: GET /protected-resource Server-->>Client: 401 Unauthorized (WWW-Authenticate: Basic realm="Restricted") Client->>Server: GET /protected-resource (Authorization: Basic <base64-encoded-credentials>) Server-->>Client: 200 OK (Protected Resource Content)
HTTP Basic Authentication Flow
Digest Authentication and Other Methods
While -u
defaults to Basic authentication, cURL can handle other authentication schemes like Digest authentication, which is more secure than Basic as it does not send the password in plain text. To explicitly specify Digest authentication, you can combine -u
with the -anyauth
or -digest
options.
curl --digest -u "username:password" https://api.example.com/digest-auth
# Or let cURL figure it out (less explicit but often works)
curl --anyauth -u "username:password" https://api.example.com/any-auth
Using cURL with Digest authentication
The --anyauth
option tells cURL to figure out the best authentication method the server supports and use it. This is often convenient but can sometimes lead to unexpected behavior if the server supports multiple methods and cURL picks one you didn't intend. For precise control, explicitly specify the method (e.g., --digest
, --ntlm
, --negotiate
).
--anyauth
in sensitive environments. It's generally safer to explicitly specify the authentication method (e.g., --digest
) if you know what the server expects, to prevent fallback to less secure methods.