是否可以只输入一次 sudo 密码,并将其配置为在其他终端上不需要输入密码?

是否可以只输入一次 sudo 密码,并将其配置为在其他终端上不需要输入密码?

我输入了第一个sudo命令。我输入了密码。一段时间内,我不需要为后续的 sudo 命令输入密码。

现在问题是。我是一个经常打开终端的人。如果在sudo第一次使用 sudo 后打开的终端中使用时不必输入密码,那将非常方便,因为在短时间内,我不必在第一次使用 sudo 的终端中输入 sudo 密码。(抱歉,句子太长了!)

有可能吗?如果不可能,原因何在?如果可以,怎么办?

答案1

当然可以。运行sudo visudo并将此行添加到sudoers文件中:

Defaults        !tty_tickets

详见man sudoers

 tty_tickets       If set, users must authenticate on a per-tty basis.
                   With this flag enabled, sudo will use a separate record
                   in the time stamp file for each tty.  If disabled, a
                   single record is used for all login sessions.  This
                   flag is on by default.

通过设置tty_tickets为关闭(这就是意思!),您可以使单个身份验证被多个会话共享。

答案2

因此,您需要执行以下操作,以便每次启动时仅让 sudo 询问一次密码:

/etc/sudoers.d/00_prompt_once:

## Only ask for the password once for all TTYs per reboot.
## See https://askubuntu.com/a/1278937/367284 and
##     https://github.com/hopeseekr/BashScripts/
Defaults !tty_tickets
Defaults timestamp_timeout = -1

答案3

在你的 sudoers.d 文件中

sudo EDITOR=vim visudo -f /etc/sudoers/<filename>

添加以下内容:

Defaults    timestamp_timeout=-1

man sudoers 显示以下内容:

sudoers uses per-user time stamp files for credential caching. ...  The user may then use sudo without a password for a short period of time  (15 minutes unless overridden by the timestamp_timeout  option). ...   The timestamp_type option can be used to select the type of time stamp record  sudoers will use.

再向下:

 timestamp_timeout
                   Number of minutes that can elapse before sudo will ask for a passwd again.  The timeout may include a fractional component if minute granularity is insufficient, for example 2.5.  The default is 15.  Set
                   this to 0 to always prompt for a password.  If set to a value less than 0 the user's time stamp will not expire until the system is rebooted.  This can be used to allow users to create or delete their own
                   time stamps via “sudo -v” and “sudo -k” respectively.

相关内容