Centos系统添加系统用户操作记录审计

前端之家收集整理的这篇文章主要介绍了Centos系统添加系统用户操作记录审计前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

有时候我们需要对线上用户操作记录进行历史记录待出现问题追究责任人,,但Linux系统自带的history命令用户有自行删除权限,那怎么设置可以让用户的操作记录实时记录,并保证普通用户无权删除呢?
1.创建系统用户shell命令行操作记录日志存放位置

  1. mkdir -p /var/log/userlogin/records/
  2. chmod 777 /var/log/userlogin/records/
  3. chmod +t /var/log/userlogin/records/

2.vim /etc/profile 在最后添加下面的代码

  1. if [ ! -d /var/log/userlogin/records/${LOGNAME} ]
  2. then
  3. mkdir -p /var/log/userlogin/records/${LOGNAME}
  4. chmod 300 /var/log/userlogin/records/${LOGNAME}
  5. fi
  6.  
  7. export HISTORY_FILE="/var/log/userlogin/records/${LOGNAME}/bash_history"
  8.  
  9. export PROMPT_COMMAND='{ date "+%Y-%m-%d %T ##### $(who am i |awk "{print \$1\" \"\$2\" \"\$5}") #### $(history 1 | { read x cmd; echo "$cmd"; })"; } >>$HISTORY_FILE'
  10. source /etc/profile

3.测试验证

  1. [root@master01 local]# source /etc/profile
  2. [root@master01 local]# cd /var/log/userlogin/records/
  3. [root@master01 records]# ls
  4. root
  5. [root@master01 records]# cd root/
  6. [root@master01 root]# ls
  7. bash_history
  8. [root@master01 root]# cat bash_history
  9. 2018-06-04 03:41:30 ##### root pts/0 (10.0.0.1) #### source /etc/profile
  10. 2018-06-04 03:41:40 ##### root pts/0 (10.0.0.1) #### cd /var/log/userlogin/records/
  11. 2018-06-04 03:41:41 ##### root pts/0 (10.0.0.1) #### ls
  12. 2018-06-04 03:41:43 ##### root pts/0 (10.0.0.1) #### cd root/
  13. 2018-06-04 03:41:43 ##### root pts/0 (10.0.0.1) #### ls
  14. [root@master01 root]# su - postgres
  15. [postgres@master01 ~]$ echo 12345 >>test001
  16. [postgres@master01 ~]$ ls
  17. pg_dump.sh test001
  18. [postgres@master01 ~]$ cat test001
  19. 12345
  20. [postgres@master01 ~]$ logout
  21. [root@master01 root]# pwd
  22. /var/log/userlogin/records/root
  23. [root@master01 records]# ls
  24. postgres root
  25. [root@master01 records]# cd postgres/
  26. [root@master01 postgres]# ls
  27. bash_history
  28. [root@master01 postgres]# cat bash_history
  29. 2018-06-04 03:42:17 ##### root pts/0 (10.0.0.1) #### cd ..
  30. 2018-06-04 03:42:18 ##### root pts/0 (10.0.0.1) #### ls
  31. 2018-06-04 03:42:29 ##### root pts/0 (10.0.0.1) #### echo 12345 >>test001
  32. 2018-06-04 03:42:31 ##### root pts/0 (10.0.0.1) #### ls
  33. 2018-06-04 03:42:36 ##### root pts/0 (10.0.0.1) #### cat test001

猜你在找的CentOS相关文章