How to use curl command with proxy username and password on Linux or Unix

Posted on in Categories , , , , last updated January 17, 2016

My sysadmin provided me the following proxy details:
       Port: 3128
       Username: foo
       Password: bar

The settings worked perfectly with Google Chrome and Firefox browser. How do I use it with the curl command? How do I tell the curl command to use my proxy settings from Google Chrome browser?

Many Linux and Unix command line tools such as curl, wget, lynx, and more; use the environment variable called http_proxy, https_proxy, ftp_proxy to find the proxy details. It allows you to connect text based session and applications via the proxy server.

Unix and Linux curl command with proxy syntax

The syntax is:

export http_proxy=http://your-ip-address:port/
## http proxy with username and password 
export http_proxy=http://user:password@your-proxy-ip-address:port/
## HTTPS version ##
export https_proxy=https://your-ip-address:port/
export https_proxy=https://user:password@your-proxy-ip-address:port/

Another option is to pass the -x option to the curl command. To use the specified proxy:

curl -x <[protocol://][user:password@]proxyhost[:port]> url
--proxy <[protocol://][user:password@]proxyhost[:port]> url
--proxy http://user:password@Your-Ip-Here:Port url
-x http://user:password@Your-Ip-Here:Port url


First set the http_proxy:

export http_proxy=http://foo:bar@
export https_proxy=$http_proxy
## Use curl command ##
curl -I
curl -v -I

Sample outputs:

* Rebuilt URL to:
*   Trying
* Connected to 1202.54.1.1 ( port 3128 (#0)
* Proxy auth using Basic with user 'foo'
> Host:
> Proxy-Authorization: Basic x9VuUml2xm0vdg93MtIz
> User-Agent: curl/7.43.0
> Accept: */*
> Proxy-Connection: Keep-Alive
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Server: nginx
Server: nginx
< Date: Sun, 17 Jan 2016 11:49:21 GMT
Date: Sun, 17 Jan 2016 11:49:21 GMT
< Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8
< Vary: Accept-Encoding
Vary: Accept-Encoding
< X-Whom: Dyno-l1-com-cyber
X-Whom: Dyno-l1-com-cyber
< Vary: Cookie
Vary: Cookie
< Link: <>; rel=""
Link: <>; rel=""
< X-Frame-Options: SAMEORIGIN
X-Frame-Options: SAMEORIGIN
< X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
X-XSS-Protection: 1; mode=block
< X-Cache: MISS from server1
X-Cache: MISS from server1
< X-Cache-Lookup: MISS from server1:3128
X-Cache-Lookup: MISS from server1:3128
< Connection: keep-alive
Connection: keep-alive
* Connection #0 to host left intact

In this example, I’m downloading a pdf file:
$ export http_proxy="vivek:[email protected]:3128/"
$ curl -v -O

OR use the -x option:
curl -x 'http://vivek:[email protected]:3128' -v -O
Sample outputs:

Fig.01: curl in action (click to enlarge)
Fig.01: curl in action (click to enlarge)

How to use socks protocol?

The syntax is same:

curl -x socks5://[user:password@]proxyhost[:port]/ url
curl --socks5

Remember, the proxy string can be specified with a protocol:// prefix to specify alternative proxy protocols. Use socks4://, socks4a://, socks5:// or socks5h:// to request the specific SOCKS version to be used. No protocol specified, http:// and all others will be treated as HTTP proxies. If the port number is not specified in the proxy string, it is assumed to be 1080. The -x option overrides existing environment variables that set the proxy to use. If there’s an environment variable setting a proxy, you can set proxy to “” to override it.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

3 comment

Leave a Comment