Change User Agent with curl: Easy Steps
In this article
To change the user agent with curl, use the -A or --user-agent option followed by the string you want to send. For example, curl -A "Mozilla/5.0" https://example.com sends a request that identifies as a generic Firefox-like browser.
What Is a User Agent and Why Does It Matter?
Every HTTP request your browser or tool sends includes a User-Agent header. This header is a short text string that tells the web server what kind of client is making the request—typically the browser name, version, and operating system. For example, a typical Chrome user agent on Windows looks like:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Servers use this information to tailor responses—delivering different page layouts, file formats, or even blocking requests that look like bots. When you use curl, the default user agent is curl/8.0.1 (or whatever version you have). Some sites block or alter responses for unknown or non-browser user agents, so changing it can help you fetch content that would otherwise be denied.
How to Set a Custom User Agent in curl
The simplest way is with the -A flag. Here’s the syntax:
curl -A "Your User Agent String" https://example.com
You can also use the long form --user-agent. Both do exactly the same thing. For example, to mimic an iPhone Safari browser:
curl --user-agent "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1" https://example.com
If you want to see what user agent curl is sending by default, use curl -I https://example.com and look at the request headers with -v (verbose) or -H to inspect. The verbose output shows both the request and response headers.
Other Ways to Send a User Agent
Besides -A, you can set any header explicitly with -H. This is useful if you need to send multiple custom headers or if you want to override the user agent in a script that already uses -H for other things.
curl -H "User-Agent: Mozilla/5.0" https://example.com
Both methods produce the same result. The -A flag is just a convenient shorthand. If you use both, the last one on the command line wins.
How to Choose the Right User Agent String
There’s no single “correct” user agent—it depends on what you’re trying to achieve. Common scenarios:
- Accessing a site that blocks curl – Use a common browser string like Chrome or Firefox on Windows or macOS.
- Testing a mobile layout – Use a smartphone user agent (e.g., iPhone or Android).
- Interacting with an API that requires a specific client – Some APIs check the user agent and reject unknown ones. Use the one the API documentation specifies.
- Web scraping – Many sites block non-browser user agents. Using a real browser string reduces the chance of being blocked, but always respect the site’s terms of service and robots.txt.
You can find up-to-date user agent strings online (e.g., on useragentstring.com), but you can also copy the one from your own browser by visiting a site like whatismybrowser.com or checking the network tab in developer tools.
Common Problems and Misconceptions
Myth: Changing the user agent makes you anonymous. It does not. The user agent is just one of many headers your request sends. Your IP address, cookies, and other fingerprinting data remain visible. It only changes what the server sees in that one header.
Problem: The site still blocks you. If a site uses more advanced bot detection (like JavaScript challenges or IP reputation), changing the user agent won’t help. You may need to use a headless browser or a different approach.
Problem: You see a 403 Forbidden error. Some sites block requests that don’t have a browser-like user agent. Try setting one that matches a recent browser. Also check if the site requires other headers like Accept or Referer.
Problem: The user agent string has spaces. Always enclose it in quotes so the shell treats it as a single argument. Forgetting quotes will cause curl to see multiple arguments and fail.
Using curl with a User Agent in Scripts
When you automate requests in bash or Python, you can set the user agent once and reuse it. For example, in a bash script:
UA="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
curl -A "$UA" https://example.com
In Python using the requests library, you can set it per request:
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get('https://example.com', headers=headers)
This is useful for web scraping or testing APIs that require a specific client identifier.
Security and Ethical Considerations
Spoofing a user agent is generally legal, but it can violate a website’s terms of service. If you’re accessing a site that explicitly forbids automated access, changing your user agent to hide the fact that you’re using curl may be considered a breach. Always check the site’s robots.txt and terms before scraping. Also, be aware that some sites use user agent as a security measure—impersonating a browser might be seen as malicious if you’re trying to bypass access controls. Use this technique responsibly, and never use it to circumvent authentication or payment systems.
Frequently Asked Questions
How do I see the user agent curl is sending?
Use curl -v https://example.com. The verbose output shows the request headers, including the User-Agent line. You can also use curl -I to see only the response headers, but that doesn’t show the request. To see exactly what you send, -v is the easiest.
Can I set the user agent permanently in a config file?
Yes. Create or edit ~/.curlrc and add a line like user-agent = "Mozilla/5.0". Then every curl command will use that user agent unless you override it with -A on the command line. This is useful for your own environment, but be careful not to forget it when you actually want the default.
Does changing the user agent affect HTTPS or cookies?
No. The user agent is just a header. It doesn’t change how TLS works or how cookies are handled. Cookies are still stored and sent based on the domain and path, independent of the user agent. However, some sites may set different cookies for different user agents (e.g., mobile vs. desktop), so you might see different behavior.
What is the default user agent for curl?
The default is curl/<version>, for example curl/8.4.0. It includes the curl version but no operating system or browser info. This is often a giveaway that the request is not from a browser, which is why some sites block it.
Can I use a random user agent for each request?
Yes, you can generate a random user agent from a list in your script. However, doing so may look suspicious if the user agent doesn’t match your actual browser or OS. If you’re testing your own site, random user agents can help simulate different clients, but for scraping, it’s better to use a consistent, realistic one.