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

Thanks to .ssh/config, alias and setterm.

  • I know, we have tons of such questions, but I just did not found any simple as my current version. Also to those, who can expand my answer with details, pls expand it. Then after more detailed version we can accept it. – gaRex Jun 5 '13 at 8:13


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

alias ssh=ssh_alias


# Make sure you have this line there:
PermitLocalCommand yes


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
# ....
# 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.

  • 2
    When i login I get the following error: setterm: argument error: 'rest' Any idea why? – linello Aug 29 '18 at 9:07
  • man setterm, search there --clear. In mine version it exists. – gaRex Aug 29 '18 at 13:15
  • 1
    Can anyone add description of the changes done – Nilesh Feb 4 '19 at 5:43
  • Unfortunately this recipe breaks rsync. I am getting these messages when runnung rsync: protocol version mismatch -- is your shell clean? (see the rsync man page for an explanation) rsync error: protocol incompatibility (code 2) at compat.c(178) [Receiver=3.1.3] It is not due to the wrong setterm argument – monok Dec 7 '20 at 14:04

(read gaRex's response first)

setterm has changed the arguments in recent versions:


function ssh_alias() {
    ssh $@;
    setterm --default --clear all;

alias ssh=ssh_alias


Host myproject.pro
    HostName myproject.com
    User root
    IdentityFile ~/.ssh/myproject
    LocalCommand setterm --term linux --background white --foreground black --clear all

You can still use:

--clear rest

You can reload .bash_aliases file with:

exec bash

More info:

man setterm
  • This is a 3 year old question and your answer is similar to the one already answered – SeanClt Mar 26 '16 at 20:52
  • 3
    yep... I only wanted to add more info. Could be a response to gaRex's response. – JoniJnm Mar 26 '16 at 21:58
  • yep i am with and clearly understand you want to help and we want folks like you – SeanClt Mar 26 '16 at 21:59
  • 3
    click on "improve this answer" in my initial answer. – gaRex Jun 15 '16 at 6:26

On Apple Mac/OSX setterm is not available but you can use osascript.

For the above application it's convenient to do this with a little shell script:


    SAFE_SCHEME=\"${SCHEME//\"/}\"  # sanitise user input

    /usr/bin/osascript <<EOF
    tell application "Terminal"
        set current settings of window 1 to settings set $SAFE_SCHEME
    end tell

This script takes a single argument that corresponds to one of the colour schemes that terminal 'knows' about (e.g. Ocean); and may be invoked in place of setterm in the answers above. Remember to add it to the ~/.bash_aliases too, so that the terminal reverts to the original colour scheme when when you exit the ssh session.

Note that the default bash profile on OSX does not source .bash_aliases so you may need to add something like this to your ~/.bash_profile:

    if [ -f ~/.bash_aliases ]; then
        source ~/.bash_aliases

For additional information on how to change the terminal colours in OSX, see this SO answer.


I needed this when connecting to my own computers. What I did was as simple as adding this snippet to my .bash_profile (which is in my dotfiles, so it ends up in most of my computers anyway):

[ -n "$SSH_CONNECTION" ] && echo -e "\033]11;#336699\a"

You can change the 336699 for any hex color you want.

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.