Curl Set User Agent Command on Linux or Unix

Posted on in Categories , last updated May 8, 2018

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://www.cyberciti.biz/
$ curl -I -A "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" https://www.cyberciti.biz/faq/

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

Sample outputs:
209.244.4.106 - - [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
Curl Set User Agent Using Chrome
Select tiny menu icon on right side > choose More tools > Network conditions:
network conditions chrome
Uncheck the Select automatically user agent check box and you will get drop down menu as follows:
select user agent in chrome
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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 0 comments/add one below):

Join the discussion at www.nixcraft.com