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 user
时user
:
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 - user
或su -l user
:
-, -l, --login
Provide an environment similar to what the user would expect had
the user logged in directly.