24

So you want to change terminal colors and reset them back on exit?

It's possible thanks to .ssh/config, alias, and setterm

1
  • 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, 2013 at 8:13

5 Answers 5

27
  • .bash_aliases:
    function ssh_alias() {
      ssh "$@";
      setterm -default -clear rest;
      # If `-clear rest` gives error `setterm: argument error: 'rest'`, try `-clear reset` instead 
    }
    
    alias ssh=ssh_alias
    
  • /etc/ssh/ssh_config:
    # Ensure this line exists:
      PermitLocalCommand yes
    
  • .ssh/config:
    Host your.production.host
      User root
      LocalCommand setterm -term linux -back red -fore white -clear rest
    

In Bash, you can now:

some command

# all in default colors:
  ssh your.production.host

# colors changed:
# ....

exit
# colors changed back! yeea!


Alternative to setterm:

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

  • For example, xtermcontrol --bg '#600' will make the terminal background a dark red, although you may need to install xtermcontrol before using it (e.g. sudo apt install xtermcontrol on Debian-based systems)
5
  • 2
    When i login I get the following error: setterm: argument error: 'rest' Any idea why?
    – linello
    Aug 29, 2018 at 9:07
  • man setterm, search there --clear. In mine version it exists.
    – gaRex
    Aug 29, 2018 at 13:15
  • 1
    Can anyone add description of the changes done
    – Nilesh
    Feb 4, 2019 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, 2020 at 14:04
  • Since you're using a shell wrapper anyway, it might make more sense to keep all setterm use in it, otherwise you have half the logic in the SSH config, the other in the wrapper. If you need per-host tuning, probably easier to differentiate via wrappers, too.
    – Christian
    Jun 24, 2021 at 21:16
7

(Read gaRex's response first)

setterm has changed the arguments in recent versions: (more info: man setterm)

  • .bash_aliases:
    function ssh_alias() {
      ssh $@;
      setterm --default --clear all;
    }
    
    alias ssh=ssh_alias
    
    You can still use --clear rest and reload .bash_aliases via exec bash

  • .ssh/config:
    Host myproject.pro
      HostName myproject.com
      User root
      IdentityFile ~/.ssh/myproject
      LocalCommand setterm --term linux --background white --foreground black --clear all
    
4
  • This is a 3 year old question and your answer is similar to the one already answered
    – SeanClt
    Mar 26, 2016 at 20:52
  • 3
    yep... I only wanted to add more info. Could be a response to gaRex's response.
    – JoniJnm
    Mar 26, 2016 at 21:58
  • yep i am with and clearly understand you want to help and we want folks like you
    – SeanClt
    Mar 26, 2016 at 21:59
  • 3
    click on "improve this answer" in my initial answer.
    – gaRex
    Jun 15, 2016 at 6:26
1

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.

1

I follow a similar approach to Enrico's, changing the background/foreground of the terminal, then connecting to the server, and changing back the original color when disconnecting from the servers, with no admin permission needed, only requiring a few lines added to .bash_aliases:

alias background_local='printf "\e]11;#333333\e\\"'
alias background_server='printf "\e]11;#336699\e\\"'
alias goto_server='background_server; ssh server ; background_local"
  • If you wish, you can mix and match, changing also the foreground color with the command:
    alias foreground_server='printf "\e]10;#fff2af\e\\"'
    

I assumed you have a working .ssh/config file, so ssh server connects to your server; for completeness, in .ssh/config you may want to have:

Host server
  HostName address.to.your.server
  User yourusername
0

On Apple Mac/OSX, setterm is not available, but you can use osascript with a little shell script:

#!/bin/sh

DEFAULT_SCHEME=Basic
SCHEME=${1:-$DEFAULT_SCHEME}
SAFE_SCHEME=\"${SCHEME//\"/}\"  # sanitize user input

/usr/bin/osascript <<EOF
tell application "Terminal"
  set current settings of window 1 to settings set $SAFE_SCHEME
end tell
EOF
  • This takes a single argument corresponding to one of the colour schemes terminal knows about (e.g. Ocean) and may be invoked in place of setterm in the above answers, remembering to also add it to ~/.bash_aliases so the terminal reverts to the original colour scheme when when you exit the ssh session

The default bash profile on OSX does not source .bash_aliases, so you may need to add something like this to ~/.bash_profile:

if [ -f ~/.bash_aliases ]; then
  source ~/.bash_aliases
fi
  • For additional information on how to change the terminal colours in OSX, see this SO answer.

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.