对于Linux用户操作记录一般通过命令history来查看历史记录,但是如果在由于误操作而删除了重要的数据的情况下,history命令就不会有什么作用了。那么依然要存有历史操作记录应该如何来实现呢?
其实我们可以通过登陆IP地址来记录所有用户登录所操作的历史操作!具体操作就是在/etc/profile配置文件的末尾加入以下脚本代码来实现:
[root@server ~]# cat >>/etc/profile<< EOF
>
> history
>
> USER=`whoami`
>
> USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
>
> if [ "$USER_IP" = "" ]; then
> USER_IP=`hostname`
> fi
>
> if [ ! -d /tmp/history ]; then
> mkdir /tmp/history
> chmod 777 /tmp/history
> fi
>
> if [ ! -d /tmp/history/${LOGNAME} ]; then
> mkdir /tmp/history/${LOGNAME}
> chmod 300 /tmp/history/${LOGNAME}
> fi
>
> export HISTSIZE=4096
>
> DT=`date +"%Y-%m-%d_%H:%M:%S"`
>
> export HISTFILE="/tmp/history/${LOGNAME}/${USER}@${USER_IP}_history.$DT"
>
> chmod 600 /tmp/history/${LOGNAME}/*history* 2>/dev/null
>
> EOF
[root@server ~]# source /etc/profile
[root@server ~]# logout
# 此时需要退出系统再重新登录,在/tmp/history/目录下才有记录
[root@server ~]# cat >>/etc/profile<< EOF > > history > > USER=`whoami` > > USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'` > > if [ "$USER_IP" = "" ]; then > USER_IP=`hostname` > fi > > if [ ! -d /tmp/history ]; then > mkdir /tmp/history > chmod 777 /tmp/history > fi > > if [ ! -d /tmp/history/${LOGNAME} ]; then > mkdir /tmp/history/${LOGNAME} > chmod 300 /tmp/history/${LOGNAME} > fi > > export HISTSIZE=4096 > > DT=`date +"%Y-%m-%d_%H:%M:%S"` > > export HISTFILE="/tmp/history/${LOGNAME}/${USER}@${USER_IP}_history.$DT" > > chmod 600 /tmp/history/${LOGNAME}/*history* 2>/dev/null > > EOF [root@server ~]# source /etc/profile [root@server ~]# logout # 此时需要退出系统再重新登录,在/tmp/history/目录下才有记录 |
[root@server ~]# cd /tmp [root@server tmp]# ll 总计 24 drwx------ 2 root root 4096 2012-10-11 gconfd-root drwxrwxrwx 3 root root 4096 2012-10-11 history drwx------ 2 root root 4096 08-11 01:11 keyring-Ki8IOJ srwxr-xr-x 1 root root 0 2012-10-11 mapping-root srw------- 1 root root 0 2012-10-11 scim-panel-socket:0-root drwx------ 2 root root 4096 2012-10-11 ssh-jPPigl3182 drwx------ 2 root root 4096 10-10 21:16 ssh-KDmPtr3350 [root@server tmp]# cd history/ [root@server history]# ll 总计 4 d-wx------ 2 root root 4096 10-10 21:16 root [root@server history]# cd root/ [root@server root]# ll 总计 4 -rw------- 1 root root 37 10-10 21:16 root@192.168.1.96_history.2012-10-10_21:16:42 |
[root@server tmp]# ll
总计 24
drwx------ 2 root root 4096 2012-10-11 gconfd-root
drwxrwxrwx 3 root root 4096 2012-10-11 history
drwx------ 2 root root 4096 08-11 01:11 keyring-Ki8IOJ
srwxr-xr-x 1 root root 0 2012-10-11 mapping-root
srw------- 1 root root 0 2012-10-11 scim-panel-socket:0-root
drwx------ 2 root root 4096 2012-10-11 ssh-jPPigl3182
drwx------ 2 root root 4096 10-10 21:16 ssh-KDmPtr3350
[root@server tmp]# cd history/
[root@server history]# ll
总计 4
d-wx------ 2 root root 4096 10-10 21:16 root
[root@server history]# cd root/
[root@server root]# ll
总计 4
-rw------- 1 root root 37 10-10 21:16 root@192.168.1.96_history.2012-10-10_21:16:42