How can I automatically change the background/foreground color of my terminal based on the host that I SSH into?

My current setup is an Ubuntu desktop running Gnome-Terminal that SSHs into a set of RHEL5 hosts. I have an SSH config file that I use to connect to these hosts.

I have searched this site, as well as many others, but haven't been able to get any posted solution to work. So far, I have tried using escape characters, setterm, and tput. Those commands work fine when I am not SSH'd into a machine. As soon as I SSH, however, I loose the set colors. Also, I have messed around with the Gnome-Terminal profile settings, but I'd rather not manually set the profile each time I run SSH.

  • Please check this link for more information askubuntu.com/questions/13705/… Nov 1, 2013 at 9:20
  • That is definitely a possibility, but there is one problem with it. If I SSH into machine A, and then SSH into machine B, the terminal has all of my history with machine A will be closed, and I will lose the history. Nov 1, 2013 at 22:01

1 Answer 1


You can dynamically set the foreground and background colors by commands such as these:

echo -ne '\e]10;red\a'
echo -ne '\e]11;#ffff00\a'

I would probably emit these as part of $PS1 or $PROMPT_COMMAND both locally and on the remote servers (with different color on each server). That way when you terminate your ssh command, you'll get back to the local color (and it even works as expected via multiple ssh hops).

  • Sorry, where do I put this so that it gets run when I exit the ssh?
    – Kvothe
    Nov 19, 2020 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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