在linux下面,為了保證服務器安全,通常會記錄所敲命令的歷史記錄,但是記錄為1000條,並且退出重新登錄后,之前的變會沒有了,通過編輯/etc/bashrc文件記錄歷史命令到日志文件下面,並已登錄來源IP,登錄用戶名,登錄時間命名日志文件名字
查看默認的history
編輯/etc/bashrc
編輯/etc/bashrc文件,加入以下內容,也可以放在/etc/profile文件里
# vim /etc/bashrc
USER_IP=$(echo -e "\033[31m\033[1m`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`\033[0m")
IP=$(who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g')
USER=$(whoami)
USER_NAME=`echo -e "\033[36m\033[1m$(whoami) \033[0m"`
HISTFILESIZE=100000
HISTSIZE=4096
HISTTIMEFORMAT="%F %T $USER_IP $USER_NAME "
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
DT=`date "+%Y%m%d_%H%M"`
export HISTFILE="/var/log/history/${LOGNAME}/${DT}_${USER}_${IP}"
chmod 600 /var/log/history/${LOGNAME}/* 2>/dev/null
退出重新登錄再次查看
查看前面保留的歷史記錄日志
[root@salt-master ~]# ll /var/log/history/ 總用量 0 d-wx------. 2 root root 6 5月 22 14:27 root [root@salt-master ~]# ll /var/log/history/root/ 總用量 4 -rw-------. 1 root root 123 5月 22 14:29 20190522_1427_root_192.168.1.123 [root@salt-master ~]# cat /var/log/history/root/20190522_1427_root_192.168.1.123 #1558506461 ls #1558506467 history #1558506519 ll /var/log/history/ #1558506571 ll /var/log/history/root/ #1558506574 exit
