【问题标题】:User environment is not sourced with chroot用户环境不是来自 chroot
【发布时间】:2012-11-26 18:04:18
【问题描述】:

我的 chroot 环境有点问题,希望你能帮助我:)

这是我的故事:

1 - 我创建了一个用户演示(有一个像 /home/demo 这样的家),我通过脚本 /bin/chrootshell 将他 chroot,如下所示:

#!/bin/bash
exec -c /usr/sbin/chroot /home/$USER /bin/bash --login -i

2 - 此用户禁用了通常的登录身份验证,因此我必须使用 su - demo 以他的身份登录

一切正常(就像所有 chrooted 系统命令或我的 java 配置)。但是每次我成为用户演示时,似乎我的 .bashrc 或 /etc/profile 都没有来源......而且我不知道为什么。

但是,如果我启动手动 bash,它会像您在此处看到的那样工作:

root@test:~# su - demo
bash-4.1$ env
PWD=/
SHELL=/bin/chrootshell
SHLVL=1
_=/bin/env
bash-4.1$ bash
bash-4.1$ env
PWD=/
SHLVL=2
SHELL=/bin/chrootshell
PLOP=export variable test
_=/bin/env

如您所见,我的 $PLOP 变量(在 /.bashrc == /home/demo/.bashrc 中描述)在第二个 bash 中设置得很好,但我不知道为什么

如果您对我的问题有任何线索,请提前感谢:)

编辑:我实际上不明白为什么 SHELL=/bin/chrootshell ?在我的 chroot 环境中,我使用 /bin/bash shell 声明我的演示用户

【问题讨论】:

    标签: linux bash chroot .bash-profile


    【解决方案1】:

    据我所知,您遇到的行为是 bash 按设计工作。

    简而言之:当 bash 作为登录 shell 启动时(当您使用 --login 调用 bash 时会发生这种情况),它将读取 .profile 而不是 .bashrc。当 bash 作为非登录 shell 启动时,bash 将读取 .bashrc 而不是 .profile

    阅读有关startup files 的 bash 手册章节以获取更多信息。

    解决此设计决策的建议是创建一个.bash_profile,其中包含以下内容:

    if [ -f "~/.profile" ]; then
        source "~/.profile"
    fi
    
    if [ -f "~/.bashrc" ]; then
        source "~/.bashrc"
    fi
    

    如果作为登录 shell 启动,这将使 bash 读取 .profile.bashrc,如果作为非登录 shell 启动,则只读取 .bashrc。现在你可以把需要做一次(登录时)的东西放在.profile,把每次都需要做的东西放在.bashrc

    【讨论】:

    • 嗨@lesmana,谢谢你的回复!不幸的是,我对你的建议也有同样的问题。我感觉问题出在我的su 命令上,你怎么看?
    • su 的手册页说:“当使用 - 时,必须将其指定为最后一个 su 选项。”。试试:su demo -
    • 是的,你完全正确。但是,如果您检查语法:su [options] [username],这是我的最后一个选择。可以肯定的是,我测试了您的建议,但仍然遇到同样的问题! :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多