Colors in ssh connection

20,725

Solution 1

Read the dircolors.sh subsection from "Beyond Linux From Scratch" book:

This script uses the ~/.dircolors and /etc/dircolors files to control the colors of file names in a directory listing. They control colorized output of things like ls --color. The explanation of how to initialize these files is at the end of this section.

cat > /etc/profile.d/dircolors.sh << "EOF"
# Setup for /bin/ls and /bin/grep to support color, the alias is in /etc/bashrc.
if [ -f "/etc/dircolors" ] ; then
        eval $(dircolors -b /etc/dircolors)

        if [ -f "$HOME/.dircolors" ] ; then
                eval $(dircolors -b $HOME/.dircolors)
        fi
fi
alias ls='ls --color=auto'
alias grep='grep --color=auto'
EOF

Solution 2

Using a combination of https://unix.stackexchange.com/questions/9883/how-can-i-run-a-script-immediately-after-connecting-via-ssh and nik's answer you can do:

ssh host.example.com -t '. /etc/profile; . ~/.profile; /bin/bash'

This will execute your profile scripts on login, then open a bash shell. Your profile scripts are where the colors are defined.

Or, for maximum convenience, add the following to your ~/.ssh/config file:

Host *
  LocalCommand . /etc/profile; . ~/.profile; /bin/bash
Share:
20,725

Related videos on Youtube

Adam Matan
Author by

Adam Matan

Team leader, developer, and public speaker. I build end-to-end apps using modern cloud infrastructure, especially serverless tools. My current position is R&amp;D Manager at Corvid by Wix.com, a serverless platform for rapid web app generation. My CV and contact details are available on my Github README.

Updated on September 17, 2022

Comments

  • Adam Matan
    Adam Matan over 1 year

    I have a colorful bash terminal (e.g. ls and vim show colors when configured to do so).

    How can I have these colors when connecting to a remote server via ssh?

  • slhck
    slhck almost 12 years
    Can you expand your answer a little? The question has 2,800 views — would be great to add a little more context.