14

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/

3
  • 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, 2011 at 13:41
  • What Operating System is your laptop working on?
    – SamK
    Jan 11, 2011 at 13:45
  • 2
    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 :) Jan 11, 2011 at 15:05

5 Answers 5

11

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.

3
  • So it can generate the same color for both foreground and background?
    – Chris S
    Sep 9, 2012 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. Sep 9, 2012 at 1:47
  • As this is he top answer for the top google result, its a shame the link is dead :(
    – stevens
    Jun 6, 2021 at 10:20
3

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.

3
  • 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, 2011 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, 2011 at 14:26
  • 2
    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, 2011 at 14:51
1

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

0

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.

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .