linux下监控用户的操作记录-new

  • 格式:pdf
  • 大小:123.23 KB
  • 文档页数:3

linux下监控用户的操作记录
这个方案会在每个用户退出登录时把用户所执行的每一个命令都发送给日志守护进程rsyslogd,你也可通过配置“/etc/rsyslog.conf”进一步将日志发送给日志服务器
操作如下:
确保存在/data目录
把下面内容添加到/etc/profile文件底部
[root@vm17218 /]# vi /etc/profile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19........
DT=`date "+%Y-%m-%d_%H:%M:%S"`
export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`who am i 2>/dev/null| \
awk '{print $NF}'|sed -e 's/[()]//g'`] "
echo "" > .bash_history
if [ ! -d /data/history ]
then
mkdir /data/history 2>/dev/null
chmod 777 /data/history
fi
if [ ! -d /data/history/${LOGNAME} ]
then
mkdir /data/history/${LOGNAME} 2>/dev/null
chmod 777 /data/history/${LOGNAME}
fi
export PROMPT_COMMAND='\
if [ "$(history 1)" != "$LAST_CMD" ]; then
export LAST_CMD="[$OLD_PWD]$(history 1)";
echo "$LAST_CMD" >>
"/data/history/${LOGNAME}/${LOGNAME}_shell_cmd_$DT" 2>/dev/null fi ;
export LAST_CMD="$(history 1)";
export OLD_PWD=$PWD;'
[root@vm17218 /]# source /etc/profile
操作日志存档位置:
在/data/history目录下按照登陆用户名创建目录[root@vm17218space2 history]# ll
total 4
drwxrwxrwx. 2 baicells baicells 4096 May 11 03:15 baicells drwxrwxrwx. 2 root root 47 May 11 03:21 root
按照当次登陆时间命名和存档
[baicells@vm17218space2 baicells]$ ll
total 16
-rw-rw-r--. 1 baicells baicells 652 May 11 03:11 baicells_historylog_2017-05-11_03:09:55
-rw-rw-r--. 1 baicells baicells 129 May 11 03:11 baicells_shell_cmd_2017-05-11_03:11:52
-rw-rw-r--. 1 baicells baicells 1277 May 11 03:15 baicells_shell_cmd_2017-05-11_03:12:08
-rw-rw-r--. 1 baicells baicells 1367 May 11 03:20 baicells_shell_cmd_2017-05-11_03:15:36
[baicells@vm17218space2 baicells]$
记录操作的具体时间、登陆IP地址、执行命令行以及该操作所在目录
[root@vm17218space2 history]# more baicells/baicells_shell_cmd_2017-05-11_03:12:08
[/home/baicells] 2 [2017-05-11 03:12:13] [111.207.206.12] cd /data
[/data] 3 [2017-05-11 03:12:14] [111.207.206.12] ll
[/data] 4 [2017-05-11 03:12:16] [111.207.206.12] cd history
[/data/history] 5 [2017-05-11 03:12:18] [111.207.206.12] ll
[/data/history] 6 [2017-05-11 03:12:20] [111.207.206.12] cd baicells
[/data/history/baicells] 7 [2017-05-11 03:12:20] [111.207.206.12] ll
[/data/history/baicells] 8 [2017-05-11 03:12:26] [111.207.206.12] more baicells_shell_cmd_2017-05-11_03:12:08
[/data/history/baicells] 9 [2017-05-11 03:13:29] [111.207.206.12] export LAST_CMD="$(history 1)[$OLD_PWD]";
[/data/history/baicells] 10 [2017-05-11 03:13:32] [111.207.206.12] ls
[/data/history/baicells] 11 [2017-05-11 03:13:37] [111.207.206.12] more baicells_shell_cmd_2017-05-11_03:12:08
[/data/history/baicells] 12 [2017-05-11 03:13:51] [111.207.206.12] cd .. [/data/history] 13 [2017-05-11 03:13:54] [111.207.206.12] more baicells_shell_cmd_2017-05-11_03:12:08
[/data/history] 14 [2017-05-11 03:13:57] [111.207.206.12] cd baicells
[/data/history/baicells] 15 [2017-05-11 03:13:58] [111.207.206.12] more baicells_shell_cmd_2017-05-11_03:12:08
[/data/history/baicells] 16 [2017-05-11 03:14:32] [111.207.206.12] sudo vi /etc/profile。