bash 用户环境不是来自 chroot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13570558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:51:50  来源:igfitidea点击:

User environment is not sourced with chroot

linuxbashchroot.bash-profile

提问by Labynocle

I have a little problem with a chroot environment and I hope you could help me :)

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

Here's my story:

这是我的故事:

1 - I created a user demo (with a home like /home/demo) and I chrooted him thanks to a script /bin/chrootshellwhich is as following:

1 - 我创建了一个用户演示(有一个像 的家/home/demo),并且由于脚本/bin/chrootshell如下,我将他 chroot 了:

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

2 - Usual login authentication are disabled for this user, so I have to use su - demoto be logged as him

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

Everything works well (like all the chrooted system commands or my java configuration). But each time I become user demo, it seems my .bashrc or /etc/profile are not sourced... And I don't know why.

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

But if I launch a manual bash it works as you can see here:

但是,如果我启动手动 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

As you can see, my $PLOPvariable (describes in /.bashrc == /home/demo/.bashrc) is well set in the second bash, but I don't know why

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

Thanks in advance if you have any clue about my issue :)

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

edit: What I actually don't understand is why SHELL=/bin/chrootshell? in my chroot env I declare my demo user with /bin/bashshell

编辑:我实际上不明白的是为什么SHELL=/bin/chrootshell?在我的 chroot env 中,我用/bin/bashshell声明了我的演示用户

回答by lesmana

As far as I can tell the behaviour that you are experiencing is bash working as designed.

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

In short: when bash is started as a login shell (that is what happens when you call bash with --login) it will read .profilebut not .bashrc. When bash is started as a non login shell then bash will read .bashrcbut not .profile.

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

Read the bash manual chapter about startup filesfor more information.

阅读有关启动文件的 bash 手册章节以获取更多信息。

My suggestion to work around this design decision is to create a .bash_profilewith the following content:

我建议解决这个设计决定是创建一个.bash_profile具有以下内容的:

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

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

That will make bash read .profileand .bashrcif started as login shell, and read only .bashrcif started as non login shell. Now you can put the stuff which needs to be done once (during login) in .profile, and the stuff which needs to be done everytime in .bashrc.

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