Setting Proxy in Linux Via the Bash Shell

Setting Proxy in Linux Via the Bash Shell

·

4 min read

Working in a corporate environment and 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.

This list is not exhaustive, it is just what I have used previously. Leave a comment if you've done something different that has worked for your use case.

TL:DR

  • /etc/profile (system-wide), ~/.bash_profile, ~/.bash_login or ~/.profile [interactive shells only, login]

  • ~/.bashrc (user only) [interactive, non-login]

  • /etc/environment (system-wide), or even better a new script in /etc/profile.d

Interactive (Login vs Non-Login) vs Non-Interactive

First, let's talk about Interactive vs Non-Interactive shells:

  • Interactive: an interactive shell is a shell that reads and writes to the user's terminal. This can be login or non-login.

    • A login shell is when a user logs in to the terminal locally, or remotely via SSH, or if the shell is launched with the --login flag.

    • A non-login shell is typically forked from an interactive shell, typically by typing bash in the prompt.

  • Non-interactive: a non-interactive shell is not associated with a user's terminal, for example when executing a script (eg cron) or executing a command remotely.

    ssh remote_host 'env'

Interactive login shell startup (assuming the files exist and are readable)

/etc/profile which is system-wide, then (local to the user's session):

|-> ~/.bash_profile

|-> ~/.bash_login

|-> ~/.profile

These are run once the user logs in.

Interactive non-login shell startup

~/.bashrc

Any commands located in ~/.bashrc is run every time a new shell is opened.

/etc/environment is also system-wide, read by pam_env.so during login.

It generally isn't recommended to make modifiecations to /etc/environment (for example it doesn't support expanding existing variables, so something like setting the PATH will not work), instead, create a file in /etc/profile.d which will be executed as regular shell scripts by all shells during the initialisation.

Non-Interactive shells

The fail-safe way is either via BASH_ENV, or by exporting the variables in a wrapper and running source <wrapper>.sh before using the variables.

Notes:

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

  • -E

  • –preserve-env

For example:

sudo -E yum update -q

Onto setting the proxy. I have only needed proxy settings for interactive sessions and generally do it one of two ways, depending on user scope.

Interactive Shells Only

/etc/profile

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

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

Then, logout and log back in to pick up the changes:

logout

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

~/.bashrc

Add the following to ~/.bashrc for the user for which you wish the proxy settings applied:

export http_proxy=http://myproxy.com:80
export https_proxy=$http_proxy
export ftp_proxy=$http_proxy
export no_proxy=.test.subdomain,.localhost,.internal

Then source the file to load the changes into the current session:

source ~/.bashrc

Non-Interactive Shells

As I mentioned above, this isn't a scenario that I have needed to have proxy settings applied, but just in case you need it the following should work (example is for cron).

Create a new file in /etc/profile.d named <company>_set_proxy.sh and add the proxy values to it. In the script that is run by the cron job (or any other script), source that file to ensure the proxy values

vi /etc/profile.d/<company>_set_proxy.sh

# Press insert key
export http_proxy=http://myproxy.com:80
export https_proxy=$http_proxy
export ftp_proxy=$http_proxy
export no_proxy=.test.subdomain,.localhost,.internal

# Esc
# :wq to save
:wq

Then the script called by crontab:

source /etc/profile.d/<company>_set_proxy.sh
# Rest of script

Well that's about it. As I said, these are a few approaches I have used previously.

If you have done something different that worked for you, let me know in the comments. Have a great day.