为了完善linux系统审计,记录所有登录到本机人员的操作记录到系统日志,然后上传到日志服务器。 步骤1:初始化history的相关参数 执行以下命令: echo'#HistoryformatexportHISTTIMEFORMAT="%Y-
为了完善linux系统审计,记录所有登录到本机人员的操作记录到系统日志,然后上传到日志服务器。
步骤1:初始化history的相关参数
执行以下命令:
echo '# History format export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S " shopt -s histappend export PROMPT_COMMAND="history -n;history -a" export HISTSIZE=100000 export HISTFILESIZE=100000 ' > /etc/profile.d/history.sh使得配置生效,运行:source /etc/profile.d/history.sh
步骤2:修改syslog的配置文件
echo '#record history log local4.=debug -/var/log/history.log #log to syslog server *.* @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/bash awk -F: '$3>=500 && $NF!="/sbin/nologin" || $1=="root"{print $1,$6}' /etc/passwd|\ while read user path do his_file="$path/.bash_history" if [ -s "${his_file}" ];then count=`cat ${his_file}|wc -l` cat ${his_file}|\ while read line do str=`echo "${line}"|sed -nr '/^#[0-9]{10}$/p'` if [ -n "${str}" ];then my_time=`echo "${line}"|sed 's/^#//'` my_date=`date -d "1970-01-01 UTC ${my_time} sec""+%Y-%m-%d %T "` echo -en "${my_date}" else echo "${line}" fi done |sort|\ while read cmd do logger -t history[$$] -p local4.debug "${user} ${cmd}" done sed -i "1,${count}d" ${his_file} fi done将以上脚本放入crontab中,每10分钟运行一次:
echo '#get history */10 * * * * root /root/shell/get_history.sh >/dev/null' >>/etc/crontab至此,部署完毕。
查看日志内容:
tail /var/log/history.log
效果如下:
2012-08-29T17:46:14.531831+08:00 localhost history[11753]: root 2012-08-29 16:59:44 cat .bash_history 2012-08-29T17:46:14.533192+08:00 localhost history[11753]: root 2012-08-29 17:45:13 tail -f /var/log/history.log 2012-08-29T17:46:14.534344+08:00 localhost history[11753]: root 2012-08-29 17:45:29 vi /etc/crontab 2012-08-29T17:46:14.535546+08:00 localhost history[11753]: root 2012-08-29 17:45:38 /root/shell/get_history.sh 2012-08-29T17:46:14.536739+08:00 localhost history[11753]: root 2012-08-29 17:45:42 tail -f /var/log/history.log