Linux下记录所有用户的登录和操作日志

一般我们可以用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 有空格

在/var/log/history下会以每个用户为名新建一个文件夹,
[root@telecom225 /]# cd /var/log/history/

[root@telecom225 history]# ll
total 20
d-wx------ 2 test dennishan 4096 Jul 4 12:06 test
d-wx------ 2 jerryhe jerryhe 4096 Jul 4 14:12 jerryhe

每次用户退出后都会产生以用户名、登录IP、时间的文件,里面包含此用户本次的所以操作

[root@telecom225 history]# cd jerryhe/
[root@telecom225 jerryhe]# ls
jerryhe@180.173.188.252_20130626_14:59:51
jerryhe@180.173.188.252_20130628_14:40:26
jerryhe@180.173.188.252_20130703_09:44:21
jerryhe@180.173.188.252_20130703_11:18:40
jerryhe@180.173.188.252_20130703_11:21:11
jerryhe@180.173.188.252_20130703_14:21:49
jerryhe@180.173.188.252_20130703_14:30:48
jerryhe@180.173.188.252_20130704_13:37:19
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值