Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is it possible to change the profile of a terminal upon switching user in the same terminal?

Actually my goal is to change the terminal to a profile which indicates to me that it is a terminal as 'root' and that I better be very careful. Of course I am always very careful with root, and to improve that even more, I want to change the profile of the terminal so that it clearly shows to me that I'm currently using a terminal that has root-priviliges.

So for clarity: I don't want to make a shortcut to open a bash terminal with a certain profile, I am thinking of some edits to .bashrc which detects a user change and can switch to a profile.

Is this possible, and if yes, how? ;)

share|improve this question
add comment

4 Answers

You just need to edit .bashrc file for root user and add there whatever you need to have that different profile.

For example, it's usual to set PS1 in .bashrc to provide a different prompt (one that ends with # instead of with $) to let the user know he's in a root terminal. In fact, I think this should be the defaultt behaviour so you should have this different prompt already.

share|improve this answer
add comment

If you are using su you can add the -, -l or --login option to the command to "Provide an environment similar to what the user would expect had the user logged in directly." All according to the su man-page. These options will load the new users .bashrc or whatever file(s) apply to the shell being loaded.

For example su - will switch to the user root with all the same settings as if logged in directly as root from a terminal.

For more information see man su.

share|improve this answer
add comment

I am lazy, I use the same .bashrc for root and my user. The main reason is I like to set a number of aliases (cp -I , etc).

Add this to set your prompt:

if [[ $EUID == 0 ]] ; then
    PS1='\[\033[01;31m\]\u\[\033[01;30m\]@\[\033[01;34m\]\h\[\033[01;30m\]:\[\033[01;34m\]\w\[\033[00m\]\$' #RED
else
    PS1='\[\033[01;32m\]\u\[\033[01;30m\]@\[\033[01;34m\]\h\[\033[01;30m\]:\[\033[01;34m\]\w\[\033[00m\]\$' #Green

   # Comment out the above and uncomment the below for a blue user name.
   # PS1='\[\033[01;34m\]\u\[\033[01;30m\]@\[\033[01;34m\]\h\[\033[01;30m\]:\[\033[01;34m\]\w\[\033[00m\]\$' #Blue
fi

The if [[ $EUID == 0 ]] sets your user name to red if running as root.

See : http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html for additional tips / options.

share|improve this answer
add comment

add following lines to .bashrc

if [ $(id -u) -eq 0 ];
then # you are root, set red colour prompt
  PS1="\\w# "
else # normal
  PS1="\\w$ "
fi

you can also customize PS1 look at this

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.