有时候我们需要记录Linux用户的操作记录用于审计,因此就要避免用户可以自行清除操作日志,一个简单的方式是使用script功能。

首先在用户的profile文件中开启记录功能:

[banping@linux ~]$ cd /home/banping/
[banping@linux ~]$ vi .bash_profile
# write log
exec /usr/bin/script -a -f -q /tmp/test/script-`date +%Y%m%d%k%M`.lst

这行脚本的意思是在/tmp/test目录下以时间为文件名来记录操作信息,由于是写在了.bash_profile文件中,用户登入到Linux系统的时候就会触发执行。

然后我们在/tmp下建立test目录存放操作日志信息即可:

[banping@linux tmp]# mkdir test

这样就实现了记录的功能,而要防止用户自行修改,我们可以设置这些文件只能被附加,不能被修改或删除:

[root@linux banping]# chattr +a .bash_profile

[root@linux tmp]# chattr +a -R test

这样登录用户就无法修改这些信息了,以下是一个简单的测试:

[root@tomcat tmp]# cd test
[root@tomcat test]# touch 1.txt
[root@tomcat test]# rm 1.txt
rm: remove regular empty file `1.txt'? y
rm: cannot remove `1.txt': Operation not permitted
[root@tomcat test]# cd ..
[root@tomcat tmp]# chattr -a -R test
[root@tomcat tmp]# cd test
[root@tomcat test]# rm 1.txt
rm: remove regular empty file `1.txt'? y

结合登录文件的安全保存和防火墙功能,可以实现一个简单而完整的安全审计解决方案。

 

8 Responses to 使用script记录Linux用户的操作记录

  1. Yousri 说道:

    这个方法不错

  2. Yousri 说道:

    不过你这样的话普通用户对于/tmp/test/能有读写权限?

  3. banping 说道:

    赋权限给普通用户就行吧,用被监控的用户建立这个目录是没问题,有空详细测试一下。

  4. Yousri 说道:

    中午简单的测试下 确实可行

  5. clement 说道:

    非常不错。。。谢谢了。。。刚需要

  6. banping 说道:

    to clement 不必客气

  7. 云麟 说道:

    我记得这货会把不可见字符也记录了

  8. wss8848 说道:

    我是用了这条语句怎们登陆进去一条命令也写不了了?

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>