想知道用户登陆系统后都操作了什么,怎么办?
别急,linux下有一个script工具,专门记录终端会话中所有输入输出结果,并存放到指定文件中。
先看看怎么录制吧!
1、创建日志存放目录
|
1
2
|
# mkdir /opt/operation_log
# chmod 777 -R /opt/operation_log
|
2、设置用户登陆后自动录制
|
1
2
3
4
5
|
# vi /etc/profile #末尾追加一下内容
if
[ $UID -
ge
500 ];
then
exec
script -t 2>
/opt/operation_log/
$USER-$UID-`
date
+%F-%T`.
date
-a -q -f
/opt/operation_log/
$USER-$UID-`
date
+%F-%T`.log
fi
# source /etc/profile #刷新生效
|
参数说明:
-t:记录操作时序,2>将输出的时序存到指定文件中,回放时用到此时间文件
-a:输出结果追加到文件中
-q:静默启动
-f:每次写完后刷新输出
3、查看生成的文件
|
1
2
3
4
|
# ll /opt/operation_log/
total 8
-rw-rw-r-- 1
test
test
124 Jul 3 07:17
test
-1001-2015-07-03-07:17:36.
date
-rw-rw-r-- 1
test
test
167 Jul 3 07:17
test
-1001-2015-07-03-07:17:36.log
|
可以看到,分别生成我们定义的日志格式。
当用记录日志比较多时,用more或者cat查看就比较费劲了,这时有个对应的工具叫scriptrelay,通过结合script输出的时序文件,可以自动播放。
4、操作记录回放
|
1
|
# scriptreplay test-1001-2015-07-03-07:17:36.date test-1001-2015-07-03-07:17:36.log
|
是不是轻松多了!
如果你只是单纯记录本次操作命令的话,可以直接运行:
# script test
会切换到script中,等你执行完命令后输入exit退出,再查看test文件即可。
本文转自 李振良OK 51CTO博客,原文链接:http://blog.51cto.com/lizhenliang/1670563,如需转载请自行联系原作者