How to specify root's environment variable?

Solution 1:

I had a similar problem. I looked in my /etc/sudoers file and I saw these lines:

Defaults    env_reset
Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \
                        LS_COLORS MAIL PS1 PS2 QTDIR USERNAME \
                        LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION \
                        LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC \
                        LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS \
                        _XKB_CHARSET XAUTHORITY"

To get my environment variable to be there I had to add its name after "XAUTHORITY". In your case you would have:

Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \
                        LS_COLORS MAIL PS1 PS2 QTDIR USERNAME \
                        LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION \
                        LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC \
                        LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS \
                        _XKB_CHARSET XAUTHORITY LD_LIBRARY_PATH"

Give that a try.

Also make sure that you set BASH_ENV="~/.bashrc" in /etc/environment.

See this other answer for more details

Solution 2:

Crazy idea, but are you sure that the server actually runs as root? Some servers specify an account that they run under, so even though you invoke the start script as root they are actually running as another user.

Solution 3:

The way you've reached your root user matters here. Different methods for "logging in" create different environments.

For example:

  • If bash is your root shell, it will read .bashrc upon startup
  • If bash is your root shell, and it is a "login" shell, it will read .bash_profile upon startup
  • If you are logging in via ssh, it will use .ssh/environment as well
  • If you are logging in via login, that is from the console, /etc/pam.d/login will use pam_env.so to read /etc/security/pam_env.conf (depending on the distro) and /etc/environment (if there is readenv=1) on the command line
  • etc.

So, if you are not getting the environment you want - you need to figure out the chain of logins that is leading you to the software you are starting. This might be something like: gdm (X) login -> terminal emulator -> bash shell -> su -> bash shell (root) -> software.

However, if you just want to take the best guess, /etc/environment should be read for every process in a recent distributions.