bash: environment variable lost after su root?

1.7k Views Asked by At

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?

2

There are 2 best solutions below

10
On BEST ANSWER

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.

0
On

If you built bash 4.3.30 yourself, you probably forgot to configure it the same way your OS configured it. Many Linux distributions modify bash to source a file like /etc/bash.bashrc in addition to ~/.bashrc for interactive shells. Vanilla bash only sources ~/.bashrc.

The configuration is done by modifying config-top.h if I remember correctly.

Also see Dot Files.