Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have 5 machines I constantly ssh into to do work. Its getting increasingly frustrating when I am issuing wrong commands on wrong boxes. Luckily I havent done anything bad yet. I wanted to know if there is any hack which I can hardcode which will display my prompt in different colors based on the machine I am ssh into? Such as blue for desktop1, purple for laptop, red for server etc? Is this possible?

Currently I am using this command export PS1="\e[0;31m[\u@\h \W]\$ \e[m " taken from here http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/ but it obviously doesnt work across ssh.

Also, if you have any other cool bash tips for helping me ease my sight will be wonderful. I got this tip which colors the man pages.

http://linuxtidbits.wordpress.com/2009/03/23/less-colors-for-man-pages/

share|improve this question
1  
The name@**server** line before each command doesn't convey what server your on? It takes some getting used to, but its the best existing way –  TheLQ Jan 11 '11 at 13:41
    
What Operating System is your laptop working on? –  SamK Jan 11 '11 at 13:45
    
name@server is particularly unhelpful when using things like the Amazon cloud, because it changes every boot and doesn't convey the purpose of the box or an easily memorable name (user@ip-x-x-x-x). So +1 for this question :) –  Rafiq Maniar Jan 11 '11 at 15:05

5 Answers 5

The method I use is to generate a color for the hostname from the hostname. There are not many colors to choose from so it will easily generate clashes, but it's useful for the small amount of machines that I manage.

hostnamecolor=$(hostname | od | tr ' ' '\n' | awk '{total = total + $1}END{print 30 + (total % 6)}')

PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\[\e[${hostnamecolor}m\]\]\h \[\e[32m\]\w\[\e[0m\]\n$ '

The first line generates a number between 30 (inc) and 36 (exc) from the hostname of the machine. The second line applies it to the prompt with username and path in green (32) and the host name in the generated color.

No background colors are set, and I exclude cyan (36) and white (37) from the foreground to avoid clashes with the backgrounds of terminals that I use.

share|improve this answer
    
So it can generate the same color for both foreground and background? –  Chris S Sep 9 '12 at 0:49
    
No background color is generated. But you are correct that the generated color can match the background. I exclude white (37) and cyan (36) because they don't work well with the terminal backgrounds I use. I've edited the answer to make that clear. –  Dan Midwood Sep 9 '12 at 1:47

You are on the right track. I've used the method detailed on the page you link to, and detailed exactly how on my blog. It works for me using PuTTY from a windows box, and also SSH from one CentOS server to another. If it isn't working for you, and you are sure you got the syntax correct, it could be down to your SSH client.

share|improve this answer
    
Hi, I did change the .bashrc and I understand that I can change colors locally. What I wanted was that when I ssh to a different computer, the prompt color changes. I know that the terminal will not know automatically so I was looking for pointers to make this happen. –  bcrawl Jan 11 '11 at 14:03
1  
Edit: ok, yes you are right. If the .bashrc file is changed on each computer, when you ssh, it will display its configured colors. I was not editing the right file. Thanks. Consider it solved. –  bcrawl Jan 11 '11 at 14:26
    
If this answer led you directly to the solution, then consider marking this as an Accepted Answer by clicking on the outline of a checkmark to the left of the answer. –  Kevin M Jan 11 '11 at 14:51

You can alter the bash.rc on each machine to give'em different prompt colors. Or if you have enough privilegies alter sshd config to permit user environment transfer via the PermitUserEnvironment directive.

share|improve this answer

At my job we have different stages of servers

production staging development

I use a red prompt as prod and green for staging. Now all staging machines have stg in the domain.

So prod would be

web1.domain.com
web2.domain.com

Staging/Dev would be

stg-web1.domain.com
stg-web2.domain.com
dev-web1.domain.com
etc

This will only really work for redhat/centos

But for prod servers I have the following in

/etc/sysconfig/bash-prompt-xterm

Inside is

export PS1="\[[\e[1;31m\]\u@\[\e[1;31m\]\h\[\e[0m\] \W] "

That will turn the prompt red.. Now you can not touch /etc/bashrc and all other non-prod machines will stay the normal foreground color you use.. but I made them green

if [ "$PS1" ]; then
    case $TERM in
        xterm*)
                if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
                        PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
                else
                        PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
                fi
                ;;

That if block should be there.. I just changed the color on the else part to be green.. 33 is green

share|improve this answer

We are working on a more comprehensive hostname string checksum based solution in this gist incl. 256 color compatibility. Aside from the case where you're actually interested in determining a specific color for each of your servers yourself, I feel this is one of those "it's what we have computers for" problems, so getting "random" colors determined for you based on checksums should be a good approach.

share|improve this answer

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.