I have 2 different servers (all running Linux) to which I connect via ssh.

After login all terminal background looks the same. I want to have different color themes or backgrounds for different servers. For example, if I ssh to "server-1" my terminal background turns Gray and if I login to "server-2" it turn blue.

How can I achieve this?

Note: I am using "MATE Terminal".

1 Answer 1


In XTerm, you can use the control sequences in your .bashrc or such. Like:

if [ "${-//[!i]/}" = 'i' ]; then case $HOSTNAME in server1) echo -e '\e]11;darkgray\a\e]10;black\a' ;; server2) echo -e '\e]11;darkblue\a\e]10;gray\a' ;; esac fi

Other terminal emulators have similar features.

In Putty, you can configure the color directly, not sure if these control sequences from XTerm will work (they didn't in cursory testing, haven't looked at my settings, though).

The reason for checking the shell interactive flag instead of for stdout being a terminal (i.e. exit status of tty or [ -t 1 ]) is in some cases (e.g. Ansible) a tty may be allocated (ssh -tt) even for batch processing, and outputting to stdout in such a circunstance could make the stream not "clean", which may cause trouble (e.g. Ansible).

Note: I tested [ "${-//[!i]/}" = 'i' ] for the versions of bash, mksh, zsh and ksh available for Centos 7, and it worked in all of them.

  • I tried this in XTerm but it does not work. Is "server1" output of command hostname ?
    – d.putto
    Commented May 8, 2015 at 16:45
  • $HOSTNAME is an environment variable. If you can use xterm you could change the file ~/.Xresources of each server to configure the background color.
    – statox
    Commented May 8, 2015 at 16:53
  • @d.putto: try echo $HOSTNAME when logged in each server and replace accordingly. If you just wanna try the color changing sequences, just run the echo -e ... commands while in the shell on XTerm.
    – spuk
    Commented May 8, 2015 at 17:05
  • as @statox says, if you're willing to run XTerm with different parameters, you can configure the colors for different windows in ~/.Xresources, or run XTerm with xterm -bg blue -fg gray, etc.
    – spuk
    Commented May 8, 2015 at 17:06
  • 1
    Thanks. Another solution I've seen is checking for "i" in $-, meaning bash is interactive. I think all are equally fine.
    – egmont
    Commented May 11, 2015 at 20:24

You must log in to answer this question.

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