如何在特定时间内授予 sudo 命令权限?有没有办法在 sudoers 文件中仅授予 2 天内执行特定命令的权限?
答案1
sudoers 文件不支持基于时间的限制,但有一个简单的方法。创建一个包含您更改的文件/etc/sudoers.d/
(使用sudo visudo -f /etc/sudoers.d/yourfile
):
file.sh
将以下内容添加到文件(例如:)
mv /etc/sudoers.d/yourfile /etc/sudoers.d/.yourfile
这将禁用你的更改:
sudo at -f file.sh 2pm + 2 days
例子:
at -f file.sh 2pm + 2 days
warning: commands will be executed using /bin/sh
job 4 at Thu Oct 15 14:00:00 2015
在这种情况下,它会在你发出命令两天后的下午 2 点移动文件。at
手动的有一些选项(您可以使用时间、天、周、月、年、关键字,例如下一个或添加/减去周期)。进行一些带有选项的测试,以确保您理解它(需要考虑的一点:at
在当天下午 2 点之前或之后开始很重要。)
at
重启后也能继续使用,因此是处理此类事情的好工具。而且您可以切换访问权限...
sudo mv /etc/sudoers.d/.yourfile /etc/sudoers.d/yourfile | at 2pm + 2 days
sudo mv /etc/sudoers.d/yourfile /etc/sudoers.d/.yourfile | at 2pm + 4 days
sudo mv /etc/sudoers.d/.yourfile /etc/sudoers.d/yourfile | at 2pm + 6 days
sudo mv /etc/sudoers.d/yourfile /etc/sudoers.d/.yourfile | at 2pm + 8 days
并让该用户发疯(我现在能做什么呢)。
自述文件位于/etc/sudoers/
:
# As of Debian version 1.7.2p1-1, the default /etc/sudoers file created on
# installation of the package now includes the directive:
#
# #includedir /etc/sudoers.d
#
# This will cause sudo to read and parse any files in the /etc/sudoers.d
# directory that do not end in '~' or contain a '.' character.
#
# Note that there must be at least one file in the sudoers.d directory (this
# one will do), and all files in this directory should be mode 0440.
#
# Note also, that because sudoers contents can vary widely, no attempt is
# made to add this directive to existing sudoers files on upgrade. Feel free
# to add the above directive to the end of your /etc/sudoers file to enable
# this functionality for existing installations if you wish!
#
# Finally, please note that using the visudo command is the recommended way
# to update sudoers content, since it protects against many failure modes.
# See the man page for visudo for more information.
如果我没看错的话,它不会执行名称中带有“.”的任何文件。因此,mv
我在第一个命令中将“.”放在前面,使其不可见。如果正确理解,您可以在任何地方放置“.”。小心使用“~”,它被 gEdit 等编辑器用作“备份”功能。
at
默认情况下未安装。要安装
sudo apt-get install at