歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux下記錄所有用戶操作的腳本

Linux下記錄所有用戶操作的腳本

日期:2017/2/28 14:45:06   编辑:Linux教程

這個腳本是在網上找到的,稍微做了一些修改,可以實現在Linux下所有用戶,不管是遠程還是本地登陸,在本機的所有操作都會記錄下來,並生成包含“用戶/IP/時間”的文件存放在指定位置,方便管理員以後查看不同用戶都在服務器上干了些什麼!

目前這個代碼只實現了記錄用戶的操作命令,但是像vi編輯只會記錄vi這條命令,但是在文件裡所修改的內容無法記錄。。。有時間再研究一下!

將下面的代碼追加到/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

Copyright © Linux教程網 All Rights Reserved