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

简介:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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 查看所有登录用户进程信息,这些查看命令大同小异;
  
2、使用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操作记录也会有

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[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”进一步将日志发送给日志服务器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
操作如下:
把下面内容添加到 /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
***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************

本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/7373146.html,如需转载请自行联系原作者
相关文章
|
2月前
|
资源调度 JavaScript Linux
【Azure 应用服务】本地Node.js部署上云(Azure App Service for Linux)遇到的三个问题解决之道
【Azure 应用服务】本地Node.js部署上云(Azure App Service for Linux)遇到的三个问题解决之道
|
8天前
|
消息中间件 分布式计算 Java
Linux环境下 java程序提交spark任务到Yarn报错
Linux环境下 java程序提交spark任务到Yarn报错
18 5
|
8天前
|
Java Linux Python
Linux环境下 代码java调用python出错
Linux环境下 代码java调用python出错
22 3
|
7天前
|
Oracle Java 关系型数据库
Linux下JDK环境的配置及 bash: /usr/local/java/bin/java: cannot execute binary file: exec format error问题的解决
如果遇到"exec format error"问题,文章建议先检查Linux操作系统是32位还是64位,并确保安装了与系统匹配的JDK版本。如果系统是64位的,但出现了错误,可能是因为下载了错误的JDK版本。文章提供了一个链接,指向Oracle官网上的JDK 17 Linux版本下载页面,并附有截图说明。
Linux下JDK环境的配置及 bash: /usr/local/java/bin/java: cannot execute binary file: exec format error问题的解决
|
1月前
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
9天前
|
Linux 编译器 开发工具
快速在linux上配置python3.x的环境以及可能报错的解决方案(python其它版本可同样方式安装)
这篇文章介绍了在Linux系统上配置Python 3.x环境的步骤,包括安装系统依赖、下载和解压Python源码、编译安装、修改环境变量,以及常见安装错误的解决方案。
20 1
|
2月前
|
前端开发 Linux
深度探索Linux操作系统 —— 构建桌面环境3
深度探索Linux操作系统 —— 构建桌面环境
34 12
|
1月前
|
Shell Linux API
C语言在linux环境下执行终端命令
本文介绍了在Linux环境下使用C语言执行终端命令的方法。首先,文章描述了`system()`函数,其可以直接执行shell命令并返回结果。接着介绍了更强大的`popen()`函数,它允许程序与命令行命令交互,并详细说明了如何使用此函数及其配套的`pclose()`函数。此外,还讲解了`fork()`和`exec`系列函数,前者创建新进程,后者替换当前进程执行文件。最后,对比了`system()`与`exec`系列函数的区别,并针对不同场景推荐了合适的函数选择。
|
9天前
|
Linux Python
linux之部署python环境&创建虚拟环境
linux之部署python环境&创建虚拟环境
|
9天前
|
Web App开发 Linux Python
linux上安装selenium环境及测试
该文章提供了在Linux CentOS上安装Selenium环境、Chrome浏览器及Chromedriver的详细步骤,并演示了如何以无头模式进行测试。
26 0
下一篇
无影云桌面