Linux :: /etc/profile.d 中的共享别名不适用于 root 用户,为什么?

vir*_*yes 6 linux root alias user-profile

在我的 Fedora 14 开发人员机器上,我可以将 sharedAliases.sh 文件添加到 /etc/profile.d —— 然后我的用户和 root 用户都可以访问共享别名。

切换到远程 CentOS 5.7 机器,看起来完全相同的配置,没有骰子,root 用户无权访问共享别名。

这可能是因为我 SSH 进入 CentOS 机器,不确定。无论如何,蹩脚的解决方法是将共享别名复制到 root 用户的 .bashrc 中,因为这是我能够将所需的首选项放入 root 的唯一方法(是的,我知道我应该 sudo,但一直在使用 su 滚动年)。

感谢您的想法,如果可能的话,宁愿不必重复别名。

qua*_*nta 7

必须与通过 ssh 登录与直接登录机器时的差异有关

我怀疑您在su没有破折号 ( -) 的情况下执行命令,如果是这样,它将调用交互式非登录shell。结合你只有以下内容/root/.bashrc

# Test for an interactive shell.  There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
if [[ $- != *i* ]] ; then
    # Shell is non-interactive.  Be done now!
    return
fi
Run Code Online (Sandbox Code Playgroud)

(不是来源/etc/bashrc

而如果您直接 login 或 use su -,它将调用登录shell,/etc/profile读取并sharedAliases.sh执行。

要查看哪个文件被不同的 shell 读取,通过以 root 身份执行以下命令将日志添加到所有这些文件:

echo "echo 'running /etc/bashrc'" >> /etc/bashrc
echo "echo 'running /etc/profile'" >> /etc/profile
echo "echo 'running /root/.bashrc'" >> ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

创建测试别名:

# echo "alias list='ls'" > /etc/profile.d/test.sh
Run Code Online (Sandbox Code Playgroud)

现在,以普通用户身份登录并使用su,您将看到如下内容:

$ su
Password: 
running /root/.bashrc
bash-3.2# list
bash: list: command not found
Run Code Online (Sandbox Code Playgroud)

并与su -

$ su -
Password: 
running /etc/profile
running /root/.bashrc
# list
total 226540
-rw-rw-r-- 1 root root     60148987 Apr  1  2011 3241948.flv
Run Code Online (Sandbox Code Playgroud)