想知道用户登陆系统后都操作了什么,怎么办?

别急,linux下有一个script工具,专门记录终端会话中所有输入输出结果,并存放到指定文件中。

先看看怎么录制吧!

 

1、创建日志存放目录

# mkdir /opt/operation_log
# chmod 777 -R /opt/operation_log

2、设置用户登陆后自动录制

# vi /etc/profile   #末尾追加一下内容

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 [ $UID -ge 500 ]; then

exec script -t 2>/opt/operation_log/$USER-${USER_IP}-`date +%F-%T`.date -a -q -f /opt/operation_log/$USER-${USER_IP}-`date +%F-%T`.log

fi


# source /etc/profile   #刷新生效

参数说明:

-t:记录操作时序,2>将输出的时序存到指定文件中,回放时用到此时间文件

-a:输出结果追加到文件中

-q:静默启动

-f:每次写完后刷新输出

3、查看生成的文件

用户远程登陆操作退出后

[root@localhost operation_log]# ll
总用量 8
-rw-rw-r-- 1 work work 217 3月 9 00:50 work-192.168.199.126-2017-03-09-00:50:29.date
-rw-rw-r-- 1 work work 1556 3月 9 00:50 work-192.168.199.126-2017-03-09-00:50:29.log

可以看到,分别生成我们定义的日志格式。

当用记录日志比较多时,用more或者cat查看就比较费劲了,这时有个对应的工具叫scriptrelay,通过结合script输出的时序文件,可以自动播放。

4、操作记录回放

# scriptreplay work-192.168.199.126-2017-03-09-00:50:29.date work-192.168.199.126-2017-03-09-00:50:29.log

 

如果你只是单纯记录本次操作命令的话,可以直接运行:

# script test

会切换到script中,等你执行完命令后输入exit退出,再查看more test文件即可。

 

如果没有scriptreplay命令

[root@localhost ~]# wget http://dxdown.onlinedown.net/down/util-linux-ng-2.17.2.tar.zip
[root@localhost ~]# unzip util-linux-ng-2.17.2.tar.zip
[root@localhost ~]# tar zxvf util-linux-ng-2.17.2.tar.gz
[root@localhost ~]# cd util-linux-ng-2.17.2
[root@localhost ~]# ./configure && make (注意此处只是make了)
[root@localhost ~]# cp misc-utils/scriptreplay /usr/bin/
[root@localhost ~]# cd ..
[root@localhost ~]# rm -rf util-linux-ng-2.17.2*

这个时候就可以用scriptreplay这个命令来播放刚才的录像了

 

unzip 命令用不了:

     #yum list | grep zip/unzip 

     #yum install zip

     #yum install unzip

     基本完成,如果在编译的时候出现错误:gcc : error trying to exec 'cc1plus': execvp : No sunch        file or directory

    可以用gcc -v/g++ -v 来查看gcc 版本,会发现没有安装。安装如下:

    #yum list | grep gcc

    #yum install gcc-c++

    #yum install unzip