su -s '/bin/bash' 继续在 /etc/passwd 中使用 shell

su -s '/bin/bash' 继续在 /etc/passwd 中使用 shell

我在 /etc/passwd 中手动编辑了 root 的 shell,但有一个拼写错误。更糟糕的是,我的盒子上没有 sudo 用户。

所以我尝试了以下方法,得到了以下结果:

$ su -s '/bin/bash chsh root' root
Password: 
Cannot execute /usr/bin/scrrn: No such file or directory

(你看到我做了什么,对吧?呃。)

我认为这是因为这个,从手册页来看:

If the target user has a restricted shell (i.e. the shell field of
this user's entry in /etc/passwd is not listed in /etc/shells),
then the --shell option or the $SHELL environment variable won't be
taken into account, unless su is called by root.

也许我的问题不应该专门针对 su -s 但这是我可以开始的地方。

答案1

一种可能是在单用户模式下启动系统,或者使用救援启动 USB 闪存盘(与您的发行版相关)来更正中的拼写错误/etc/passwd

在许多 Linux 系统上,您可以告诉 GRUB 引导加载程序运行带参数的内核init=/bin/bash。一旦你这样做了,你可以重新编辑小心你的/etc/passwd

顺便说一句,改变外壳的正确做法是,chsh但你真的不想chsh这样/usr/bin/screenroot

(您可以创建另一个 uid 0 和 shell 帐户/usr/bin/screen;我不确定这样做是否明智......)

最后,仔细阅读苏(1)

-s--shell=

         Run the specified shell instead of the default.

所以你应该跑/bin/su -s /bin/bash;注意那/bin/bash chsh root不是可执行文件的路径(可以是执行(2)-d 作为 shell),但如果 shell 未在中列出,则对 root 不起作用/etc/shells

你也可以尝试/usr/bin/sudo SHELL=/bin/bash -s

答案2

我希望我能给 @Basile Starynkevitch 50% 的信任,或者即使他写了单独的答案。因为他的建议之一绝对让我走上了正轨。

1 - 正如 @Basile Starynkevitch 所说,我能够启动机器,并且在 GRUB 中,我点击“e”来编辑启动选项。在以“linux”开头的行上我添加了参数:

init=/bin/bash

这将我引导到 root bash shell。进步!

2 - 作为文件系统上的 root,您仍然无法编辑任何内容,因为文件系统是以只读方式安装的(即使您从我发现的 GRUB 中删除该选项,但我没有研究原因),因此您必须输入:

# mount -o remount,rw /

然后您可以继续:

3 - 编辑 /etc/passwd 文件

正如@Basile Starynkevitch 所说:

顺便说一句,更改 shell 的正确做法是 chsh

他是对的。但我刚刚做了

# vi /etc/passwd

因为我通常不会做同样的蠢事两次。

相关内容