SSH Terminal Appearance – How to Change Terminal Appearance When SSHing to Remote Machine

sshterminal

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 🙁

Best Answer

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.

Related Question