/etc/profile/ 中设置的路径未出现在其他用户的环境中

/etc/profile/ 中设置的路径未出现在其他用户的环境中

conda我在 /etc/profile 中有以下 PATH 变量。最后一行应允许我对任何用户使用该命令。

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

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; 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

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

export PATH=/opt/miniconda3/bin:$PATH

当我是 root 时它可以工作:

root@server:~# echo $PATH
/opt/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

但当我这样做su useruser

user@server:/root$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

/opt/miniconda3/bin:如您所见,它未出现在 $PATH 变量中。/etc/profile 中定义的变量不应该在系统范围内可用吗?我该如何实现这一点?

答案1

当您使用 时su user,生成的环境与启动登录 shell 时不同。具体来说,根据man su

   The current environment is passed to the new shell. The value of $PATH
   is reset to /bin:/usr/bin for normal users, or
   /sbin:/bin:/usr/sbin:/usr/bin for the superuser. This may be changed
   with the ENV_PATH and ENV_SUPATH definitions in /etc/login.defs.

要使用su(特别是读取/etc/profile文件)获取登录 shell 环境,您可以使用su - usersu -l user

   -, -l, --login
       Provide an environment similar to what the user would expect had
       the user logged in directly.

相关内容