linux(centos 6)下记录所有用户的操作以及ip、时间

时间:2023-03-09 06:09:37
linux(centos 6)下记录所有用户的操作以及ip、时间

编辑/etc/profile文件,在文件末尾加入下面代码:

[root@iZ23nn1p4mjZ root]# vi /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 注释: /var/log/history这是记录日志的存放位置,可以自定义。
在/var/log/history下会以每个用户为名新建一个文件夹

[root@iZ23nn1p4mjZ history]# ll
total 4
d-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