有没有办法让用户只执行 2 天的命令

Abb*_*asi 14 sudo

如何在特定时间授予 sudo 命令权限?有什么方法可以授予仅在 sudoers 文件中执行特定命令 2 天的权限。?

Rin*_*ind 19

sudoers 文件不支持基于时间的限制,但有一个简单的方法。创建一个包含您在/etc/sudoers.d/(with sudo visudo -f /etc/sudoers.d/yourfile) 中的更改的文件:

file.sh将以下内容添加到文件(例如:)

mv /etc/sudoers.d/yourfile /etc/sudoers.d/.yourfile
Run Code Online (Sandbox Code Playgroud)

这将禁用您的更改:

 sudo at -f file.sh 2pm + 2 days
Run Code Online (Sandbox Code Playgroud)

例子:

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
Run Code Online (Sandbox Code Playgroud)

在这种情况下,它会在您发出命令 2 天后的下午 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
Run Code Online (Sandbox Code Playgroud)

并让该用户发疯(wtf 现在我可以做到这一点)。

中的自述文件/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.
Run Code Online (Sandbox Code Playgroud)

如果我正确阅读了它,它将不会执行任何带有“.”的文件。名字中的任何地方。所以第一个mv命令我放置了“。” 在前面使它也看不见。如果正确假设,您可以放置​​一个“。” 任何地方。小心“~”,它被 gEdit 等编辑器用作“备份”功能。


at默认情况下未安装。安装

sudo apt-get install at
Run Code Online (Sandbox Code Playgroud)

  • 你在哪里看到cp?*天真地吹口哨* (3认同)
  • 这位先生呢?哦,我是否可以建议您的多个用户也使用自己的文件名 sudoers.d。似乎比编辑 sudoers 好多了。 (2认同)