从挂起状态唤醒后,如何强制 sudo 始终要求输入密码?

15 security command-line sudo suspend password

假设我使用 执行命令sudo,然后如果我sudo在接下来的15/5 分钟内使用(不确定它记得我多久),它不会提示我输入密码。如果我至少有那么长时间没有使用它,它只会再次提示我。

但是,如果我暂停我的机器,唤醒它并在该时间段结束之前登录,我可以sudo再次执行命令并且它不会询问我的密码,因为我没有让它达到一定数量时间。

那么我怎样才能做到,无论我有多久没有使用sudo过,一旦我再次登录,它肯定会在暂停后提示我输入密码,即使我仍在那个时间范围内15/5 分钟不使用sudo还没过去?

最有可能的事情是让它执行一个命令,以sudo在执行唤醒时执行的某个命令时删除所有身份缓存。

我正在使用 GNOME 3.18 运行 Ubuntu GNOME 15.10。

kos*_*kos 19

来自man sudo

     -K, --remove-timestamp
                 Similar to the -k option, except that it removes the user's
                 cached credentials entirely and may not be used in conjunc?
                 tion with a command or other option.  This option does not
                 require a password.  Not all security policies support cre?
                 dential caching.
Run Code Online (Sandbox Code Playgroud)

所以你想要的是你的用户在sudo -K每次系统挂起时运行。

Ubuntu 15.04+ (systemd)

这可以在 Ubuntu 15.04+ 上通过在/lib/systemd/system-sleep/.

  1. 运行sudo nano /lib/systemd/system-sleep/disable_sudo_useruser为方便起见,替换为您的用户名);
  2. 粘贴以下脚本(替换user为您用户的用户名):
     -K, --remove-timestamp
                 Similar to the -k option, except that it removes the user's
                 cached credentials entirely and may not be used in conjunc?
                 tion with a command or other option.  This option does not
                 require a password.  Not all security policies support cre?
                 dential caching.
Run Code Online (Sandbox Code Playgroud)
  1. 命中CTRL+ OENTERCTRL+ X;

  2. 运行sudo chmod o+x /lib/systemd/system-sleep/disable_sudo_user;


要为休眠/混合睡眠启用此功能,请改用此脚本:

#!/bin/sh
case $1/$2 in
    pre/suspend)
        su user -c 'sudo -K'
        ;;
esac
Run Code Online (Sandbox Code Playgroud)

以前的 Ubuntu 版本(新贵)

这可以通过在/etc/pm/sleep.d/.

  1. 运行sudo nano /etc/pm/sleep.d/disable_sudo_useruser为方便起见,替换为您的用户名);
  2. 粘贴以下脚本(替换user为您用户的用户名):
#!/bin/sh
case $1 in
    pre)
        su user -c 'sudo -K'
        ;;
esac
Run Code Online (Sandbox Code Playgroud)
  1. 命中CTRL+ OENTERCTRL+ X;

  2. 运行sudo chmod o+x /etc/pm/sleep.d/disable_sudo_user;


要为休眠也启用此功能,请改用此脚本:

#!/bin/sh
case $1 in
    suspend)
        su user -c 'sudo -K'
        ;;
esac
Run Code Online (Sandbox Code Playgroud)

  • 如果你使用 `sudo -u user sudo -k` 会更有趣。:D (2认同)