How do I go into root and have it change my prompt?

3

1

I am using ubuntu-10.04 and bash. I want to go into root and have it change my prompt to underlined red characters to reflect that I am in root. At the bash prompt, if I type:

$ sudo bash

then I go into root, however my prompt doesn't change. In /root/.bashrc I have:

RED="\[\033[0;31m\]"
UNDERLINE="\[\033[4m\]"
DEFAULT="\[\033[0m\]"
export PS1=$RED$UNDERLINE'\u@\h:\w\$'$DEFAULT' '

however this file isn't being read. In order for it to be read I have to enter at the prompt:

# . /root/.bashrc

which I don't want to have to do. How do I set it up so that when I go into root, /root/.bashrc runs automatically?

Philip Hunt

Posted 2011-04-10T09:06:47.643

Reputation: 31

Answers

2

  • First, is sudo root even a valid command? (sudo -s or sudo -i would be.)

  • I prefer editing my own .bashrc instead of root's. You can have, for example,

    if (( $UID == 0 )); then
        PS1="$RED$UNDERLINE"
    else
        PS1="$GREEN"
    fi
    PS1="$PS1\u@\h:\w\$$DEFAULT "
    
  • You can use sudo -i to make root's rcfiles be read instead of yours.

user1686

Posted 2011-04-10T09:06:47.643

Reputation: 283 655

Sorry, I meant to say sudo bash. You're right, sudo root is meaningless. Anyway, your suggestion of an if statement on the $UID works. – Philip Hunt – 2011-04-10T11:58:53.030

@Philip: If an answer is helpful, consider marking it as accepted.

– user1686 – 2011-04-10T12:07:06.583

1

Seems like sudo is configured to use the original users's environment variables and not the new ones.

Either you set the env_reset and env_keep options in /etc/sudoers or you explicitly reset the environment variables to the ones of the target user with -H: sudo -H -u root

user16115

Posted 2011-04-10T09:06:47.643

Reputation: