放到这个文件底下就行
[root@telecom225 /]# 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
PS:刷新让命令生效
[root@VM-16-2-centos ~]# source /etc/profile
日志目录:
[root@VM-16-2-centos ~]# cd /var/log/history/
[root@VM-16-2-centos history]# ls
root
记录的日志(都是以登录IP和时间命名):
[root@VM-16-2-centos root]# ls
root@114.242.26.29_20211229_08:58:12 root@114.242.26.29_20211229_08:59:35
[root@VM-16-2-centos root]# tail -f root@114.242.26.29_20211229_08\:59\:35