调整 /etc/profile 中的 $PATH 不会影响 root

调整 /etc/profile 中的 $PATH 不会影响 root

PATH我在 中添加了一个目录/etc/profile。这对我的用户帐户有效,但对 root 无效。将它添加到我的很容易,/root/.bashrc但我想了解哪里出了问题。这是一个几乎未修改的 Debian 6,所以我认为我的更改应该可以解决问题。

这是我的/etc/profile

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/lib/distcc/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
  PATH="/usr/lib/distcc/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
export PATH

if [ "$PS1" ]; then
  if [ "$BASH" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

# The default umask is now handled by pam_umask.
# See pam_umask(8) and /etc/login.defs.

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

编辑:我添加的路径是distcc-stuff。以下是echo $PATH告诉我的内容:

$ echo $PATH
/usr/lib/distcc/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

答案1

您需要运行登录 shell(或者运行非交互式 shell,但这不是您想要的)来加载/etc/profile

使用

su - username

或者在 root 的情况下

su -

去做这个。

--l与或相同--login,并使 shell 成为登录 shell。

答案2

您需要模拟登录 shell,您可以使用 sudo 来实现-i

sudo -i <command>

man sudo

 -i [command]
             The -i (simulate initial login) option runs the shell specified by the password
             database entry of the target user as a login shell.  This means that login-spe‐
             cific resource files such as .profile or .login will be read by the shell.  If a
             command is specified, it is passed to the shell for execution via the shell's -c
             option.  If no command is specified, an interactive shell is executed.  sudo
             attempts to change to that user's home directory before running the shell.  The
             security policy shall initialize the environment to a minimal set of variables,
             similar to what is present when a user logs in.  The Command Environment section
             in the sudoers(5) manual documents how the -i option affects the environment in
             which a command is run when the sudoers policy is in use.

答案3

需要一个登录 shell,可以使用 sudo 创建:

sudo bash --login <command>

相关内容