Different color prompts for different machines when using terminal/ssh?

Solution 1:

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.

Solution 2:

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.

Solution 3:

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