为什么 root 没有来源 .bash_profile?

sir*_*ion 11 osx root su bashrc

每次我尝试使用su(not su -)以 root 身份登录时,它都不会来自.bash_profileuser1 的主目录。

基本上,我的/var/root目录没有.bash_profile,所以我把副本.bash_profile/var/root测试su -

它也不会自动来源.bash_profile(in var/root)。

无论如何,.bash_profile当我使用su.

我该怎么办?

(它以前有效!有一天,它只是没有来源!也许 bash 中的某些设置更改了?当我source .bash_profile登录后进入时它有效。)

我正在使用 Mac 和 OS X Yosemite。

Mic*_*mer 11

rootOS X 上的默认 shell是/bin/sh. 它sh也是 的一个版本bash,但是当使用名称shBash调用时

尝试尽可能模仿历史版本的启动行为sh,同时也符合 POSIX 标准。

当作为交互式登录 shell 或作为具有--login选项的非交互式 shell 调用时,它首先尝试/etc/profile和 中读取和执行命令~/.profile,按照该顺序。...作为 sh 调用的 shell 不会尝试从任何其他启动文件读取和执行命令

也就是说,它根本不读取.bash_profile无论它是否作为登录 shell 调用。您可以.profile改为使用,甚至可以将一个符号链接到另一个。如果您使用 启动登录外壳su -l.profile则在启动时加载,但.bash_profile永远不会加载。


您还可以dscl用于更改 root 的 shell(请注意,/etc/passwd用于确定 OS X 上的 shell)。您可以使用dscl . -read /Users/root UserShell;检查 root 的当前 shell 。在将其更改为其他内容之前,请查阅文档并仔细考虑。

另一种方法是简单地更改您的su调用以强制bash立即执行。

鉴于您所说的,我可能会推荐符号链接,但您可能希望查看 Bash 的POSIX 模式所做的更改并决定是否要使用它们。


mya*_*aut 5

因为它不被视为“登录外壳”(直接从login, 或调用sshd),而是简单的“交互式外壳”。例如,请参见此处:https : //www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

所以强制su使用登录选项,即带-l选项:

su -l
Run Code Online (Sandbox Code Playgroud)

或者将您的环境放入.bashrc文件中。


Lam*_*ert 5

root 用户将尝试执行 .bashrc 文件而不是 .bash_profile,因为您没有调用登录 shell。

从 bash 手册man bash

   ~/.bash_profile
          The personal initialization file, executed for login shells
   ~/.bashrc
          The individual per-interactive-shell startup file
Run Code Online (Sandbox Code Playgroud)

请注意~.bashrc 文件需要位于 root 用户的主目录中的位置。

要实际回答您的问题,如果您希望使用 user1 的 .bashrc 文件,我建议您在从 user1 到 root 用户的源 .bashrc 之间建立一个(符号)链接。

您还可以考虑通过将行添加. /homedirectory/of/user1/.bash_profile到 root 用户的 .bashrc 文件来获取 user1 .bashrc 甚至 .bash_profile 的源。