Search code examples
bashenvironment-variablessu

bash: environment variable lost after su root?


I defined some aliases in /etc/bash.bashrc. When I login using herbert, I can use these aliases. But after I su root, these aliases can't be used anymore.

This is very strange because configs in /etc/bash.bashrc should be global and can be used by all users.

Everything worked fine before but went strange these days. What I can remember is that I upgraded my Bash, from 4.2.25 to 4.3.30, but I am not sure whether this version change is the reason.

Is this due to Bash versions?


Solution

  • When you do su root (or just su), the user ID is changed to that of the superuser but the default environment isn't loaded. Instead, try su -.

    Paraphrasing the manual (man su), the option -, -l or --login provides a similar environment to what you would get if you logged in directly as that user. This means that things such as profiles will be loaded.