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.


  • 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
  • 1
    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

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.

| 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.

| 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
  • 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 '11 at 14:51

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


Staging/Dev would be


This will only really work for redhat/centos

But for prod servers I have the following in


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
                if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
                        PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'

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

| improve this answer | |

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.

| 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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