54

When I connect over ssh to remote hosts everything is just a single font/color. I would like to have colours like I do locally e.g. green for executable and blue for symlinks etc. And such that when I run $ git diff on the ssh host it shows me diff with colours =)

  • Is that an Ubuntu server you're connecting to? – Stefano Palazzo Dec 5 '10 at 20:42
  • @stefano-palazzo: to Debian. – Dima Dec 6 '10 at 0:46

10 Answers 10

35

Because it was xterm on the server, I figured something was wrong with .bashrc

And indeed! ls --color=auto works only when you are connected to TTY. Changing everything to simply --color in .bashrc on the remote host and everything is in pretty colours now.

  • 42
    It would help this answer a lot if you said where you put --color=auto. .bashrc on the server? What commands? – rfay Aug 24 '13 at 19:37
  • 8
    Since this is an old question which is still relevant, I just wanted to add that on Ubuntu systems, the default .bashrc has a case statement which defines which terms are allowed color. If you find the "case "$TERM$" in" line in your .bashrc, adding "xterm) color_prompt=yes;;" will also enable color. Also you can uncomment the "force_color_prompt=yes" line to globally enable it always. – Mike E Jul 6 '15 at 17:58
  • 2
    it was Mike E 's comment that fixed it for me, – Frank Schrijver Sep 24 '16 at 21:10
  • 3
    I'm confused. What does it mean to "changing everything to simply --color"? Can I see an example? – still_dreaming_1 Aug 10 '17 at 21:11
  • 1
    Are you guys talking about the local host, the remote host or both? – Adam Jan 10 '18 at 9:47
19

This worked for me:

ssh -t my_host my_command

-t was the key. Explained.

  • This works perfectly, and is very convenient for one off tasks. Thanks! – XtraSimplicity Jan 4 '17 at 3:17
  • I lost the colorize when I did su - command in the ssh connection prompt – dubis Feb 20 at 8:30
11

Seems like colors were already set in ~/.bashrc for me and the issue is that ssh does not use the bashrc file. You can use bashrc in your ssh session by adding the following to ~/.bash_profile:

if [ -f ~/.bashrc ]; then
      . ~/.bashrc
fi
  • 1
    This worked for me, and was a simple and efficient way to get it done! – flith May 26 '17 at 8:24
7

What's the content of your XTERM env variable on the server when you connect to it?

~ > export | grep -i term
TERM=xterm
  • $ export | grep -i term declare -x TERM="xterm" – Dima Dec 6 '10 at 0:36
4

In my case, the missing part was to have colored ls, grep, etc. which can be added by adding aliases to the .bashrc file:

alias ls='ls --color=auto'
alias grep='grep --color=auto'

etc.

2

I tried changing ~./bashrc settings (both on local and remote server), but it did not seem to work.

Then I noticed that ~/.bashrc of remote server does not even get executed if I connect to it over ssh. So I made ~/.bashrc of remote server to execute by puttinng if [ -f ~/.bashrc ]; then . ~/.bashrc fi in remote server's ~/.bash_profile. (based on https://stackoverflow.com/questions/820517/bashrc-at-ssh-login).

So this solution did not require changing any ~/bashrc files directly but it did require changing ~/bash_profile file of remote server so that ~/bashrc file of remote server got executed.

  • Ubuntu uses .profile and not .bash_profile by default, and the default .profile does source .bashrc. – muru Mar 7 '16 at 22:18
  • That is correct. But does .profile get executed by default when logging in through ssh? – chris544 Mar 7 '16 at 22:43
  • Yes. Provided you didn't override it using .bash_profile, bash runs .profile when started as a login shell. And SSH starts bash as a login shell. – muru Mar 7 '16 at 22:44
  • It is correct that ~/.profile is not read if ~/.bash_profile exists. But doesn't ~/.bash_profile exist on Ubuntu by default? – chris544 Mar 7 '16 at 22:51
  • See my first comment again. No, it doesn't. Ubuntu uses .profile. – muru Mar 7 '16 at 22:52
2

Since the colors worked fine while being loggged in directly, I just un-uncommented the line force_color_prompt=yes in the file ~/.bashrc, that gave me colors over ssh, too:

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

(Ubuntu 18.04 LTS)

  • Upvoted, this seems to be the most straight-forward solution. – snwflk Apr 25 at 12:53
1

In my situation, I recently installed chef-local and it asked me to add a line to .bash_profile. When I sign in, .bashrc never loads anymore, because it saw .bash_profile.

What I did was to add a line in .bash_profile:

source .bashrc
export PATH="/opt/chefdk/embedded/bin:$PATH"

I logged out and back in and got my colored terminal right away.

0

There is a comment from "Mike E" above that contained the answer for me, but it is not only hard to read, it is kind of hard to figure out what he means if you don't use .bashrc a lot - and I don't.

After screwing around a bit, I got the desired results by changing the following lines in ~/.bashrc on the machine I was logging into with ssh:

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac

to:

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
    xterm) color_prompt=yes;;
esac

I am thinking I could have just added "|xterm" after "color" in the first line, or dug around and figure out why ssh was using "xterm" instead of "xterm-color" and change that, but this works and I have other things to do now.

0

I was losing my color when connecting via a proxy because TERM=dumb so I fixed it:

ssh myproxy "ssh pi@localhost -p 5000 -tt 'TERM=xterm bash'"
  • 1
    You should allocate a terminal on the first connection as well, if you're going to do that. – muru Aug 28 '18 at 4:02

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.