编辑/etc/profile文件,在文件末尾加入下面代码:
[root@iZ23nn1p4mjZ root]# vi /etc/profile
historyUSER=`whoami`USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`if [ "$USER_IP" = "" ]; thenUSER_IP=`hostname`fiif [ ! -d /var/log/history ]; thenmkdir /var/log/historychmod 777 /var/log/historyfiif [ ! -d /var/log/history/${LOGNAME} ]; thenmkdir /var/log/history/${LOGNAME}chmod 300 /var/log/history/${LOGNAME}fiexport HISTSIZE=4096DT=`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 注释: /var/log/history这是记录日志的存放位置,可以自定义。 在/var/log/history下会以每个用户为名新建一个文件夹
[root@iZ23nn1p4mjZ history]# ll
total 4d-wx------ 2 root root 4096 Jan 9 07:29 root[root@iZ23nn1p4mjZ root]# ll
total 4-rw------- 1 root root 54 Jan 9 07:29 root@101.69.255.190_20170109_07:29:05