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

5,323

Solution 1

  • 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.

Solution 2

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

Share:
5,323
Rishab Jain
Author by

Rishab Jain

Updated on September 18, 2022

Comments

  • Rishab Jain
    Rishab Jain over 1 year

    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?

  • Rishab Jain
    Rishab Jain about 13 years
    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.
  • user1686
    user1686 about 13 years
    @Philip: If an answer is helpful, consider marking it as accepted.