查看/etc/profile 和 ~/.bash_profile这两文件中HISTSIZE和HISTFILESIZE的设置不为0
mkdir /var/log/histopry;chmod 777 /var/log/history
把以下内容加入到/etc/profile中
#history
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
HISTDIR=/var/log/history
if [ -z $USER_IP ]
then
USER_IP=`hostname`
fi
if [ ! -d $HISTDIR/${LOGNAME} ]
then
mkdir -p $HISTDIR/${LOGNAME}
chmod 300 $HISTDIR/${LOGNAME}
fi
DT=`date +%Y%m%d`
export HISTFILE="$HISTDIR/${LOGNAME}/${USER_IP}.$DT"
touch $HISTFILE 2>/dev/null
chmod 600 $HISTFILE 2>/dev/null
chattr +a $HISTFILE 2>/dev/null
#要实现以上这句需要修改/etc/sudoers文件加上以下两句,允许用户对指定目录使用chattr这个命令
#Cmnd_Alias DSKCMD=/usr/bin/chattr +a /var/log/history/*
#%developers ALL=(ALL) NOPASSWD:DSKCMD
#
shopt -s histappend
readonly PROMPT_COMMAND="history -a"
readonly HISTFILE
readonly HISTFILESIZE
readonly HISTSIZE
readonly HISTCMD
readonly HISTCONTROL
readonly HISTIGNORE
重新登陆即可.
本shell是配合前一个记录用户历史操作记录shell的,代码如下:
#!/bin/sh -
# filename getrecord
user=
time=
while [ $# -gt 0 ]
do
case $1 in
-u|--user) user=$2
shift 2
;;
-t|--time) time=$2
shift 2
;;
-*) echo "$0:$1 Wrong Options!" >&2
shift
exit 0
;;
--) break
;;
*) break
;;
esac
done
if [ -z "$user" ]
then
user=${LOGNAME}
fi
if [ -z "$time" ]
then
time=`date +%Y%m%d`
fi
file=`find /tmp/operation/$user/ |awk '/'$time'$/'`
cat $file
此段shell通过接收用户及时间参数来查看某用户的历史操作记录,如:
# getrecord -u root -t 20101223 --查看root在12月23号对服务器做的记录
如果不给定参数,shell将提取当前登陆用户及当日时间来进行查询.
你可以将此shell添加到bin中,直接使用命令来查询:
# mv getrecord ~/bin
# getrecord
转换日志里面的时间:
history -r /日志文件路径;history
脚本直接处理日志里面的时间:
vi his.sh
#!/bin/bash
$1|while read line
do
[ -z "$line" ]
if [ `echo $line|grep "^#"` ] ;
then
time=`echo $line|awk -F \\# '{print $NF}'`
printf "`date +"%F_%T" -d @$time` "
else
echo $line
fi
done
使用: /root/his.sh "cat /var/log/history/root/192.168.1.26.20130311"