暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

运维记——优化History并开启linux系统用户命令日志审计功能

IT敢客 2018-08-08
422

        让 Linux 记录所有用户操作记录,这里我们有两点要讲,
        第一点而言,对于 Linux 系统而言,我们知道要查看用户的操作记录命令可以用 history 命令查看,但是 history 命令还是比较过于简陋,如下图所示,默认 history 只显示编号,命令,这里就缺少很多我们需要的信息了,比如关键的哪个时间点操作的命令。

  1. [amber@30th-1 ~]$ history |head -10

  2.     3  jmap -heap 16875

  3.     4  top

  4.     5  jamp -heap 32748

  5.     6  jmap -heap 32748

  6.     7  ps -ef |grep 32748

  7.     8  jmap -histo 32748 | head -20

  8.     9  ll

  9.    10  cd test-serv/

  10.    11  ll

  11.    12  cd /home/test

这里我们给 history 命令加上时间和日期。然后再来看一下,可以看到多了日期和时间了。

  1. export HISTTIMEFORMAT="%F %T "

  1. [amber@30th-1 ~]$ history |head -10             

  2.     5  2018-05-10 09:06:26 jamp -heap 32748

  3.     6  2018-05-10 09:06:26 jmap -heap 32748

  4.     7  2018-05-10 09:06:26 ps -ef |grep 32748

  5.     8  2018-05-10 09:06:26 jmap -histo 32748 | head -20

  6.     9  2018-05-10 09:06:26 ll

  7.    10  2018-05-10 09:06:26 cd test-serv/

  8.    11  2018-05-10 09:06:26 ll

  9.    12  2018-05-10 09:06:26 cd test1

  10.    13  2018-05-10 09:06:26 cd test2

  11.    14  2018-05-10 09:06:26 ll

但是这还是不能满足我们的需求,我们还想知道是哪个用户的操作的,这里我们需要这样设置一下

  1. export HISTTIMEFORMAT="%F %T `whoami` "

  1. [amber@30th-1 ~]$ history |head -10                      

  2.     7  2018-05-10 09:06:26 amber ps -ef |grep 32748

  3.     8  2018-05-10 09:06:26 amber jmap -histo 32748 | head -20

  4.     9  2018-05-10 09:06:26 amber ll

  5.    10  2018-05-10 09:06:26 amber cd test-serv/

  6.    11  2018-05-10 09:06:26 amber ll

  7.    12  2018-05-10 09:06:26 amber cd test1

  8.    13  2018-05-10 09:06:26 amber cd test2

  9.    14  2018-05-10 09:06:26 amber ll

  10.    15  2018-05-10 09:06:26 amber tail -f stdout.log 

  11.    16  2018-05-10 09:06:26 amber tail -111f stdout.log

上面我们就可以看出是哪个用户操作了,不过这个有点鸡肋,因为显示的肯定当前用户的操作。
如果我还想知道是哪个用户通过哪个 ip 登录操作的,可以这样来一下

  1. export HISTTIMEFORMAT="%F %T `who am i |awk '{print \$1\" \"\$2\" \"\$5}'` "

  1. [root@Amber ~]# history |head -10

  2.    32  2018-04-23 17:53:54 root pts/0 (xxx.xxx.xxx.xxx) vim cpu.py 

  3.    33  2018-04-23 17:54:14 root pts/0 (xxx.xxx.xxx.xxx) ll

  4.    34  2018-04-23 17:54:41 root pts/0 (xxx.xxx.xxx.xxx) rm -rf info.txt 

  5.    35  2018-04-23 17:54:42 root pts/0 (xxx.xxx.xxx.xxx) ll

  6.    36  2018-04-23 17:55:03 root pts/0 (xxx.xxx.xxx.xxx) vim cpu.py 

  7.    37  2018-04-23 21:15:50 root pts/0 (xxx.xxx.xxx.xxx) ll

  8.    38  2018-04-23 21:15:51 root pts/0 (xxx.xxx.xxx.xxx) ll

  9.    39  2018-04-23 21:15:55 root pts/0 (xxx.xxx.xxx.xxx) cd mail/

  10.    40  2018-04-23 21:15:56 root pts/0 (xxx.xxx.xxx.xxx) ls

  11.    41  2018-04-23 21:16:08 root pts/0 (xxx.xxx.xxx.xxx) vim check_cpu.sh

上面都是临时设置的,退出系统后,下次不生效了。

根据需求,有三种不同的设置环境变量的方法。

1、临时设置当前用户的环境变量

2、永久设置当前/其他用户的环境变量

3、永久设置所有用户的环境变量

第一种就不说了,就上面介绍的,

  1. export HISTTIMEFORMAT="%F %T `who am i |awk '{print \$1\" \"\$2\" \"\$5}'` "

第二种,我们需要在当前用户的.bashrc 或者.bash_profile 里面追加个命令就可以了,最后 source 一下就可以了

  1. # echo 'HISTTIMEFORMAT="%F %T `who am i |awk '{print \$1\" \"\$2\" \"\$5}'` "' >> ~/.bashrc

  2. # echo 'HISTTIMEFORMAT="%F %T `who am i |awk '{print \$1\" \"\$2\" \"\$5}'` "' >> ~/.bash_profile

  1. # source ~/.bashrc

  2. # source ~/.bash_profile

第三种就是需要将这个设置添加到/etc/profile 里面了

将 HISTTIMEFORMAT 变量加入 etc/profile 文件中,让它对所有用户永久生效。

  1. # echo 'HISTTIMEFORMAT="%F %T `who am i |awk '{print \$1\" \"\$2\" \"\$5}'` "' >> /etc/profile

注意:如果原先有 HISTTIMEFORMAT 需要注释掉原先的。
运行下面命令来让文件中的修改生效。

  1. # source /etc/profile

接下来我们讲一下第二点,第二点要讲的原因是因为第一点 history 命令是有条数限制的,默认 history 只有 1000 条记录,我们用echo $HISTSIZE可以查看具体条数切用户的操作记录都保存在~/.bash_histroy 文件里面,我们虽然可以通过增加 history 的条数,我们可以在/etc/profile 或者.bash_profile 或者.bashrc 里面增加 HISTSIZE=10000,然后 source 一下,但是这都是不好的方法,因为数量还是有限,另外就是只能记录当前登录的用户操作记录。

所以这里我们第二点讲的就是如何记录所有用户的操作记录,当然也要包括登录信息包括登录用户和 ip 已经日期时间。这里我们就叫它开启 linux 系统用户命令日志审计功能。

1.创建用户审计文件存放目录和审计日志文件 ; 

  1. mkdir -/var/log/usermonitor/

2.创建用户审计日志文件;

  1. echo usermonitor >/var/log/usermonitor/usermonitor.log

3.将日志文件所有者赋予一个最低权限的用户;

  1. chown nobody:nobody /var/log/usermonitor/usermonitor.log

4.给该日志文件赋予所有人的写权限; 

  1. chmod 002 /var/log/usermonitor/usermonitor.log

5.设置文件权限,使所有用户对该文件只有追加权限 ;

  1. chattr +/var/log/usermonitor/usermonitor.log

6.编辑 vim /etc/profile 文件,添加如下脚本命令;

  1. export HISTORY_FILE=/var/log/usermonitor/usermonitor.log

  2. export PROMPT_COMMAND='{ date "+%y-%m-%d %T ##### $(who am i |awk "{print \$1\" \"\$2\" \"\$5}")  #### $(whoami)  #### $(history 1 | { read x cmd; echo "$cmd"; })"; } >>$HISTORY_FILE'

7.使配置生效

  1. source  /etc/profile

8.查看效果

  1. [root@Amber usermonitor]# tailf usermonitor.log 

  2. 18-05-10 09:57:01 ##### root pts/0 (xxx.xxx.xxx.xxx)  #### root  #### 2018-05-10 09:57:00 root pts/0 (xxx.xxx.xxx.xxx) source  /etc/profile

  3. 18-05-10 09:57:01 ##### root pts/0 (xxx.xxx.xxx.xxx)  #### root  #### 2018-05-10 09:57:00 root pts/0 (xxx.xxx.xxx.xxx) source  /etc/profile

  4. 18-05-10 09:57:01 ##### root pts/0 (xxx.xxx.xxx.xxx)  #### root  #### 2018-05-10 09:57:00 root pts/0 (xxx.xxx.xxx.xxx) source  /etc/profile

  5. 18-05-10 09:57:02 ##### root pts/0 (xxx.xxx.xxx.xxx)  #### root  #### 2018-05-10 09:57:02 root pts/0 (xxx.xxx.xxx.xxx) pwd

  6. 18-05-10 09:57:03 ##### root pts/0 (xxx.xxx.xxx.xxx)  #### root  #### 2018-05-10 09:57:03 root pts/0 (xxx.xxx.xxx.xxx) ll

  7. 18-05-10 09:57:03 ##### root pts/0 (xxx.xxx.xxx.xxx)  #### root  #### 2018-05-10 09:57:03 root pts/0 (xxx.xxx.xxx.xxx) ll

  8. 18-05-10 09:57:03 ##### root pts/0 (xxx.xxx.xxx.xxx)  #### root  #### 2018-05-10 09:57:03 root pts/0 (xxx.xxx.xxx.xxx) ll

  9. 18-05-10 09:57:03 ##### root pts/0 (xxx.xxx.xxx.xxx)  #### root  #### 2018-05-10 09:57:03 root pts/0 (xxx.xxx.xxx.xxx) ll

  10. 18-05-10 09:57:07 ##### root pts/0 (xxx.xxx.xxx.xxx)  #### root  #### 2018-05-10 09:57:07 root pts/0 (xxx.xxx.xxx.xxx) cd /var/log/usermonitor/

  11. 18-05-10 09:57:07 ##### root pts/0 (xxx.xxx.xxx.xxx)  #### root  #### 2018-05-10 09:57:07 root pts/0 (xxx.xxx.xxx.xxx) ll

审计时查看/var/log/usermonitor/usermonitor.log 文件即可,它会记录登上服务器所有用户使用的命令。我们也还可以对这个文件进行打包分割操作等等,随便写个脚本就行了。

通过以上 2 点设置后,我们可以方便的记录用户的所有操作信息了,少数的最近操作可以直接 history 查看,多数的就查看/var/log/usermonitor/usermonitor.log 文件了。

注意点:如果以上设置不生效,也就是登录以后 echo $HISTTIMEFORMAT 看一下是否不生效,请一步步排查是否.bash_profile 或者.bashrc 或者/etc/profile 或者/etc/bashrc 里面的 HISTTIMEFORMAT 是否设置冲突了!


文章转载自IT敢客,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论