How to change terminal colors when connecting to SSH hosts

17

14

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

Thanks to .ssh/config, alias and setterm.

gaRex

Posted 2013-06-05T08:12:02.170

Reputation: 221

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 – 2013-06-05T08:13:39.317

Answers

20

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

gaRex

Posted 2013-06-05T08:12:02.170

Reputation: 221

2When i login I get the following error: setterm: argument error: 'rest' Any idea why? – linello – 2018-08-29T09:07:31.550

man setterm, search there --clear. In mine version it exists. – gaRex – 2018-08-29T13:15:49.060

Can anyone add description of the changes done – Nilesh – 2019-02-04T05:43:51.130

4

(read gaRex's response first)

setterm has changed the arguments in recent versions:

.bash_aliases

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

alias ssh=ssh_alias

.ssh/config

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

JoniJnm

Posted 2013-06-05T08:12:02.170

Reputation: 141

This is a 3 year old question and your answer is similar to the one already answered – SeanClt – 2016-03-26T20:52:05.997

3yep... I only wanted to add more info. Could be a response to gaRex's response. – JoniJnm – 2016-03-26T21:58:39.150

yep i am with and clearly understand you want to help and we want folks like you – SeanClt – 2016-03-26T21:59:56.487

3click on "improve this answer" in my initial answer. – gaRex – 2016-06-15T06:26:11.453

0

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:

    #!/bin/sh

    DEFAULT_SCHEME=Basic
    SCHEME=${1:-$DEFAULT_SCHEME}
    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
    EOF

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
    fi

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

Martin CR

Posted 2013-06-05T08:12:02.170

Reputation: 1

0

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.

Enrico

Posted 2013-06-05T08:12:02.170

Reputation: 141