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
4

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

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'

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.