Setting Proxy Values in Linux via the Shell

Need to set a proxy so your server can communicate with the Internet? If so, there are a few ways to accomplish this, depending on your use case.

TL:DR

  • /etc/profile [interactive shells only]
  • ~/.bashrc [both interactive and non-interactive shells]
  • /etc/environment

Notes:

When using sudo you may see different behaviour. To have sudo pick up any environment variables pass in either the of the following flags:

  • -E
  • –preserve-env

For example:

sudo -E yum update -q 

In the examples below we optionally include the [sudo] command, of course this will depend on what user you are running with. We recommend not running commands as “root”.

Interactive Shells Only

Add the following to /etc/profile if you want to only have the proxy values picked up when using an interactive shell (eg Putty or Terminal), this is read as part of the login process.

Example:

[sudo] vi /etc/profile
export http_proxy=http://myproxy.com:80
export https_proxy=$http_proxy
export ftp_proxy=$http_proxy
export no_proxy=.test.subdomain,.localhost,.int.tld
:wq
logout

Note, you may not need all of the proxy values above, you may only need to set 1 or 2 depending on your configuration. If in doubt, check with your system administrator, hosting provider etc.

Both Interactive and Non-Interactive Shells

If you need to use a proxy in both interactive and non-interactive shells (eg a cron job) then you can set the proxy values via ~./bashrc

Example:

[sudo] vi ~/.bashrc
export http_proxy=http://myproxy.com:80
export https_proxy=$http_proxy
export ftp_proxy=$http_proxy
export no_proxy=.test.subdomain,.localhost,.internal
:wq
source ~/.bashrc

You can also set the proxy via the /etc/environment script, which is will initialise any system variables defined within it upon booting.

Viewing the Proxy Values

To verify the values have been set, run the following:

env | grep -i proxy

Of course, this is only a very short guide on setting environment variables using the bash shell.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x

Join our newsletter.

Get notified first when we publish new content, plus we’ll be sending some content out that we don’t publish here.