This question's purpose is to document my findings and solution after two hours of struggle, in hopes that it will be useful to others in the future.
Situation
I've got a fresh Linux machine set up. When logged in to it locally, ls --color=auto
correctly displays directory entries in colours. However, when logged in to it remotely via SSH, ls --color=auto
does not seem to work.
Why is it so? How to get coloured ls
over SSH as well?
Other symptoms
The output of dircolors
mirrors the behaviour of ls --color=auto
. When running locally, dircolors
sets a contentful LS_COLORS
environment variable. However when running over SSH, dircolors
sets LS_COLORS=''
instead.
TLDR
If you've ran into this with the aforementioned symptoms, you are probably using a relatively new and/or obscure terminal emulator (I was using Alacritty). Colours work by default because your terminal emulator sets the
COLORTERM
environment variable, which gets picked up byls
. However, this variable is not propagated across SSH by default, which causes the symptom of "colouredls
only works locally".Full explanation
Both
dircolors
andls
(whenLS_COLORS
is unset) behave similarly when it comes to the handling ofTERM
andCOLORTERM
, so I will only addressls
in this explanation.This is the source code of
ls
as of time of writing (permalink):As you can see,
ls
will always colourise its output whenCOLORTERM
is not empty. Only when it's empty or absent will it check whetherTERM
's value is among the known terminals that support colours. Therefore as long as your terminal emulator sets theCOLORTERM
variable (as is the case with Alacritty for example),ls
will colourise by default even when yourTERM
is not known tols
.The problem is, SSH by default only propagates one variable to the server -
TERM
. For older and/or common terminals this is okay, because they are known tols
, but this breaks colourisation for terminals like Alacritty.Solution #1
Add your terminal to the list of known terminals.
dircolors
will then setLS_COLORS
based on your configuration, which will get picked up byls
.This may be easier or harder depending on which distro you are on:
/etc/profile.d/colorls.sh
) that loads/etc/DIR_COLORS
. So you can simply addTERM alacritty
(or whatever yourTERM
is) to this file.~/.bashrc
(defined by/etc/skel/.bashrc
) contains scripting to load~/.dircolors
if it exists. So you can rundircolors --print-database > ~/.dircolors
then add the aforementioned line.~/.bashrc
(or whatever shell you prefer). Here's what Debian has for reference:Solution #2
Make SSH propagate
COLORTERM
.On the machine you are trying to access (server), run:
On your machine you are connecting from (client), run:
This might be preferable if you use multiple different terminal emulators. On the flipside, it does require configuring both the SSH server and client. So pick your poison I guess.