2

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}')
  • 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 Feb 11 '18 at 8:10
5

Either use 'su -' to get a login shell, or move the aliases to ~/.bashrc. See: https://superuser.com/questions/183870/difference-between-bashrc-and-bash-profile/183980#183980

| improve this answer | |
1

If you ever get different color behaviours between different user ids, first type "which vi" and see what editor you're actually dealing with. And then look at your .vimrc next, assuming it's vim. If not, make sure you have this in your .bashrc

alias vi='vim'
| improve this answer | |
0

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.

| improve this answer | |
New contributor
Jared is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.