19

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.

Mathias Bynens
  • 11,642
  • 13
  • 66
  • 111
Adam Gold
  • 191

7 Answers7

15

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.

Jason Salaz
  • 24,471
  • 1
    I will however second Hai Vu's notes: You really, really, really should never su nor work as root.

    There is sudo for that purpose, or you can work in such a way that you don't require root privileges at all.

    – Jason Salaz Mar 14 '12 at 18:33
  • Thank you to both you and @Hai Vu. "su -" does the trick and yes, completely understood, root should generally be avoided. – Adam Gold Mar 14 '12 at 19:39
  • 1
    I already knew to add the '-' after the su command, but still was not getting my .bash_profile for the root user sourced. Florian Bidabe's and patrix's answer below about changing the root shell from sh to bash fixed the problem for me. – JaredC Sep 25 '14 at 11:19
9

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.

nohillside
  • 100,768
6

When I absolutely must, I sudo bash which makes me root but with JRobert's environment. My .bashrc contains:

# Prompt: 'jrobert@JRiMac ~' in green (red, if I'm root), '$' in white
if [[ $UID == 0 ]]; then
   export PS1="\[\e[1;31;40m\]\u@\h \W\[\e[0m\]\$ "
else
   export PS1="\[\e[32;40m\]\u@\h \W\[\e[0m\]\$ "
fi

, to make the root-prompt glare back at me in RED to emphasize that I'm now (more) vulnerable to my own [pick one: brashness, dumbness, fat-finger tendencies, unwillingness to take good advice, up s.creek - don't drop the paddle].

JRobert
  • 3,144
3

I am using sudo -i to switch to to root user. In this case, shell configuration is read from /var/root/.profile.

nohillside
  • 100,768
Matic
  • 31
1

In general, I advice against su as it is dangerous, just search for it and you know why. On to your question, the root account works the same way your account does: it source ~/.bash_profile and/or ~/.bashrc. I don't know which one, but my intuition leans toward ~/.bashrc, so you might want to try to put your aliases there. Here is a suggestion:

su           # type password to get into root account
vi ~/.bashrc # put your aliases there
exit         # exit your root session
su           # try again to see if your aliases works
Hai Vu
  • 531
-1

One way to solve this is to run the following to switch root's shell to bash:

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

This solution has fixed my issue as well.

Graham Miln
  • 43,776
Yang Wang
  • 101
-2

if you have only one 'default' user and the root user you can put alias su="su -" on .bash_profile of the 'default' user and you get the effect you want. Works and with no hacky things.

grg
  • 201,078