
我想在date -s <string>
使用命令時運行 shell 腳本。例如,我想透過在 shell 腳本中執行以下命令將命令記錄到檔案 /tmp/user.log
logger -p user.notice "date -s command executed" -f /tmp/user.log
date -s <string>
在 shell 上執行時如何執行 shell 腳本?
為了使其更通用,我想當其他人在我的系統上發出特定的 linux 命令時執行我的 shell 腳本。這個怎麼做?
答案1
重新表達這個問題,您想知道何時有人嘗試設定係統時間。這正是audit
子系統的用途…它允許您審核特定係統呼叫的執行。在這種情況下,您想知道何時有人呼叫可以更改系統時間的各種系統呼叫中的任何一個。透過使用該audit
子系統,您可以獲得一個無論有人呼叫/bin/date
還是他們自己本地建置的命令版本都可以工作的解決方案。
請參閱auditd(8)
和audit.rules(7)
有關規則語法的完整描述,以及審核時間更改操作的範例,您可以在範例文件中找到“時間更改” nispom.rules
。您可以在本機系統上找到它,也可以在這裡找到它:
有關audit
子系統的更多資訊(而且文件有點難以取得):
答案2
您無法輕易阻止使用者執行他自己的程式版本,但如果您假設該使用者沒有惡意,則很容易:
- 建立一個包裝器腳本來記錄程式然後運行它(使用原始參數)
- 確保包裝器替換使用者路徑中的原始程式。
您可以使用別名讓使用者使用包裝器,也可以簡單地移動/重新命名原始程式並將包裝器放在原始位置。
如果您只想記錄某些執行,請在包裝器腳本中使用正規表示式。
答案3
非常感謝serverfault
朋友們的解答。
我想在你們的幫助下我已經找到了問題的答案。但是如果有任何與之相關的錯誤或需要改進的話,請大家幫幫我?我來啦。
這些是我所做的事情。
我)。datewrapper.sh
使用以下程式碼在 /etc 中建立了一個腳本
#! /bin/bash # wrapper script for the date command. # whenever the date -s command is issued, it will be logged. # executing the date command first with parameter list if any date $@ # check whether the date command executed successfully if [ "$?" == "0" ] ; then for param in $@ ; do # if "-s" option is used, log it if [ "$param" == "-s" ] ; then # user.notice logs the message to /tmp/log/user.log # as per the commands in /etc/syslog-ng/syslog-ng.conf logger -p user.notice "New date set" break fi done fi exit 0
ii). chmod a+x /etc/datewrapper.sh ;別名 date='/etc/datewrapper.sh'
三)。日期
世界標準時間 2010 年 12 月 21 日星期二 21:51:01
四)。日期-s“21:53:05”
世界標準時間 2010 年 12 月 21 日星期二 21:53:05
五)。我檢查了/tmp/log/user.log。它顯示訊息
12 月 21 日 21:53:05 localhost root:新日期設置
所以結果是,每當使用者發出date
命令時,我的腳本都會被執行,每當他發出帶有選項的命令時-s
,它都會登入/tmp/log/user.log