5

I have noticed that when I ssh to a server and then su to the root user, I do not get color in bash. In this specific case when I say "do not get color in bash" I am talking about editing files with vim. Now, if I sudo after login I get color, so no problems there. If I su to root and source /root/.bash_profile then I get color as root. But I do not want to have to source .bash_profile file every time I su to root. Here are the contents of my /root/.bashrc and /root/.bash_profile files. What can I do to get color when doing su?

# .bashrc

# User specific aliases and functions

# You may uncomment the following lines if you want `ls' to be     colorized:
export LS_OPTIONS='--color=auto'
eval "`dircolors`"
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'


# Source global definitions
if [ -f /etc/bashrc ]; then
     . /etc/bashrc
fi

=============================================

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
alias vi='/usr/bin/vim'
alias grep='/bin/grep --color'
export EDITOR=/usr/bin/vim

# HISTSIZE = number of lines in memory while session is ongoing
# HISTFILESIZE = maximum number of lines in the history file on   disk
export HISTSIZE=3000
export HISTFILESIZE=5000
export HISTFILE=/root/history/.bash_hist-$(who -m | awk '{print   $1}')
1
  • 1
    Personal opinion: The root user should have no aliases, colours or other bells or whistles in their shell. The very few instances (near to never ever) when you're in a interactive root shell, you should type out exactly what you need to do, and then exit. Accidentally doing the wrong thing is fatal.
    – Kusalananda
    Commented Feb 11, 2018 at 8:10

3 Answers 3

7

Either use su - to get a login shell, or move the aliases to ~/.bashrc. See: Answer on SuperUser

3

For me the solution was to check my $TERM variable by running

printf "$TERM\n"

which I got

xterm

because I was using putty from windows, therefore I edited it in putty like so: Connection>Data>Terminal details>Terminal-type string: xterm-256color

enter image description here

(Also please make sure to save these settings)

Lastly you need to edit /root/.bashrc like so:

sudo vi /root/.bashrc

on lines 38-41 below:

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

to be as follows (again in lines 38-41):

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

Alternatively you can just uncomment line 46:

#force_color_prompt=yes

by removing the #, however I chose to do the above instead.

-1
su -m

Works for me on a terminal su'ing into a pi.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .