How can I make sure an environment variable (GRAILS_HOME for example) is set when I sudo?

I put a script in my /etc/profile.d with this value and made it ugo+x. What do I need to do to make this visible to the super user?

2 Answers 2


Try with

sudo su -

The - loads up all the environment files. From man su:

-, -l, --login
   Provide an environment similar to what the user would expect had
   the user logged in directly.

   When - is used, it must be specified as the last su option. The
   other forms (-l and --login) do not have this restriction.

Update: In the general case you run sudo -i mycomment, as the man page of sudo says,

-i [command]
           The -i (simulate initial login) option runs the shell
           specified in the passwd(5) entry of the target user as a
           login shell.  This means that login-specific resource files
           such as .profile or .login will be read by the shell.  If a
           command is specified, it is passed to the shell for
           execution.  Otherwise, an interactive shell is executed.
           sudo attempts to change to that user's home directory
           before running the shell.  It also initializes the
           environment, leaving DISPLAY and TERM unchanged, setting
           HOME, SHELL, USER, LOGNAME, and PATH, as well as the
           contents of /etc/environment on Linux and AIX systems.  All
           other environment variables are removed.
  • This works, but I don't like mucking about as root any more than necessary. I'd prefer something that would work without doing a full su.
    – C. Ross
    Feb 1, 2011 at 17:25
  • 1
    And -E will pass your current environment instead of initializing a new one.
    – psusi
    Feb 1, 2011 at 18:44
  • Actually -i seems to be the only one to work, but it looses my current directory ...
    – C. Ross
    Feb 8, 2011 at 17:24

Per this answer: Setting the PATH so it applies to all users, including root/sudo if you note Sudo resets all variables and path by default.

Pertinent Bit:

The manual page for sudoers states:

env_reset       If set, sudo will reset the environment to only contain
                   the LOGNAME, MAIL, SHELL, USER, USERNAME and the SUDO_*
                   variables.  Any variables in the caller's environment
                   that match the env_keep and env_check lists are then
                   added.  The default contents of the env_keep and
                   env_check lists are displayed when sudo is run by root
                   with the -V option.  If the secure_path option is set,
                   its value will be used for the PATH environment
                   variable.  This flag is on by default.

So, you can do the following to maintain variables when using sudo

sudo visudo

this will open sudo settings for you. Then per what I did you add the following below

Defaults secure_path="blah"


(EXCLUDING PATH as it is set by secure_path) and those are just single spaces between each variable if you wish to have more than 1 maintained.

and what this does is tell sudo which env variables to keep and not disregard.

When done hold ctrl and hit o for Write Out hit enter and say yes to save [even though it specifies a tmp file this is OK it will be written back to the main config just say yes when asked if you wish to overwrite].

That should allow you to maintain whichever variables you wish (a big one being JAVA_HOME and also http_proxy if you use a proxy).

So it should look something like below including your specified variable:

Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin/bin"
Defaults        env_keep +="GRAILS_HOME"

and to verify it takes exit any open terminal windows and re-open one and run


It should be what you set, now issue

sudo echo $GRAILS_HOME

and it should now remain unchanged

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .