Environment Variables

From Dikapedia
Jump to: navigation, search

How to set environment variables in Linux? (proxy examples)



Check current proxy configuration status (https_proxy/https_proxy)/ This variable will show if there is a proxy server configured on the system:

# echo $http_proxy
# echo $https_proxy

Set up proxy without username and password

# export http_proxy=http://SERVER:PORT/
# export https_proxy=http://SERVER:PORT/
# export no_proxy=localhost,127.0.0.1,169.254.169.254,.amazonaws.com

Set up proxy with username and password

# export http_proxy=http://USERNAME:PASSWORD@SERVER:PORT/

Set up proxy permanently using /etc/environment. Persistent across all reboots and users:

# echo "https_proxy=https://proxy.example.com:3111/" >> /etc/environment
# echo "http_proxy=http://proxy.example.com:3222/" >> /etc/environment


/etc/environment


  • This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.
  • This file gets loaded upon boots and make the environment variables persistent.
  • /etc/environment is used by the pam_env module and does not work as a shell script like /etc/profile so scripting or glob expansion cannot be used. The file only accepts variable=value pairs. See pam_env(8) and pam_env.conf(5) for details.
  • /etc/environment is not a shell script unlike /etc/profile, it applies to all processes without shell.
    • In other words, you need "export" in /etc/profile, but NOT in /etc/environment.

Example:

$ cat /etc/environment
https_proxy=https://proxy.example.com:3111/

$ sudo reboot

$ echo $https_proxy
https://proxy.example.com:3111/

$ env |grep proxy
https_proxy=https://proxy.example.com:3111/


/etc/profile



From: https://bencane.com/2013/09/16/understanding-a-little-more-about-etcprofile-and-etcbashrc/ by Benjamin Cane

What is /etc/profile used for?

If you have been using Linux for a while you are probably familiar with the .profile or .bash_profile files in your home directory. These files are used to set environmental items for a users shell. Items such as umask, and variables such as PS1 or PATH.

The /etc/profile file is not very different however it is used to set system wide environmental variables on users shells. The variables are sometimes the same ones that are in the .bash_profile, however this file is used to set an initial PATH or PS1 for all shell users of the system. /etc/profile.d

In addition to the setting environmental items the /etc/profile will execute the scripts within /etc/profile.d/*.sh. If you plan on setting your own system wide environmental variables it is recommended to place your configuration in a shell script within /etc/profile.d. What is /etc/bashrc used for?

Like .bash_profile you will also commonly see a .bashrc file in your home directory. This file is meant for setting command aliases and functions used by bash shell users.

Just like the /etc/profile is the system wide version of .bash_profile. The /etc/bashrc for Red Hat and /etc/bash.bashrc in Ubuntu is the system wide version of .bashrc.

Interestingly enough in the Red Hat implementation the /etc/bashrc also executes the shell scripts within /etc/profile.d but only if the users shell is a Interactive Shell (aka Login Shell) When are these files used?

The difference between when these two files are executed are dependent on the type of login being performed. In Linux you can have two types of login shells, Interactive Shells and Non-Interactive Shells. An Interactive shell is used where a user can interact with the shell, i.e. your typical bash prompt. Whereas a non-Interactive shell is used when a user cannot interact with the shell, i.e. a bash scripts execution.

The difference is simple, the /etc/profile is executed only for interactive shells and the /etc/bashrc is executed for both interactive and non-interactive shells. In fact in Ubuntu the /etc/profile calls the /etc/bashrc directly.

Other notes: /etc/profile.d is used so you can break out some of the setting that go in profile.

For instance instead of putting settings for vi in /etc/profile, I can put them in /etc/profile.d/vi

Per the comment at the top of /etc/profile:
System wide environment and startup programs, for login setup Functions and aliases go in /etc/bashrc It's NOT a good idea to change this file unless you know what you are doing. It's much better to create a custom.sh shell script in /etc/profile.d/ to make custom changes to your environment, as this will prevent the need for merging in future updates.


pam_env


pam_env - PAM module to set/unset environment variables

  • Sometimes variables set in /etc/environment are not available after $ su - because pam_env bypassed for root user when using su. As a work around, append the below entry to /etc/pam.d/su:
session required pam_env.so
  • You can force PAM configuration files (like /etc/pam.d/sudo, /etc/pam.d/login, /etc/pam.d/su) to use pam_env module by inserting the line above.


Ref:
https://access.redhat.com/solutions/4093921 https://bugzilla.redhat.com/show_bug.cgi?id=1702712