So you want to change terminal colors and reset them back on exit?
It's possible thanks to .ssh/config
, alias
, and setterm
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communitySo you want to change terminal colors and reset them back on exit?
It's possible thanks to .ssh/config
, alias
, and setterm
.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!
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.
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)setterm: argument error: 'rest'
Any idea why?
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
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.
Jun 24, 2021 at 21:16
(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
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.
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"
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
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
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
I'm using Yakuake, but the same steps work with Konsole, so this answer is "KDE/Konsole" specific.
I wanted to change terminal colors when connected to important servers, and switch it back when ssh session was closed. Based on gaRex's answer I did the following:
PermitLocalCommand yes
in ~/.ssh/config
LocalCommand konsoleprofile ColorScheme=RedOnBlack;TabColor=#FF0000
to the Host
directive for production serversssh
to be a function in my ~/.zshrc
, by inserting ssh() { /usr/bin/ssh "$@" ; konsoleprofile ColorScheme=Breeze}
there/usr/bin/konsoleprofile
is a neat script, self described as
A command-line tool to change the current tab's profile options.
So before every ssh connection, my current console will change it's color scheme to RedOnBlack
, and tab color will be red too (this works only in Konsole so far, can't find a way to change yakuake tab color)
And then, after SSH is closed, color scheme will be reset to my default - Breeze.