让 Linux 记录所有用户操作记录,这里我们有两点要讲,
第一点而言,对于 Linux 系统而言,我们知道要查看用户的操作记录命令可以用 history 命令查看,但是 history 命令还是比较过于简陋,如下图所示,默认 history 只显示编号,命令,这里就缺少很多我们需要的信息了,比如关键的哪个时间点操作的命令。
[amber@30th-1 ~]$ history |head -10
3 jmap -heap 16875
4 top
5 jamp -heap 32748
6 jmap -heap 32748
7 ps -ef |grep 32748
8 jmap -histo 32748 | head -20
9 ll
10 cd test-serv/
11 ll
12 cd /home/test
这里我们给 history 命令加上时间和日期。然后再来看一下,可以看到多了日期和时间了。
export HISTTIMEFORMAT="%F %T "
[amber@30th-1 ~]$ history |head -10
5 2018-05-10 09:06:26 jamp -heap 32748
6 2018-05-10 09:06:26 jmap -heap 32748
7 2018-05-10 09:06:26 ps -ef |grep 32748
8 2018-05-10 09:06:26 jmap -histo 32748 | head -20
9 2018-05-10 09:06:26 ll
10 2018-05-10 09:06:26 cd test-serv/
11 2018-05-10 09:06:26 ll
12 2018-05-10 09:06:26 cd test1
13 2018-05-10 09:06:26 cd test2
14 2018-05-10 09:06:26 ll
但是这还是不能满足我们的需求,我们还想知道是哪个用户的操作的,这里我们需要这样设置一下
export HISTTIMEFORMAT="%F %T `whoami` "
[amber@30th-1 ~]$ history |head -10
7 2018-05-10 09:06:26 amber ps -ef |grep 32748
8 2018-05-10 09:06:26 amber jmap -histo 32748 | head -20
9 2018-05-10 09:06:26 amber ll
10 2018-05-10 09:06:26 amber cd test-serv/
11 2018-05-10 09:06:26 amber ll
12 2018-05-10 09:06:26 amber cd test1
13 2018-05-10 09:06:26 amber cd test2
14 2018-05-10 09:06:26 amber ll
15 2018-05-10 09:06:26 amber tail -f stdout.log
16 2018-05-10 09:06:26 amber tail -111f stdout.log
上面我们就可以看出是哪个用户操作了,不过这个有点鸡肋,因为显示的肯定当前用户的操作。
如果我还想知道是哪个用户通过哪个 ip 登录操作的,可以这样来一下
export HISTTIMEFORMAT="%F %T `who am i |awk '{print \$1\" \"\$2\" \"\$5}'` "
[root@Amber ~]# history |head -10
32 2018-04-23 17:53:54 root pts/0 (xxx.xxx.xxx.xxx) vim cpu.py
33 2018-04-23 17:54:14 root pts/0 (xxx.xxx.xxx.xxx) ll
34 2018-04-23 17:54:41 root pts/0 (xxx.xxx.xxx.xxx) rm -rf info.txt
35 2018-04-23 17:54:42 root pts/0 (xxx.xxx.xxx.xxx) ll
36 2018-04-23 17:55:03 root pts/0 (xxx.xxx.xxx.xxx) vim cpu.py
37 2018-04-23 21:15:50 root pts/0 (xxx.xxx.xxx.xxx) ll
38 2018-04-23 21:15:51 root pts/0 (xxx.xxx.xxx.xxx) ll
39 2018-04-23 21:15:55 root pts/0 (xxx.xxx.xxx.xxx) cd mail/
40 2018-04-23 21:15:56 root pts/0 (xxx.xxx.xxx.xxx) ls
41 2018-04-23 21:16:08 root pts/0 (xxx.xxx.xxx.xxx) vim check_cpu.sh
上面都是临时设置的,退出系统后,下次不生效了。
根据需求,有三种不同的设置环境变量的方法。
1、临时设置当前用户的环境变量
2、永久设置当前/其他用户的环境变量
3、永久设置所有用户的环境变量
第一种就不说了,就上面介绍的,
export HISTTIMEFORMAT="%F %T `who am i |awk '{print \$1\" \"\$2\" \"\$5}'` "
第二种,我们需要在当前用户的.bashrc 或者.bash_profile 里面追加个命令就可以了,最后 source 一下就可以了
# echo 'HISTTIMEFORMAT="%F %T `who am i |awk '{print \$1\" \"\$2\" \"\$5}'` "' >> ~/.bashrc
或
# echo 'HISTTIMEFORMAT="%F %T `who am i |awk '{print \$1\" \"\$2\" \"\$5}'` "' >> ~/.bash_profile
# source ~/.bashrc
或
# source ~/.bash_profile
第三种就是需要将这个设置添加到/etc/profile 里面了
将 HISTTIMEFORMAT 变量加入 etc/profile 文件中,让它对所有用户永久生效。
# echo 'HISTTIMEFORMAT="%F %T `who am i |awk '{print \$1\" \"\$2\" \"\$5}'` "' >> /etc/profile
注意:如果原先有 HISTTIMEFORMAT 需要注释掉原先的。
运行下面命令来让文件中的修改生效。
# source /etc/profile
接下来我们讲一下第二点,第二点要讲的原因是因为第一点 history 命令是有条数限制的,默认 history 只有 1000 条记录,我们用echo $HISTSIZE可以查看具体条数切用户的操作记录都保存在~/.bash_histroy 文件里面,我们虽然可以通过增加 history 的条数,我们可以在/etc/profile 或者.bash_profile 或者.bashrc 里面增加 HISTSIZE=10000,然后 source 一下,但是这都是不好的方法,因为数量还是有限,另外就是只能记录当前登录的用户操作记录。
所以这里我们第二点讲的就是如何记录所有用户的操作记录,当然也要包括登录信息包括登录用户和 ip 已经日期时间。这里我们就叫它开启 linux 系统用户命令日志审计功能。
1.创建用户审计文件存放目录和审计日志文件 ;
mkdir -p /var/log/usermonitor/
2.创建用户审计日志文件;
echo usermonitor >/var/log/usermonitor/usermonitor.log
3.将日志文件所有者赋予一个最低权限的用户;
chown nobody:nobody /var/log/usermonitor/usermonitor.log
4.给该日志文件赋予所有人的写权限;
chmod 002 /var/log/usermonitor/usermonitor.log
5.设置文件权限,使所有用户对该文件只有追加权限 ;
chattr +a /var/log/usermonitor/usermonitor.log
6.编辑 vim /etc/profile 文件,添加如下脚本命令;
export HISTORY_FILE=/var/log/usermonitor/usermonitor.log
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.使配置生效
source /etc/profile
8.查看效果
[root@Amber usermonitor]# tailf usermonitor.log
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
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
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
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
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
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
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
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
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/
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 是否设置冲突了!