≡ Menu

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

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 www.cyberciti.biz
curl -v -I www.cyberciti.biz

Sample outputs:

* Rebuilt URL to: www.cyberciti.biz/
*   Trying
* Connected to 1202.54.1.1 ( port 3128 (#0)
* Proxy auth using Basic with user 'foo'
> HEAD HTTP://www.cyberciti.biz/ HTTP/1.1
> Host: www.cyberciti.biz
> 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: <http://www.cyberciti.biz/wp-json/>; rel="https://api.w.org/"
Link: <http://www.cyberciti.biz/wp-json/>; rel="https://api.w.org/"
< 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:myPasswordHere@"
$ curl -v -O http://dl.cyberciti.biz/pdfdownloads/b8bf71be9da19d3feeee27a0a6960cb3/569b7f08/cms/631.pdf

OR use the -x option:
curl -x 'http://vivek:myPasswordHere@' -v -O http://dl.cyberciti.biz/pdfdownloads/b8bf71be9da19d3feeee27a0a6960cb3/569b7f08/cms/631.pdf
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 http://www.cyberciti.biz/

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.

Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 3 comments… add one }
  • manoj August 8, 2016, 9:22 am

    I want to learn linux + networking for example iptables etc.
    What is the best place to start?

  • Juan Pablo K October 14, 2016, 2:43 pm

    I need a help to curl comand to download a large files from google drive, nothing works for me, sorry about my poor english.

  • Ben December 1, 2016, 3:52 pm

    thx for the post. this is exactly what i needed.

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , ,