歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 按登錄IP記錄Linux所有用戶操作日志

按登錄IP記錄Linux所有用戶操作日志

日期:2017/3/2 9:56:04   编辑:關於Linux

Linux用戶操作記錄一般通過命令history來查看歷史記錄,但是如果因為某人誤操作了刪除了重要的數據,這種情況下history命令就不會有什麼作用了。以下方法可以實現通過記錄登陸IP地址和所有用戶登錄所操作的日志記錄!

在/etc/profile配置文件的末尾加入以下腳本代碼就可以實現,下面腳本是我網上找來的,原作者不知。但原腳本的時間變量有錯誤,不能記錄時間,本人測試發現並檢查修正:

PS1="`whoami`@`hostname`:"'[$PWD]'
history
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
if [ "$USER_IP" = "" ]
then
USER_IP=`hostname`
fi
if [ ! -d /tmp/history ]
then
mkdir /tmp/history
chmod 777 /tmp/history
fi
if [ ! -d /tmp/history/${LOGNAME} ]
then
mkdir /tmp/history/${LOGNAME}
chmod 300 /tmp/history/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date +"%Y%m%d_%H%M%S"`
export HISTFILE="/tmp/history/${LOGNAME}/${USER_IP} history.$DT"
chmod 600 /tmp/history/${LOGNAME}/*history* 2>/dev/null

通過上面的代碼可以看出來,在系統的/tmp新建個history目錄(這個目錄可以自定義),在目錄中記錄了所有的登陸過系統的用戶和IP地址,這也是監測系統安全的方法之一

Copyright © Linux教程網 All Rights Reserved