一般我们可以用history命令来查看用户的操作记录,但是这个命令不能记录是哪个用户登录操作的,也不能记录详细的操作时间,且不完整;所以误操作而造成重要的数据丢失,就很难查到是谁操作的。
在这里我们通过脚本代码来实现记录所以用户的登录操作日志:
编辑/etc/profile文件,在文件末尾加入下面代码:
[root@telecom225 /]# 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这是记录日志的存放位置,可以自定义。
让/etc/profile文件修改后立即生效 ,可以使用如下命令:
. /etc/profile
注意: . 和 /etc/profile 有空格
本文介绍了如何通过脚本代码实现Linux系统中用户登录操作的日志记录,包括用户身份、登录IP和操作时间,以防止重要数据丢失时无法追溯。在/etc/profile文件中添加特定代码,创建历史日志目录并设置权限,确保每个用户的历史操作在退出时被详细记录在以用户名命名的文件中。
6062

被折叠的 条评论
为什么被折叠?



