Ask Different is a question and answer site for power users of Apple hardware and software. 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

Looking for a way automatically change terminal colors when ssh-ing into remote machine and revert back to the default colors, when exiting the ssh session. Simply, want have another colors when i'm working on the remote machine.

Any idea?

ps: don't want use iTerm2, but standard

share|improve this question
up vote 7 down vote accepted

I'm using the following:

have and shell command, called myssh, with content:

DEF="Basic" #the default profile name
echo "tell app \"Terminal\" to set current settings of first window to settings set \"${PROFILE}\""|osascript
ssh $@
echo "tell app \"Terminal\" to set current settings of first window to settings set \"${DEF}\""|osascript

and using bash aliases for the connect, like:

alias ssweb='myssh Homebrew'
alias ssmail='myssh Ocean'

so, when want ssh into web server, i'm simply write into terminal ssweb, and got the "Homebrew" profile. When the ssh session ends, the aplescript set back to the "Basic".

The aliases should go into your $HOME/.profile or similiar bash-startup file.

The "Homebrew", "Ocean" etc. are the setting profiles from the preferences. (you can set up different fonts, encondings etc) too.

Of course, you can use it directly (without aliases) like:

myssh Pro
       ^        ^
       |        ssh command arguments
       terminal profile name
share|improve this answer
how would you do this for iterm2 ? thanks! – Cosu Jul 11 '11 at 12:33
I stopped using iTerm when in "vim" after selecting some text it is cut out the selection instead of copy.. ;( So, Unfortunately, I haven't any idea about iTerm2 solution. – jm666 Jul 11 '11 at 19:00
Thank you! I've been looking at various kludgy ways to do this for a while, but your method is simple and flexible. Bonus points for developing a tiny framework that can take profiles and commands from different aliases, rather than strewing around a bunch of individual scripts with hard-coded preference sets. – octern Mar 26 '12 at 4:48

I know there's already a couple of answers, but here's an alternative:

There's no general way to change the background color, but we can use a standard way to change the window title. If you're logging in to a lot of non-"Unix server" devices (such as network devices; routers, switches, firewalls etc) then you have to use their shells and cannot install your own shell variables. So, here's another way:

function ssh() {
  H=$(echo $@ | sed -e 's/.*@//g;s/ .*//g')
  echo $@ | grep '@' && U=$(echo $@ | sed -e 's/@.*//g;s/.* //g')
  echo -ne "\033]0;${U:=$(whoami)}@${H%%.*}\007"
  unset H
  unset U
  /usr/bin/ssh $@

Add this to your .bashrc.

Basically, as functions and aliases are preferred to looking in the path, this takes the ssh command and uses the text before and after the "@" to set HOST and USER variables which it then uses to echo an ASCII code which is recognised by the terminal and used to set the title.

You must use "ssh user@host.blah" for this and not "ssh -l user host.blah"

If you don't provide a username it will set it as your current username (obviously if you're using a custom .ssh/config (which I use a lot) then this won't be accurate, but I think it's good enough to be useful.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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