0

I can change the bash command prompt for the Terminal for an ordinary user without issue. (In my case, I modified the $PS1 variable in /etc/bashrc.) However, that change does not modify the command prompt when one elevates privileges to root using sudo -s.

Does anyone know where the command prompt preference is stored on a macOS Sierra (10.12.5) machine for an admin user who has elevated his privileges to root using sudo -s?

I have tried modifying the following files...

  • /var/root/.bash_profile
  • /var/root/.bash_rc
  • /var/root/bash_rc
  • /var/root/.profile
  • ~/.profile

I have also tried using the command export PS1="Some prompt here# ", which changes the root prompt temporarily, but it does not stick when the session is ended and a new one is started.

  • Is this for a machine that has the root account enabled? – fd0 Jul 14 '17 at 13:04
  • No, the root account is not enabled. I am trying to get the prompt to change when I do a "sudo -s" from an admin user. – Tim Dearborn Jul 15 '17 at 3:08
1

From the sudo manual:

-s, --shell    Run the shell specified by the SHELL environment variable if it is set or the shell
               specified by the invoking user's password database entry.  If a command is specified, it
               is passed to the shell for execution via the shell's -c option.  If no command is
               specified, an interactive shell is executed.

An interactive shell would read the invoking user's .bashrc. So, Tony Williams' solution would work if it is in your .bashrc. But da4's solution should also work if it is placed in your .bash_profile.

  • Thank you. Modifying ~/.bashrc changed the root prompt when using sudo -s. – Tim Dearborn Jul 16 '17 at 3:31
2

Add the SUDO_PS1 environment variable to your ~/.bash_profile and then export the variable, such as:

export SUDO_PS1="\[\h:\w\] \u\\$ "

Note however this will only change the root prompt as it appears after you have elevated your rights via sudo.

A good list of prompt variables can be found here.

  • The above works, but only temporarily. If I quit terminal and then relaunch it, the changes are not saved. – Tim Dearborn Jul 15 '17 at 3:11
  • If you simply enter that export into an existing shell, then yes, the changes will not persist. You must add it to your ~/.bash_profile. Tested & confirmed on 10.11.6. – da4 Jul 15 '17 at 17:29
1

I am assuming that you are not actually logging in to your Mac as root since enabling root login is a really bad thing.

So you are using the command sudo -s to open a root shell from your own login. Under those circumstances the profile being run is your own. The best way to make any changes, such as to the prompt is to use an if in your profile, for example

# color prompt and make root red
if [ `id -u` = 0 ]
then
    PS1="\[\033[31m\]\h:\W \u\$\[\033[0m\] "
else
    PS1="\[\033[34m\]\h:\w \u\$\[\033[0m\] "
fi

In this example the top PS1 is for root and sets the prompt to red and the second PS1 is for any other user, e.g. you. You can make any other environment changes you want in the if as well.

  • Yes, you are correct. I am logging in as an admin user and elevating my privileges to root using "sudo -s". Your answer does not change the prompt when I have elevating my privileges to root. I did up-vote it though, as I really like the prompt color change. – Tim Dearborn Jul 15 '17 at 3:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .