通过syslog记录history历史记录推荐

为了完善linux系统审计,记录所有登录到本机人员的操作记录到系统日志,然后上传到日志服务器。

步骤1:初始化history的相关参数

执行以下命令:

echo'#HistoryformatexportHISTTIMEFORMAT="%Y-%m-%d%H:%M:%S"shopt-shistappendexportPROMPT_COMMAND="history-n;history-a"exportHISTSIZE=100000exportHISTFILESIZE=100000' /etc/profile.d/history.sh

使得配置生效,运行:source /etc/profile.d/history.sh

步骤2:修改syslog的配置文件

echo'#recordhistoryloglocal4.=debug-/var/log/history.log#logtosyslogserver*.*@syslog-server' /etc/syslog.conf

定义local4 debug为记录history日志的标记,并记录到/var/log/history.log

将@syslog-server修改成你当前使用的日志服务器的ip地址,比如192.168.12.10(如果没有日志服务器可以忽略此项)

注意:rsyslog修改方式同上,只是配置文件改成/etc/rsyslog.conf

重启syslog服务:/etc/init.d/syslog restart

步骤3:定时运行get_history.sh脚本

或者从这里下载:

https://github.com/June-Wang/github4shell/blob/master/get_history.sh

#!/bin/bashawk-F:'$3 =500 $NF!="/sbin/nologin"||$1=="root"{print$1,$6}'/etc/passwd|\whilereaduserpathhis_file="$path/.bash_history"if[-s"${his_file}"];thencount=`cat${his_file}|wc-l`cat${his_file}|\whilereadlinedostr=`echo"${line}"|sed-nr'/^#[0-9]{10}$/p'`if[-n"${str}"];thenmy_time=`echo"${line}"|sed's/^#//'`my_date=`date-d"1970-01-01UTC${my_time}sec""+%Y-%m-%d%T"`echo-en"${my_date}"echo"${line}"fidone|sort|\whilereadcmddologger-thistory[$$]-plocal4.debug"${user}${cmd}"donesed-i"1,${count}d"${his_file}fidone

将以上脚本放入crontab中,每10分钟运行一次:

echo'#gethistory*/10****root/root/shell/get_history.sh /dev/null' /etc/crontab

至此,部署完毕。

查看日志内容:

tail /var/log/history.log

效果如下:

2012-08-29T17:46:14.531831+08:00localhosthistory[11753]:root2012-08-2916:59:44cat.bash_history2012-08-29T17:46:14.533192+08:00localhosthistory[11753]:root2012-08-2917:45:13tail-f/var/log/history.log2012-08-29T17:46:14.534344+08:00localhosthistory[11753]:root2012-08-2917:45:29vi/etc/crontab2012-08-29T17:46:14.535546+08:00localhosthistory[11753]:root2012-08-2917:45:38/root/shell/get_history.sh2012-08-29T17:46:14.536739+08:00localhosthistory[11753]:root2012-08-2917:45:42tail-f/var/log/history.log

学习会使你永远立于不败之地。

通过syslog记录history历史记录推荐

相关文章:

你感兴趣的文章:

标签云: