Changing behavior of bash prompt when functioning as root
It will also depend on how you become the root user. You need to make the change in the root user's .bashrc if you are using something like su - root
or sudo -i
, where you read in the environment.
With sudo -s
, you should be reading your own .bashrc.
Consider adding some printf or echo statements to debug your code, to tell you when it has executed.
Use the id
command to make sure you are who you think you are:
root@tau:~# id
uid=0(root) gid=0(root) groups=0(root)
To change the colour of the prompt to red for root, I modified the .bashrc
file, following the examples in this thread.
regularuser@myubuntubox:~$ sudo su -
root@myubuntubox:~# vim .bashrc
Then, in vim, search for the final appearance of PS1
assignment, and add this following it (note that it updates the value of $PS1
so that other previous lines do not need to be modified, and the change is easily revertible):
# Set RED prompt
PS1="\[\e[01;31m\]$PS1\[\e[00m\]"
Following belacqua's valid answer https://askubuntu.com/a/305053/12218, by issuing sudo su the root's .bashrc is loaded.
You can check this by issuing sudo su
followed by cd ~
you will see that you are at root's home directory.
Using sudo su
you log in as root (su = switch user) in contrary with
sudo do some administrative task
as user with elevated privileges.
So by using sudo su
you become the actual root and the changes made to environment are changes made specifically to root, i.e: sudo su
followed by vim .bashrc
.