有什麼辦法可以讓使用者只執行2天的指令

有什麼辦法可以讓使用者只執行2天的指令

如何授予 sudo 命令特定時間的權限?有沒有辦法授予僅在 sudoers 檔案中執行特定命令 2 天的權限?

答案1

sudoers 檔案不支援基於時間的限制,但有一個簡單的方法。建立一個包含您的變更的檔案/etc/sudoers.d/(with 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

相關內容