通過腳本代碼實現記錄所有用戶的登錄操作日志,防止出現安全事件后無據可查。
運行 [root@xxx /]# 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運行 [root@xxx /]# source /etc/profile
加載配置生效。
注意: /var/log/history 是記錄日志的存放位置,可以自定義。
通過上述步驟,可以在 /var/log/history 目錄下以每個用戶為名新建一個文件夾,每次用戶退出后都會產生以用戶名、登錄IP、時間的日志文件,包含此用戶本次的所有操作(root用戶除外)。