Setting Proxy Environment Variables on Linux and macOS

When using shell commands that make HTTP requests or tools such as cURL, it may be necessary to use a proxy. The proxy environment variables shown in this post can be set to use a proxy and authentication credentials within the context of the current shell session. I am using macOS and the bash shell here.

View Current Environment Variables

To view all environment variables and their values, use the env shell command:

bash-3.2$ env

The environment variables are printed in the format <key>=<value>, one per line.

bash-3.2$ env
TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
TERM_PROGRAM_VERSION=388.1.1
SHELL=/bin/bash
PATH=/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin
LANG=en_US.UTF-8
HOME=/Users/stefan

To view a specific environment variable, use printenv followed by the variable name, for example:

bash-3.2$ printenvenv TERM_PROGRAM
Apple_Terminal

Set Proxy Environment Variables

When using shell commands that make HTTP requests and you are behind a proxy, it is necessary to set proxy environment variables that provide the proxy’s hostname, port and if applicable, authentication credentials. For example, cURL uses the proxy environment variables when making HTTP requests. To set a HTTP proxy and its port for the current shell session (local variable), use the export shell command and http_proxy=<HOSTNAME>:<PORT>.

export http_proxy=proxy.host.com:88

If the proxy you need to use requires an active directory domain and user credentials for authentication, you need to use the format

"<PROTOCOL>://<DOMAIN>\<USERNAME>:<PASSWORD>@<HOSTNAME>:<PORT>"

Special characters in values need to be escaped. For details on bash shell escaping, see https://www.tldp.org/…/escapingsection.html.

export http_proxy=”http://ads\\stefan:Oak%[email protected]:88″

In the above example, the active directory domain is http://ads, the username is stefan and the password is [email protected]. The backslash between domain and username is escaped with \\ and the "at" character is escaped using %40. If you need to set a proxy for other network protocols such as https and ftp, set it using the variable names https_proxy and ftp_proxy.

export https_proxy=proxy.host.com:88
export ftp_proxy=proxy.host.com:88

Now, when making requests using e.g. cURL, the program will use the proxy variables when establishing a connection. In the below example, the html code of Google’s search page is fetched via HTTPS GET.

bash-3.2$ curl https://www.google.com
<!doctype html>
  <html itemscope=”” itemtype=”http://schema.org/WebPage” lang=”en”>
    <head>
    <meta content=”Search the world’s information, including webpages, images, videos and more. Google has many special features to help you find exactly what you’re looking for.” name=”description”>
    ….
    </head>
  </html>

Unset Proxy Environment Variables

Environment variables can be unset in the bash shell using the env command and the argument -u. For example, to unset ftp_proxy, run the command:

bash-3.2$ env -u ftp_proxy

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.