I need to set the user agent header for curl command. How can I set or change user agent with curl command on Linux or Unix like system?
A user agent is nothing but software agent that is acting on behalf of a user. When you use Firefox on Ubuntu Linux, it sends the “Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0” as a user agent to the web server. Same way the curl command uses “curl/7.55.1” as a user agent. This page shows how to set or change curl command user agent. You can use curl command get headers or view source code of any website URL. Some sites may block user agent curl. Others may just display different content based on operating system, browser version, and user agent.
Change user agent with curl
The syntax is as follows for the curl command:
curl -A "user-agent-name-here" url
curl --user-agent "user-agent-name-here" url
curl -H "User-Agent: user-Agent-Name-Here"
Curl set user agent to “Firefox”
I am going to set user agent as “Firefox 59 on Windows NT 6.1”:
$ curl -A "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" https://sxi.io/
$ curl -I -A "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" https://sxi.io/
The above command will log the following entry in my webserver. You can use the cat command or grep command or tail command as follows:
$ grep 'IP address' /var/log/nginx/access.log
$ tail -f /var/log/nginx/access.log
184.108.40.206 - - [08/May/2018:14:59:30 +0000] "GET /faq/ HTTP/2.0" 200 8183 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0"
Curl Set User Agent to Safari
curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (K HTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" url
How to change user Agent with browser
You can always use Firefox, Safari or Chrome to set user agent. So no need to use the curl command.
How to change user agent in Google Chrome
First, select More tools > Developer tools
Select tiny menu icon on right side > choose More tools > Network conditions:
Uncheck the Select automatically user agent check box and you will get drop down menu as follows:
The user agent string will be changed on the current tab and remains only active when the Developer Tools are kept open.
How to change user agent in Firefox
The User Agent Switcher extension adds a menu and a toolbar button to switch the user agent of a browser. Click here to download the same. Another option is to use the about:config page typed in url bar and set general.useragent.override. See this page for more info.