
特定の時間に 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日後の午後2時にファイルが移動されます。at
マニュアルいくつかのオプションがあります (時間、日、週、月、年、next などのキーワードを使用したり、期間を追加/減算したりできます)。オプションを使用していくつかのテストを行い、理解していることを確認します (考慮すべき点: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
そしてそのユーザーを激怒させます (今ならこんなこともできるんだ)。
READMEは次の通りです/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