Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

To create persistent environment variables, I add the script file to /etc/profile.d directory, for example:

# my script
export MY_VAR=var_value

This works properly for current user:

alex@alex-64:~$ echo $MY_VAR

Now I need the same environment variables for the root user, but /etc/profile.d script doesn't work for root:

alex@alex-64:~$ echo $MY_VAR
alex@alex-64:~$ sudo su
root@alex-64:/home/alex# echo $MY_VAR


How can I set the same variables for the root?

share|improve this question
This question about how to preserve a particular variable from the caller's environment when running sudo is very related. (But it is not a duplicate because this question is about how to customize root's own environment.) – Eliah Kagan Apr 10 '15 at 17:47
up vote 15 down vote accepted

sudo does not normally preserve local environment variables. You should use it with the -E switch to do so, i.e. sudo -E su will preserve $MYVAR for root.

Alternatively, to create persistent variables that are truly system-wide, you should set them in /etc/environment.

share|improve this answer
Does not work, /etc/environemnt variables are not set for sudo – Frederic Yesid Peña Sánchez Jan 24 '15 at 13:49

Defaults env_reset in /etc/sudoers will reset root's PATH defined by /etc/environment.

You could modify it to Defaults !env_reset to disable resetting or add:

Defaults secure_path="my/custom/path:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin
share|improve this answer
If I want to use the second solution(means add path to secure_path), what is key?! path is just value. I want to add ANDROID_NDK to environment variables for root. So for now, I just using the first one(!env_reset).Thanks. – Mr.Hyde Dec 6 '15 at 6:07

Like the process you define your own environment variable, for example by edit '~/.bashrc', you can define root's environment variable by edit '/root/.bashrc'.

share|improve this answer

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.