Is there a way I can have my terminal dynamically change covers based on the host I am on? For example, the default background (as determined by terminal profile) is black. Maybe there is another terminal profile that is the exact same except that it has a blue background. Now, if I SSH from black_host which is the default into blue_host, my terminal profile switches (or the background changes from black to blue).

Is this possible? I often have anywhere from 5-10 terminals open at a time on 3-4 different machines and it would be nice to just look and see from a birds eye view which terminal belongs to which host.

2 Answers 2


I believe this is exactly what you want:


All credit goes to Yurii Soldak:

Put following script in ~/bin/ssh (ensure ~/bin/ looked before /usr/bin/ in your PATH):


HOSTNAME=`echo $@ | sed s/.*@//`

set_bg () {
  osascript -e "tell application \"Terminal\" to set background color of window 1 to $1"

on_exit () {
  set_bg "{0, 0, 0, 50000}"
trap on_exit EXIT

case $HOSTNAME in
  production1|production2|production3) set_bg "{45000, 0, 0, 50000}" ;;
  *) set_bg "{0, 45000, 0, 50000}" ;;

/usr/bin/ssh "$@"

The script above extracts host name from line "username@host" (it assumes you login to remote hosts with "ssh user@host").

Then depending on host name it either sets red background (for production servers) or green background (for all other). As a result all your ssh windows will be with colored background.

I assume here your default background is black, so script reverts the background color back to black when you logout from remote server (see "trap on_exit").

Please, note however this script does not track chain of ssh logins from one host to another. As a result the background will be green in case you login to testing server first, then login to production from it.

In the same post, Chris Page writes:

A lesser-known feature of Terminal is that you can set the name of a settings profile to a command name and it will select that profile when you create a new terminal via either Shell > New Command… or Shell > New Remote Connection….

For example, duplicate your default profile, name it “ssh” and set its background color to red. Then use New Command… to run ssh host.example.com.

It also matches on arguments, so you can have it choose different settings for different remote hosts, for example.


Set the terminal colours on each host.

My situation is slightly different than yours, in that, I'm not connecting to multiple remote servers concurrently, but the principles are the same.

In my case I wanted a simple way to change the colours of the terminal when I was connected to a remote server so that I accidentally did not run a development or test command on a staging or production server.

To accomplish this, I used the setterm command in my server(s) ~./bashrc file to inverse the colours of the terminal when connecting and restore the colours when exiting.


# Inverts console colours so that we know that we are in a remote server. 
# This is very important to avoid running commands on the server by accident.
setterm --inversescreen on

# This ensures we restore the console colours after exiting.
function restore_screen_colours {
  setterm --inversescreen off
trap restore_screen_colours EXIT

I then put this in all the servers' ~/.bashrc files so that I know when my terminal is on a remote server or not. Works great.

However, what this doesn't do is tell you which server you are connected to, which is what you're after.

To accomplish that you can simply use custom setterm parameters to set unique colours for each of your 3 or 4 servers.

For example, one server can be a white background with red text, like so:

setterm --background white --foreground red

Here are all the options for setterm:

 --term          <terminal_name>   override TERM environment variable
 --reset                           reset terminal to power-on state
 --initialize                      display init string, and use default settings
 --default                         use default terminal settings
 --store                           save current terminal settings as default
 --cursor        [on|off]          display cursor
 --repeat        [on|off]          keyboard repeat
 --appcursorkeys [on|off]          cursor key application mode
 --linewrap      [on|off]          continue on a new line when a line is full
 --inversescreen [on|off]          swap colors for the whole screen
 --foreground    default|<color>   set foreground color
 --background    default|<color>   set background color
 --ulcolor       [bright] <color>  set underlined text color
 --hbcolor       [bright] <color>  set bold text color
                 <color>: black blue cyan green grey magenta red white yellow
 --bold          [on|off]          bold
 --half-bright   [on|off]          dim
 --blink         [on|off]          blink
 --underline     [on|off]          underline
 --reverse       [on|off]          swap foreground and background colors
 --clear         [all|rest]        clear screen and set cursor position
 --tabs          [<number>...]     set these tab stop positions, or show them
 --clrtabs       [<number>...]     clear these tab stop positions, or all
 --regtabs       [1-160]           set a regular tab stop interval
 --blank         [0-60|force|poke] set time of inactivity before screen blanks
 --dump          [<number>]        write vcsa<number> console dump to file
 --append        [<number>]        append vcsa<number> console dump to file
 --file          <filename>        name of the dump file
 --msg           [on|off]          send kernel messages to console
 --msglevel      0-8               kernel console log level
 --powersave     [on|vsync|hsync|powerdown|off]
                                   set vesa powersaving features
 --powerdown     [0-60]            set vesa powerdown interval in minutes
 --blength       [0-2000]          duration of the bell in milliseconds
 --bfreq         <number>          bell frequency in Hertz
 --version                         show version information and exit
 --help                            display this help and exit

You must log in to answer this question.

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