Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I sometimes find myself with several terminals open, some of which are ssh-ed in to other (production) machines. If I run a command like "drop all tables in the database" on one of those ssh-ed terminals, I can potentially destroy tons of customer's data. But in development of course I need to run those commands from time to time.

So, I was wondering: is there a way I can get my terminal (ideally the built-in GNOME one, but I'd be willing to switch to a different terminal program if necessary) to change its visual appearance when I'm ssh-ed in to a remote machine? Ideally I'd have my normal black background, but when I ssh to a remote machine the background would change to red. Maybe the window's borders would change red and start blinking or something too.

The details of the appearance change aren't important though: what is important is making instantly obvious that I need to be more careful when working on a terminal that is ssh-ed in somewhere. Is that possible?

P.S. I did find Contextual SSH, which is exactly what I want ... except that it's Mac-only :-(

share|improve this question

migrated from May 16 '13 at 6:20

This question came from our site for professional and enthusiast programmers.

up vote 5 down vote accepted

I would suggest setting your $PS1 to relevant information, like hostname, etc. You can check your shell of choice's man page for details.

An example of things you could do to your PS1 in bash

I myself have a test in my ~/bashrc that sets the prompt's color based on server.*

*see __prompt_command() function

Ways You could change your PS1

There are any number of ways you can customize the PS1, It sound like you want something a little more noticeable, so my examples will be a little more complicated then just adding the \H to the PS1. To use any of the following, you could add them to your ~/.bashrc (on the remote servers, if not both. I sync the same conf between all my computers)

Note: To make these more readable, the following assumes that these vars are declared.
The var could easily be replaced with the contents.
Also, these examples are bash biased, you may have to tweak for other shells.

RCol='\[\e[0m\]'    # Text Reset
Red='\[\e[0;31m\]'  # Red
Gre='\[\e[0;32m\]'  # Green
Yel='\[\e[0;33m\]'  # Yellow
Blu='\[\e[0;34m\]'  # Blue
Pur='\[\e[0;35m\]'  # Purple
Cya='\[\e[0;36m\]'  # Cyan
Whi='\[\e[0;37m\]'  # White

Root Check

One thing you might like is test the $USER, for if it is root, or maybe a 'production' only account:

if [ $UID -eq "0" ];then
    PS1="${Red}\h \W ->${RCol} "        # Set prompt for root
    PS1="\h \W -> "

This would make the prompt red if you where root.

Host check

You could also test for information about the current machine, and set colors based on that:

if [ $HOSTNAME == 'moving-computer-of-doom' ]; then
    PSCol="$Cya"                # For Main Computer
elif [ $HOSTTYPE == 'arm' ]; then
    PSCol="$Gre"                # For pi
elif [ $HOSTNAME == '' ]; then
    PSCol="$Blu"                # For MetaArray
elif [[ $MACHTYPE =~ arm-apple-darwin ]]; then
    PSCol="$Gre"                # For iOS
elif [ $MACHTYPE == 'i486-pc-linux-gnu' ]; then
    PSCol="$Whi"                # For Netbook
elif [[ "$MACHTYPE" == "x86_64--netbsd" && "$OSTYPE" == "netbsd" ]]; then
    PSCol="$Yel"                # For Main Cluster
    PS1+="\h "              # Un-designated catch-all

PS1+="${PSCol}\W ->${RCol} "

This would set the prompt cyan if on my laptop, green for my pi or iOS, etc etc.
If if wasn't listed, it would add the hostname to the prompt.
So if your production servers had something that was easy to test for (like a similar hostname, you could use that)


For the most part the above would work fine without this.
If you start adding things that you would want re-evaluated more often the login (maybe git status of a dir), you could use a PROMPT_COMMAND function to have the PS1 evaluated after each command.

The Above work fine without this.

Note: Sorry if these seem confusing, these are taken from settings I use, and modified to work without the rest of my settings.

share|improve this answer
Thanks, but even if after reading your answer (and the two links you provided) I still don't understand how I change $PS1 in response to an ssh (and then change it back when I leave the remote server). Any chance you could explain that? – machineghost May 22 '13 at 23:08
@machineghost do the example help? Should anything be explained more? – demure May 22 '13 at 23:40
That was perfect, thanks so much :-) – machineghost May 23 '13 at 17:39
Possibly a silly question, but I'm assuming the prompts above only work if you make sure to copy your .bashrc to the remote host? Is that true? I'd love for it not to be. :P – Josh Gagnon Jan 13 '14 at 20:19
@JoshGagnon Yes, a $PS1 (like other envvars), are local only. I personally use a git repo to keep my dot files in one place, and put them on every machine I need too. – demure Jan 14 '14 at 21:08

This is a great article which I used to customise my bash prompt:

Makes it easy to see when I am on logged into my server as I have set the hostname part of my prompt to be red.

The value I use as my PS1 in bash_profile is:

PS1="[\e[32;1m]\u[\e[0m][\e[34;1m]@[\e[0m][\e[31;1m]\h[\e[0m]:[\e[34;1m]\w $[\e[0m] "

This gives me username in green, followed by a blue '@' sign, then the hostname is in red, then '~$' in blue. You can probably see how easy it is to customise to help you easily see when you are logged in to different servers

Make sure you read the article properly as it explains how to escape each sequence correctly. Failing to escape the sequences can cause some unpredictable behaviour when typing in longer commands.

share|improve this answer
Thanks, that totally explains how to change the bash prompt. Unfortunately however it doesn't explain how to change the bash prompt in response to me running "ssh" (and then change it back when I'm done). – machineghost May 22 '13 at 23:09

Apparently all of the previous answers only work with Bash. I use Busybox on some devices and so needed an alternative.

According to this you can add a generic line to your log-in script which should work (and did for me on multiple varied hosts):

echo -n "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"
share|improve this answer

You must log in to answer this question.

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