Linux – have the terminal background change based on hostname

gnome-terminallinuxsshterminalUbuntu

I'd like to have my terminal background color change based on hostname. My reasoning for this is mainly to have and extra way to remind myself what server I am on, so I don't do something stupid.

Is there someway I can have it use the standard aubergine background if I am on my local machine, then change red if I ssh into a production web server, or other hosts that I could specify?

Best Answer

You can use ssh/config's localcommand option to run a command whenever an alias is used. I use

host hostname
    user myusername
    localcommand xtermcontrol --bg '#abc'

This depends on xtermcontrol and your term being xterm. Presumably there are other apps for other terms.

The only problem with this approach is that it happens when you call ssh. There's nothing to undo the color change. I've done it by wrapping a function around ssh, but that has its drawbacks too.

function ssh() {
  FG=$(xtermcontrol --get-fg)
  BG=$(xtermcontrol --get-bg)
  $(which ssh) "$@"
  xtermcontrol --fg="$FG"
  xtermcontrol --bg="$BG"
}
Related Question