MENU

Linux 查看登录日志-永久记录

2019 年 10 月 21 日 • 阅读: 2604 • 安全检查

近期被坑了一波,脑袋嗡嗡的,日志什么时候都不能少!

脚本生成所有登录用户的操作历史

通过在/etc/profile里面加入以下代码就可以实现:

PS1="`whoami`@`hostname`:"'[$PWD]'
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 /tmp/dbasky ]
then
mkdir /tmp/dbasky
chmod 777 /tmp/dbasky
fi
if [ ! -d /tmp/dbasky/${LOGNAME} ]
then
mkdir /tmp/dbasky/${LOGNAME}
chmod 300 /tmp/dbasky/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date "+%Y-%m-%d_%H:%M:%S"`
export HISTFILE="/tmp/dbasky/${LOGNAME}/${USER_IP}-dbasky.$DT"
chmod 600 /tmp/dbasky/${LOGNAME}/*dbasky* 2>/dev/null
source /etc/profile 使用脚本生效

面脚本在系统的/tmp新建个dbasky目录,记录所有登陆过系统的用户和IP地址(文件名),每当用户登录/退出会创建相应的文件,该文件保存这段用户登录时期内操作历史,可以用这个方法来监测系统的安全性。

root@zsc6:[/tmp/dbasky/root]ls
10.1.80.47 dbasky.2013-10-24_12:53:08
root@zsc6:[/tmp/dbasky/root]cat 10.1.80.47 dbasky.2013-10-24_12:53:08

参考 https://www.landui.com/help/show-2714.html

兼总条贯 知至知终

无标签