How to change terminal colors when connecting to SSH hosts

colorsgnomesshterminal

So you want to change terminal colors and resetting them back on exit? It's possible!

Thanks to .ssh/config, alias and setterm.

Best Answer

.bash_aliases

function ssh_alias() {
    ssh "$@";
    setterm -default -clear rest;
}

alias ssh=ssh_alias

/etc/ssh/ssh_config

# Make sure you have this line there:
PermitLocalCommand yes

.ssh/config

Host your.production.host
  User root
  LocalCommand setterm -term linux -back red -fore white -clear rest

Now you can in bash:

some command
# all in default colors
ssh your.production.host
# colors changed
# ....
exit
# colors changed back! yeea!

NOTE If -clear rest gives you an error setterm: argument error: 'rest' - try -clear reset instead.


Alternative to setterm

If you are using gnome-terminal or another xterm and are frustrated by setterm's limited color choices, and/or your setterm changes are being overridden by color codes in your command prompt ($PS1), you may wish to use xtermcontrol instead of setterm above, as demonstrated in this answer.

For example, xtermcontrol --bg '#600' will make the terminal background a dark red. You may need to install xtermcontrol before using it, e.g. sudo apt install xtermcontrol on Debian-based systems.

Related Question