How can I change my (Linux) terminal's appearance when I SSH in to a remote machine?

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 :-(


Solution 1:

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 ~/.subbash/prompt 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
else
    PS1="\h \W -> "
fi

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:

PS1=
PSCol=
if [ $HOSTNAME == 'moving-computer-of-doom' ]; then
    PSCol="$Cya"                # For Main Computer
elif [ $HOSTTYPE == 'arm' ]; then
    PSCol="$Gre"                # For pi
elif [ $HOSTNAME == 'ma.sdf.org' ]; 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
else
    PS1+="\h "              # Un-designated catch-all
fi

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)

PROMPT_COMMAND

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.

Solution 2:

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

http://www.ibm.com/developerworks/linux/library/l-tip-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.

Solution 3:

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"