歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 通過登陸IP記錄Linux所有用戶登錄所操作日志的方法

通過登陸IP記錄Linux所有用戶登錄所操作日志的方法

日期:2017/3/1 18:06:09   编辑:Linux技術
對於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/目錄下才有記錄

通過上面的腳本代碼可以看出來,在系統的/tmp下就新建了個history目錄(這個目錄可以自定義),在目錄中記錄了所有的登陸過系統的用戶和IP地址,這也是監測系統安全的方法之一。在進行一系列的操作之後,我們進入/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 [email protected]_history.2012-10-10_21:16:42
Copyright © Linux教程網 All Rights Reserved