【发布时间】:2013-02-04 04:22:49
【问题描述】:
假设我是普通用户,我将切换到root:
user ~ $ su - root
Password:
root ~ #
所以一旦我以root 登录,我希望自动运行以下命令:
source .bash_profile
请问如何让above command 自动运行?
【问题讨论】:
标签: macos root .bash-profile
假设我是普通用户,我将切换到root:
user ~ $ su - root
Password:
root ~ #
所以一旦我以root 登录,我希望自动运行以下命令:
source .bash_profile
请问如何让above command 自动运行?
【问题讨论】:
标签: macos root .bash-profile
根据 bash 手册页,.bash_profile 用于登录 shell,而.bashrc 用于交互式非登录 shell。
在您的情况下,您不需要像这样获取.bash_profile。
您只需将source .bash_profile 放入根目录的.bashrc 文件中
if [ -f ~/.bash_profile ]; then
source ~/.bash_profile
fi
阅读me 以更好地理解.bash_profile 和.bashrc
更新
例子:
[root@sgeorge-ld ~]# cat .bashrc | tail -1
echo "Testing .bashrc for a stack query"
[root@sgeorge-ld ~]# exit
logout
[sgeorge@sgeorge-ld ~]$ su - root
Password:
Testing .bashrc for a stack query
[root@sgeorge-ld ~]#
【讨论】:
~/.bash_profile 吗?
if 条件放在/root/.bashrc 中。对吗?
su -。然后当我变成root时,什么都没有发生。
.bash_profile 而不是.bashrc
首先,当您切换到 root 用户时,您仍将是您的普通用户的主目录。您要执行哪个 .bash_profile? /Users/myuser/.bash_profile 还是 root 的 /var/root/.bash_profile?
无论你想执行什么,你都可以编辑 /var/root/.bashrc(如果你没有,创建一个)并在那里添加你的命令。
【讨论】:
echo 'hello' 添加到:/var/root/.bash_profile 和/var/root/.bashrc 作为根。但是当我su - root 再次进入里面时,什么都没有发生!
su - root代替sudo -s
/bin/sh。我使用/bin/bash。为此,您可以编辑/etc/passwd 文件。或者,如果你想继续使用 /bin/sh,而不是 /var/root/.bash_profile,你需要 /var/root/.profile
echo $SHELL,你会看到你的shell是什么。
/var/root/.profile,添加echo "hello",然后不使用su - root,而是使用sudo -s。它会起作用的。