记录终端执行的命令

/etc/profile文件添加以下脚本

#vim /etc/profile
tty1=$(tty | awk -F"/" '{print "pts""/"$4}')
ip=$(w | grep "\bpts/0\b" | awk '{print $3}')
export HISTTIMEFORMAT="`whoami`$ip  %F %T "
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 ]
then
   mkdir -p $HISTDIR
   chmod 777 $HISTDIR
fi
if [ ! -d $HISTDIR/${LOGNAME} ]
then
    mkdir -p $HISTDIR/${LOGNAME}
    chmod 300 $HISTDIR/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date +%Y%m%d_%H%M%S`
export HISTFILE="$HISTDIR/${LOGNAME}/${USER_IP}.hist.$DT"
chmod 600 $HISTDIR/${LOGNAME}/*.history* 2>/dev/null

生效

#source /etc/profile

验证

# ll /var/log/.history/root/
total 20
-rw------- 1 root root  197 2018-05-15 17:58 192.168.1.252.hist.20180515_175752
-rw------- 1 root root  291 2018-05-15 18:00 192.168.1.252.hist.20180515_175805
-rw------- 1 root root  407 2018-05-15 18:23 192.168.1.252.hist.20180515_180627
-rw------- 1 root root 2417 2018-05-15 18:17 192.168.1.252.hist.20180515_181706
-rw------- 1 root root  594 2018-05-15 18:23 192.168.1.252.hist.20180515_181740
posted @ 2018-05-15 18:23  你很棒  阅读(383)  评论(0编辑  收藏  举报