Answers:
来自man sudoers:
rootpw If set, sudo will prompt for the root password instead of the
password of the invoking user. This flag is off by default.
runaspw If set, sudo will prompt for the password of the user defined
by the runas_default option (defaults to root) instead of the
password of the invoking user. This flag is off by default.
或者,您可以完全通过ssh禁止基于密码的登录。要求使用密码加密密钥进行远程登录。然后,您可以自由使用sudo的密码。相关选项是
来自man sshd_config
PasswordAuthentication
Specifies whether password authentication is allowed. The default
is “yes”.
您是在sudoers男人中寻找这个吗?
targetpw If set, sudo will prompt for the password of the user
specified by the -u option (defaults to root) instead of the
password of the invoking user.
解决方案1:newgrp
解决用例的一种简单方法是:NOPASSWD
与组和组passwd结合使用:
在sudoers中添加一行:
%rudo ALL=(ALL:ALL) NOPASSWD:ALL
创建一个受密码保护的组:
groupadd rudo
gpasswd rudo # Enter passwd
现在,当您以非特权用户身份登录(假设您尚未在rudo
组中)时,登录到该rudo
组,此时将提示您输入密码。
login user
newgrp rudo
现在sudo
,只要您保持登录状态,就可以运行无密码操作。
解决方案2:runaspw
使用一种更好,可能更安全的方法来执行此操作runaspw
。runaspw
与runas_default
选项相关联,因此您也必须添加该选项。
假设您已经具有默认的%sudo
组条目:
%sudo ALL=(ALL:ALL) ALL
将这些行添加到sudoers文件中:
Defaults:%sudo runas_default=sudo
Defaults:%sudo runaspw
现在添加一个sudo
具有密码的新用户:
useradd sudo -d /nonexistent -s /usr/sbin/nologin -MNr
passwd sudo
现在,将提示sudo组用户输入sudo用户的passwd,但只有sudo组中的用户才可以进行sudo(与上述组解决方案不同,该组中的任何人或组passwd都可以使用sudo)。
一个小问题是默认的runas用户现在sudo
是sudo作为root用户,您需要显式指定root:
sudo -u root <cmd>
但是很容易定义别名(alias sudo='sudo -u root'
)或间接sudo命令。