How to set system-wide environment variables in Linux

In RedHat-based Linux, login shell executes /etc/profile script when a user logs in. This script customizes environment variables for all users system-wide. The /etc/profile script also sources all .sh scripts placed in /etc/profile.d directory. Therefore, in order to set system-wide environment variables in RedHat-based Linux, you can create a custom file with .sh extension in /etc/profile.d as follows.

$ sudo vi /etc/profile.d/proxy.sh
export http_proxy=http://my.proxy.com:8000
export https_proxy=http://my.proxy.com:8000

If the proxy requires authentication, you can specify username and password as well.

export http_proxy=http://username:password@my.proxy.com:8000
export https_proxy=http://username:password@my.proxy.com:8000

Set System-wide Environment Variables on Debian-based Systems

One caveat is that Debian-based systems do not use /etc/profile.d directory. Therefore, in order to set system-wide environment in Ubuntu or Debian, you can use /etc/environment instead.

$ sudo vi /etc/environment
http_proxy=http://my.proxy.com:8000
https_proxy=http://my.proxy.com:8000

For proxy with authentication:

http_proxy=http://username:password@my.proxy.com:8000
https_proxy=http://username:password@my.proxy.com:8000

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

2 thoughts on “How to set system-wide environment variables in Linux

    • You can include username and password in the form of:
      http_proxy=http://username:password@my.proxy.com:8000

      Please see the updated tutorial above.

Leave a comment

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