
我想使用與我用來登入(透過 GUI、shell 或 SSH)帳戶的密碼不同的密碼來將我的使用者提升到 sudo 狀態。這可能嗎?
編輯: 由於設定 root 密碼將允許以 root 身分登錄,因此這不是一個好方法。我更喜歡使用者特定的 sudo 密碼,而不是系統範圍的 root 密碼。
答案1
來自 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”.
答案2
您是否在 sudoers man 中尋找此內容?
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.
答案3
如何透過 SSH 停用密碼登入並允許公鑰登錄,您可以在其中設定難以猜測的密碼。那麼本地密碼可以更短並由 sudo 使用。
除此之外,您必須配置/etc/pam.d/sudo
為使用不同的(或附加的)模組,乍看之下pam_dialpass
可能會滿足您的需求。
您也可以為其中一個配置 LDAP 配置,為另一個配置本機密碼。這完全取決於您能夠並願意做出多少改變,有哪些模組可用等等。
答案4
解決方案1:newgrp
解決您的用例的一個簡單方法是:NOPASSWD
與群組和群組密碼結合使用:
向 sudoers 添加一行:
%rudo ALL=(ALL:ALL) NOPASSWD:ALL
建立一個passwd保護群組:
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 使用者的密碼,但只有sudo 群組中的使用者才能使用sudo(與上面的群組解決方案不同,群組中的任何人或具有群組passwd 的任何人都可以使用sudo) 。
一個小問題是預設 runas 使用者現在是sudo
sudo 作為 root,您需要明確指定 root:
sudo -u root <cmd>
但定義別名 ( alias sudo='sudo -u root'
) 或間接 sudo 指令很容易。