如何强制sudo在挂起唤醒后始终要求输入密码?

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

但是,如果我暂停我的机器,将其唤醒,并在该时间段结束前登录,我可以再次执行sudo命令,它不会要求我输入密码,因为我没有让它成为特定的多少时间。

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

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

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

来自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. 

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

Ubuntu 15.04+(systemd)

这可以通过在/lib/systemd/system-sleep/放置一个脚本在Ubuntu 15.04+上完成。

  1. 运行sudo nano /lib/systemd/system-sleep/disable_sudo_user (为方便起见,用用户的用户名替换user );
  2. 粘贴以下脚本(用user的用户名替换user ):
 #!/bin/sh case $1/$2 in pre/suspend) su user -c 'sudo -K' ;; esac 
  1. CTRL + OENTERCTRL + X ;

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


要为hibernate/混合睡眠启用此function,请改用此脚本:

 #!/bin/sh case $1 in pre) su user -c 'sudo -K' ;; esac 

以前的Ubuntu版本(Upstart)

这可以通过在/etc/pm/sleep.d/放置脚本在以前的Ubuntu版本上完成。

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

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


要为hibernate启用此function,请改用此脚本:

 #!/bin/sh case $1 in suspend|hybernate) su user -c 'sudo -K' ;; esac 

只有你是那个偏执狂! 您可以使用sudo-K选项。

 -K, --reset-timestamp When used without a command, invalidates the user's cached credentials. In other words, the next time sudo is run a password will be required. This option does not require a password and was added to allow a user to revoke sudo permissions from a .logout file. When used in conjunction with a command or an option that may require a password, this option will cause sudo to ignore the user's cached credentials. As a result, sudo will prompt for a password (if one is required by the security policy) and will not update the user's cached credentials. Not all security policies support credential caching. 

例如,

 sudo -K  

或者您可以将计算机放在由机器人保护的金属盒中:)

又一种方法

输入’sudo visudo’

转到说“’默认值env_reset’的行

并改为:

‘默认值为env_reset,timestamp_timeout = 0

然后保存文件。

通过将超时设置为0,系统将每次提示您输入密码。