10

Every time I try to login as root using su (not su -), it doesn't source .bash_profile in user1's home directory.

Basically, my /var/root directory doesn't have .bash_profile, so I put a copy of .bash_profile in /var/root to test su -.

It doesn't automatically source .bash_profile (in var/root), either.

Anyway, I want to make .bash_profile, of user1, sourced in root account automatically when I use su.

What should I do?

(It worked before! One day, it just doesn't source! Maybe something changed settings in bash? It works when I enter source .bash_profile after login.)

I am using Mac and OS X Yosemite.

11

The default shell for root on OS X is /bin/sh. Its sh is also a version of bash, but when invoked with the name sh Bash:

tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.

When invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. ... a shell invoked as sh does not attempt to read and execute commands from any other startup files

That is, it doesn't read .bash_profile at all, regardless of whether it was invoked as a login shell or not. You can use .profile instead, or even symlink one to the other. If you launch a login shell with su -l, .profile is loaded on startup, but .bash_profile will never be.


You can also use dscl to change root's shell (noting that /etc/passwd is not used to determine the shell on OS X). You can check root's current shell with dscl . -read /Users/root UserShell; consult the documentation and think carefully before changing it to something else.

Another approach is simply to change your su invocation to force executing bash immediately.

Given what you've said, I'd probably recommend the symlink, but you may wish to look into the changes that Bash's POSIX mode makes and decide whether you want to have them or not.

5

Because it is not considered as a "login shell" (which is invoked directly from login, or sshd) but simple "interactive shell". See here for example: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

So force su using login option i.e. with -l option:

su -l

Or put your environment into .bashrc file.

5

The root user will try to execute the .bashrc file instead of the .bash_profile since you are not invoking a login shell.

From the bash manual man bash:

   ~/.bash_profile
          The personal initialization file, executed for login shells
   ~/.bashrc
          The individual per-interactive-shell startup file

Note the ~ where the .bashrc file needs to be in the homedirectory of the root user.

To actually answer your question, if you want the .bashrc file of user1 to be used I would suggest you make a (symbolic) link between the source .bashrc from user1 to the root user.

You can also consider to source the user1 .bashrc or even .bash_profile by adding the line . /homedirectory/of/user1/.bash_profile to the .bashrc file of the root user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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