sourcing bash profile for root users

Apologies in advance if this question is going to annoy the hell out of everyone as I appreciate, it's been asked in various ways many times before. Please be assured, I have read the archives and have tried at least some of the suggestions but still cannot resolve the (simple) issue. Hopefully someone can provide an answer and I can feel appropriately humbled. FYI, for the avoidance of any ambiguity, I'm going to use absolute rather than relative paths.

When I log in, I do so as user adam. The aliases in /Users/adam/.bash_profile are loaded and I can use them immediately upon pulling up a terminal. I don't use a .bashrc file and that's fine.

Every now and then, I switch to root by typing su into the terminal and then entering my password. The prompt changes and I am now root user. My question is this: immediately upon becoming root, can my root bash profile be loaded without me having to manually source the profile file? The reason I know it doesn't happen automatically is because I have the same aliases in my root profile file and my /Users/adam/.bash_profile. After becoming root user, unless I type in source [root_profile_file], they don't work. I have tried setting up the following permutations and then switching to root via the terminal but none of them automatically source the profile/aliases (note, I don't have any of the options below set up concurrently so I don't think I'm confusing the system):

Option 1: put my aliases in /etc/profile

Option 2:

  • in /etc/profile, insert [ -r /etc/bashrc ] && . /etc/bashrc
  • put my aliases in /etc/bashrc

Option 3:

  • in /etc/bash_profile, insert [ -r /etc/bashrc ] && . /etc/bashrc
  • put my aliases in /etc/bashrc

Option 4: put my aliases in /var/root/.profile

Option 5:

  • in /var/root/.profile, insert [ -r /var/root/.bashrc ] && . /var/root/.bashrc
  • put my aliases in /var/root/.bashrc

Option 6:

  • in /var/root/.bash_profile, insert [ -r /var/root/.bashrc ] && . /var/root/.bashrc
  • put my aliases in /var/root/.bashrc

Please note, with any of the above, if I switch to root and then type source root_profile_file the aliases are loaded but only if I do indeed source the file manually. Perhaps I've totally misunderstood how bash works and it's not possible to source a profile file automatically after switching to root but I'm hoping there is a simple solution. Thanks in advance to anyone who's taken the time to read this message.


Solution 1:

The problem you are encountering is that when you run su by itself, you are not entering a 'login' shell. This means that your environment, working directory, and everything except uid/gid remain the same of the original user.

Login triggers do not execute, and you experience the issues you describe.

A simple solution to a simple problem:

su -

From the su(1) man page:

The su command is used to become another user during a login session. Invoked without a username, su defaults to becoming the superuser. The optional argument - may be used to provide an environment similar to what the user would expect had the user logged in directly.

Also:

   -, -l, --login
       Provide an environment similar to what the user would expect had the user logged in directly.

If you su into a login shell, bash will behave as you expect, and automatically source the appropriate files on "login", without the need for overly hacky workarounds.

Solution 2:

Actually root uses /bin/sh (old bourne shell), .bash_profile and .bashrc are read by bash.

It becomes tricky as those files may use functionality not available in sh. Even if you source .bashrc or source .bash_profile, you will still have some issue with complex functions for instance.

One way to solve this is to run

sudo dscl . -change /Users/root UserShell /bin/sh /bin/bash

to switch root's shell to bash.