Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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.
share|improve this answer
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 '11 at 17:25
And -E will pass your current environment instead of initializing a new one. –  psusi Feb 1 '11 at 18:44
Actually -i seems to be the only one to work, but it looses my current directory ... –  C. Ross Feb 8 '11 at 17:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.