以 root 身份登录时 ~/.bashrc 不运行

lor*_*mon 1 root bashrc

当我以 root 身份登录时:

ubuntu login: root                                                                                  
Password:                                                                                               
Last login: Tue Apr  7 17:31:14 CDT 2020 on ttyS0                                                       
Welcome to Ubuntu 19.04 (GNU/Linux 5.0.0-38-generic x86_64)                                             
Run Code Online (Sandbox Code Playgroud)

我的 /root/.bashrc (因此 /root/.bash_aliases)未运行。这是正常的吗?我怎样才能解决这个问题?

更多信息:

# getent passwd| grep root                                     
root:x:0:0:root:/root:/bin/bash
Run Code Online (Sandbox Code Playgroud)

我的/root/.profile是正常的:

# cat /root/.profile                                           
# ~/.profile: executed by Bourne-compatible login shells.                       

if [ "$BASH" ]; then                                                            
  if [ -f ~/.bashrc ]; then                                                     
    . ~/.bashrc                                                                 
  fi                                                                            
fi                                                                              

mesg n || true                                                                  
Run Code Online (Sandbox Code Playgroud)

$BASH 设置正确:

# echo $BASH                                                   
/bin/bash
Run Code Online (Sandbox Code Playgroud)

而/root/.bashrc也正常。这是一个片段:

if [ -f ~/.bash_aliases ]; then                                                 
    . ~/.bash_aliases                                                           
fi                                                                              
Run Code Online (Sandbox Code Playgroud)

如果我从命令行执行“.~/.bashrc”,它会按预期工作。

Gor*_*son 6

如果 或~/.bash_profile存在~/.bash_login,bash 将运行它而不是~/.profile. 某些软件安装程序(通常)会帮助将其设置步骤添加到~/.bash_profile,因此即使您删除它(并将其内容合并到~/.profile),它也可能会在以后重新创建。

理想情况下,我建议将所有 bash 特定的东西(即 source ~/.bashrc)移到~/.bash_profile,让它也获取通用的~/.profile,只留下mesg通用的~/.profile(这样如果你在不同的 shell 中启动它就会工作)

所以这就是我要输入的内容~/.profile

# cat /root/.profile                                           
# ~/.profile: executed by Bourne-compatible login shells.                       

mesg n || true 
Run Code Online (Sandbox Code Playgroud)

并且~/.bash_profile(请注意,它不必测试$BASH,因为它已经位于 bash 特定的文件中):

# ~/.bash_profile: executed by bash login shells.

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# If the generic profile exists, source that as well.
if [ -f ~/.profile ]; then                                                     
    . ~/. profile                                                                 
fi
Run Code Online (Sandbox Code Playgroud)