监控服务器登录用户的操作脚本(linux下shell.sh)
linux下記錄所有用戶的登錄和操作日志
在?/etc/profile文件末尾加入如下代碼:
#vim /etc/profile
history
USER=`whoami`
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 /var/log/history ]; then
mkdir /var/log/history
chmod 777 /var/log/history
fi
if [ ! -d /var/log/history/${LOGNAME} ]; then
mkdir /var/log/history/${LOGNAME}
chmod 300 /var/log/history/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date +"%Y%m%d_%H:%M:%S"`
export HISTFILE="/var/log/history/${LOGNAME}/${USER}@${USER_IP}_$DT"
chmod 600 /var/log/history/${LOGNAME}/*history* 2>/dev/null
如下圖
#source /etc/profile? ? ? ? ? ? //使其立即生效
查看/var/log/history腳本會為每個用戶自動創建一個以用戶名命名的文件夾,每次用戶退出后都會產生以用戶名、登錄IP、時間的文件,里面包含此用戶本次的所以操作
如下圖
########################寫完手工###################################################################
?
總結
以上是生活随笔為你收集整理的监控服务器登录用户的操作脚本(linux下shell.sh)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信公众号的运营
- 下一篇: Linux上日志的切割