How to use curl command with proxy username/password on Linux/ 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 command, wget command, lynx command, and others; 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 with or without a userame/password. This page shows how to perform HTTP/HTTPS requests with cURL cli using PROXY server.

Unix and Linux curl command with proxy syntax

The syntax is:

## Set the proxy address of your uni/company/vpn network ## 
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

Linux use curl command with proxy

First set the http_proxy:

## proxy server,, port: 3128, user: foo, password: bar ##
export http_proxy=http://foo:bar@
export https_proxy=$http_proxy
## Use the 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)

How to use the specified proxy server with curl on Unix

$ curl -x http://prox_server_vpn:3128/ -I

How to use socks protocol?

The syntax is same:

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

How do I configure and setup curl to permanently use a proxy connection?

Update/edit your ~/.curlrc file using a text editor such as vim:
$ vi ~/.curlrc
Append the following:

proxy =
proxy-user = "foo:bar"

Save and close the file. Another option is create a bash shell alias in your ~/.bashrc file:

## alias for curl command 
## set proxy-server and port, the syntax is
## alias curl="curl -x {your_proxy_host}:{proxy_port}"
alias curl="curl -x"

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. See curl command man page here for more info.

Posted by: SXI ADMIN

The author is the creator of SXI LLC 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.