歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 通過登陸IP記錄Linux所有用戶登錄所操作的日志

通過登陸IP記錄Linux所有用戶登錄所操作的日志

日期:2017/2/28 15:31:05   编辑:Linux教程

對於Linux用戶操作記錄一般通過命令history來查看歷史記錄,但是如果在由於誤操作而刪除了重要的數據的情況下,history命令就不會有什麼作用了。那麼依然要存有歷史操作記錄應該如何來實現呢?

其實我們可以通過登陸IP地址來記錄所有用戶登錄所操作的歷史操作!具體操作就是在/etc/profile配置文件的末尾加入以下腳本代碼來實現:

[root@server ~]# vi history.sh
#!/bin/bash
#
# ScriptName:history.sh
# Description: fits CentOS 5.x and RHEL 5.x series
# Version:1.0
#
# Author:300second
# QQ:84287030
# Date:21:10 2012-10-10
#
cat >>/etc/profile<< EOF

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 /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}@${USER_IP}_history.$DT"

chmod 600 /tmp/history/${LOGNAME}/*history* 2>/dev/null

EOF

source /etc/profile

Copyright © Linux教程網 All Rights Reserved