在运行 Ubuntu 18.04 的系统中,我可以运行命令(以 root 身份),例如
sudo -u \#2000 echo hi
Run Code Online (Sandbox Code Playgroud)
即使passwd 文件中不存在 uid 2000,这也可以以 UID 2000 运行命令。当我在 Ubuntu 20.04 和 22.04 上尝试相同的操作时,出现错误
sudo: unknown user: #2000
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎样才能恢复旧的行为?我特别希望能够使用密码文件中没有的用户权限运行脚本(在这种情况下,使用 wpcli 将与文件属于 uid 2000 的网站关联的权限)
小智 6
根据,此行为现在sudoers(5)
由选项控制:runas_allow_unknown_id
runas_allow_unknown_id
If enabled, allow matching of runas user and group IDs that are not
present in the password or group databases. In addition to explicitly
matching unknown user or group IDs in a Runas_List, this option also
allows the ALL alias to match unknown IDs. This flag is off by default.
This setting is only supported by version 1.8.30 or higher. Older
versions of sudo always allowed matching of unknown user and group IDs.
Run Code Online (Sandbox Code Playgroud)
因此,您可以添加Defaults runas_allow_unknown_id
(/etc/sudoers
用于visudo
安全地编辑该文件),然后sudo
允许您指定任意 UID,就像旧版本(20.04 之前的版本)中一样。
请注意,恢复旧行为可能并不安全,因为该设置是为了响应CVE-2019-19232和CVE-2019-19234而引入的。
PS 另请注意,sudo
当 UID 指定的用户不存在(例如未在 中列出/etc/passwd
)时,无法选择 GID,因此它使用与调用者进程相同的主 GID 和补充 GID 运行命令,这并不总是一个好主意:
# id
uid=0(root) gid=0(root) groups=0(root)
# sudo -u \#2000 id
uid=2000 gid=0(root) groups=0(root)
#
Run Code Online (Sandbox Code Playgroud)
我想以sudo
所描述的方式使用可能还会带来其他安全问题。