linux下用户操做记录审计环境的部署记录

 

一般,咱们运维管理人员须要知道一台服务器上有哪些用户登陆过,在服务器上执行了哪些命令,干了哪些事情,这就要求记录服务器上所用登陆用户的操做信息,这对于安全维护来讲颇有必要。废话很少说了,下面直接记录作法:node

1)查看及管理当前登陆用户
使用w命令查看当前登陆用户正在使用的进程信息,w命令用于显示已经登陆系统的用户的名称,以及它们正在作的事。该命令所使用的信息来源于/var/run/utmp文件。w命令输出的信息包括:
-> 用户名称
-> 用户的机器名称或tty号
-> 远程主机地址
-> 用户登陆系统的时间
-> 空闲时间(做用不大)
-> 附加到tty(终端)的进程所用的时间(JCPU时间)
-> 当前进程所用时间(PCPU时间)
-> 用户当前正在使用的命令
 
[root@test ~]# w
 13:54:14 up 2 days,  2:53,  4 users,  load average: 0.02, 0.02, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                Mon11    3:44m  0.11s  0.06s -bash
test     pts/0    172.16.255.202   10:11    1:48m  0.11s  0.03s vim userinfo.text
nanli    pts/3    172.16.255.196   12:01    1:52m  0.00s  0.00s -bash
work     pts/4    172.116.55.13   12:08    0.00s  0.02s  0.00s w
 
此外,可使用"who am i"查看使用该命令的用户及进程,使用who查看全部登陆用户进程信息,这些查看命令大同小异;
 
二、使用pkill强制退出登陆的用户
 
使用pkill能够结束当前登陆用户的进程,从而强制退出用户登陆,具体使用能够结合w命令;
-> 使用w查看当前登陆的用户,注意TTY所示登陆进程终端号
-> 使用"pkill –9 -t TTY终端号" 结束该进程所对应用户登陆(可根据FROM的IP地址或主机号来判断)
[root@test ~]# pkill -9 pts/4
[root@test ~]# w
 13:59:23 up 2 days,  2:56,  4 users,  load average: 0.02, 0.02, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                Mon11    3:44m  0.11s  0.06s -bash
test     pts/0    172.16.255.202   10:11    1:48m  0.11s  0.03s vim userinfo.text
 
2)查看全部登陆用户的操做历史
在Linux系统的环境下,不论是root用户仍是其它的用户只有登录系统后用进入操做咱们均可以经过命令history来查看历史记录。但是假如一台服务器多人登录,一天由于某人误操做了删除
了重要的数据。这时候经过查看历史记录(命令:history)是没有什么意义了(由于history只针对登陆用户下执行有效,即便root用户也没法获得其它用户histotry历史)。那有没有什么
办法实现经过记录登录后的IP地址和某用户名所操做的历史记录呢?答案确定是有的!
 
经过在/etc/profile文件底部添加如下代码就能够实现:
[root@test ~]# cat /etc/profile
......
#记录每一个用户的操做信息
export PS1='[\u@\h \w]# '
history
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 /opt/history ]
then
mkdir /opt/history
chmod 777 /opt/history
fi
if [ ! -d /opt/history/${LOGNAME} ]
then
mkdir /opt/history/${LOGNAME}
chmod 300 /opt/history/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date +"%Y%m%d_%H%M%S"`
export HISTFILE="/opt/history/${LOGNAME}/${USER_IP} history.$DT"
chmod 600 /opt/history/${LOGNAME}/*history* 2>/dev/null
 
[root@test ~]# source /etc/profile     #使得上面配置生效
 
上面脚本在系统的/opt下新建个history目录,记录全部登录过系统的用户和IP地址(文件名),每当用户登陆/退出会建立相应的文件,该文件保存这段用户登陆时期内操做历史,能够用这个
方法来监测系统的安全性。
------------------------------------------------------------------------------------------------------------------------------------------
上面的显示跟默认的linux终端显示不太习惯。如今要求终端里切换路径后,只显示当前的简介路径,不显示所有路径,而且后面带上#或$符号,那么只须要将上面的第一行
PS1参数后面的设置以下:
1)只显示当前简介路径,不显示全路径,显示#号。注意下面在"#"符号后面空出一格,这样终端的"#"符号跟命令之间就有了一格的距离,习惯而已!
PS1="[\u@\h \W]# "
2)只显示当前简介路径,不显示全路径,显示$号。注意下面的"$"符号后面空出一格。
PS1="[\u@\h \W]\$ "

这里我在脚本选择第(1)种带"#"号显示(也能够两种都不选,直接将第一行PS1的设置给去掉,这样就是默认的了终端显示.线上使用的话,推荐使用这种默认的),生效后的终
端显示内容和linux默认显示的同样。即export PS1="[\u@\h \W]# "
------------------------------------------------------------------------------------------------------------------------------------------

好比:使用nanli帐号操做:
[root@test ~]# su - nanbo
[nanbo@test ~]# echo "hahahahah"
hahahahah
[nanbo@test ~]# cd /usr/local/
[nanbo@test local]# ls
bin  etc  games  include  lib  lib64  libexec  libzip  man  openssl  sbin  share  src
[nanbo@test local]# cat /etc/passwd
[nanbo@test local]# ls /var/log/messages
/var/log/messages

而后退出nanli帐号,查看用户操做信息
[nanbo@test local]# logout
[root@test ~]# cd /opt/
[root@test opt]# ls
history  rh
[root@test opt]# cd history/
[root@test history]# ls
nanbo  root
[root@test history]# cd nanbo/
[root@test nanbo]# ls
172.16.255.193 history.20170816_150403
[root@test nanbo]# cat 172.16.255.193\ history.20170816_150403 
#1502867049
echo "hahahahah"
#1502867052
cd /usr/local/
#1502867053
ls
#1502867056
cat /etc/passwd
#1502867062
ls /var/log/messages

过一段时间,root操做记录也会有linux

[root@test ~]# cd /opt/history/
[root@test history]# ls
nanbo  root
[root@test history]# cd root/
[root@test root]# ll
total 32K
d-wx------ 2 root root 4.0K Aug 16 23:07 .
drwxrwxrwx 4 root root 4.0K Aug 16 15:04 ..
-rw------- 1 root root 2.4K Aug 16 16:43 192.168.1.193 history.20170816_134444
-rw------- 1 root root 1.2K Aug 16 17:05 192.168.1.193 history.20170816_150350
-rw------- 1 root root  251 Aug 16 18:43 192.168.1.202 history.20170816_184256
-rw------- 1 root root   18 Aug 16 20:54 192.168.1.213 history.20170816_205434
-rw------- 1 root root  329 Aug 16 23:07 192.168.1.213 history.20170816_210614
-rw------- 1 root root  185 Aug 16 15:24 172.29.20.24 history.20170816_150535
[root@test root]# cat 192.168.1.193\ history.20170816_134444 
#1502861816
cat /etc/profile
#1502861851
ls
#1502861862
vim /etc/profile
#1502861881
source /etc/profile
#1502861887
cd /usr/local/
#1502861894
vim /etc/profile
#1502861906
source /etc/profile

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
还有一种方案:这个方案会在每一个用户退出登陆 时把用户所执行的每个命令都发送给日志守护进程rsyslogd,你也可经过配置“/etc/rsyslog.conf”进一步将日志发送给日志服务器:shell

操做以下:
把下面内容添加到/etc/profile文件底部
[root@elk-node1 ~]# vim /etc/profile
........
 
#设置history格式
export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`who am i 2>/dev/null| \
awk '{print $NF}'|sed -e 's/[()]//g'`] "
 
#登陆时清空当前缓存
echo "" > .bash_history
 
#记录shell执行的每一条命令
export PROMPT_COMMAND='\
if [ -z "$OLD_PWD" ];then
export OLD_PWD=$PWD;
fi;
if [ ! -z "$LAST_CMD" ] && [ "$(history 1)" != "$LAST_CMD" ]; then
logger -t `whoami`_shell_cmd "[$OLD_PWD]$(history 1)";
fi ;
export LAST_CMD="$(history 1)";
export OLD_PWD=$PWD;'

[root@elk-node1 ~]# sudo /etc/profile

测试:
分别使用kevin,grace帐号登录这台服务器进行一些操做。
而后发现/var/log/message日志文件中已经记录了这两个用户的各自操做了~
[root@elk-node2 ~]# tail -20 /var/log/messages
Oct 24 14:16:04 elk-node2 root_shell_cmd: [/root] 321 [2016-10-24 14:16:04] [gateway] tail -100 /var/log/messages
Oct 24 14:19:09 elk-node2 root_shell_cmd: [/root] 322 [2016-10-24 14:18:51] [gateway] vim /etc/profile
Oct 24 14:19:12 elk-node2 su: (to kevin) root on pts/0
Oct 24 14:19:25 elk-node2 root_shell_cmd: [/root] 315 [2016-10-24 14:19:23] [gateway] tail -f /var/log/messages
Oct 24 14:19:40 elk-node2 kevin_shell_cmd: [/home/kevin] 2 [2016-10-24 14:19:40] [gateway] echo "123456" > test
Oct 24 14:19:43 elk-node2 kevin_shell_cmd: [/home/kevin] 3 [2016-10-24 14:19:43] [gateway] uptime
Oct 24 14:19:45 elk-node2 kevin_shell_cmd: [/home/kevin] 4 [2016-10-24 14:19:45] [gateway] who
Oct 24 14:19:47 elk-node2 kevin_shell_cmd: [/home/kevin] 5 [2016-10-24 14:19:47] [gateway] last
Oct 24 14:19:48 elk-node2 root_shell_cmd: [/root] 323 [2016-10-24 14:19:12] [gateway] su - kevin
Oct 24 14:19:52 elk-node2 su: (to grace) root on pts/0
Oct 24 14:20:00 elk-node2 grace_shell_cmd: [/usr/local/src] 2 [2016-10-24 14:20:00] [gateway] ls
Oct 24 14:20:03 elk-node2 grace_shell_cmd: [/usr/local/src] 3 [2016-10-24 14:20:03] [gateway] date
Oct 24 14:20:11 elk-node2 grace_shell_cmd: [/usr/local/src] 4 [2016-10-24 14:20:11] [gateway] free -m
Oct 24 14:20:12 elk-node2 root_shell_cmd: [/root] 324 [2016-10-24 14:19:52] [gateway] su - grace
Oct 24 14:20:23 elk-node2 root_shell_cmd: [/root] 316 [2016-10-24 14:20:18] [gateway] tail -f /etc/sudoers
Oct 24 14:20:30 elk-node2 root_shell_cmd: [/root] 317 [2016-10-24 14:20:24] [gateway] tail -f /var/log/messages
Oct 24 14:20:35 elk-node2 root_shell_cmd: [/root] 318 [2016-10-24 14:20:35] [gateway] tail -100 /var/log/messages
Oct 24 14:20:46 elk-node2 su: (to kevin) root on pts/0
Oct 24 14:23:42 elk-node2 root_shell_cmd: [/root] 325 [2016-10-24 14:20:46] [gateway] su - kevin
Oct 24 14:23:45 elk-node2 root_shell_cmd: [/root] 326 [2016-10-24 14:23:45] [gateway] cat /etc/profile