通過登陸IP記錄Linux所有用戶登錄所操作日志的方法
發(fā)布日期:2022-04-05 11:44 | 文章來源:腳本之家
對(duì)于Linux用戶操作記錄一般通過命令history來查看歷史記錄,但是如果在由于誤操作而刪除了重要的數(shù)據(jù)的情況下,history命令就不會(huì)有什么作用了。那么依然要存有歷史操作記錄應(yīng)該如何來實(shí)現(xiàn)呢?
其實(shí)我們可以通過登陸IP地址來記錄所有用戶登錄所操作的歷史操作!具體操作就是在/etc/profile配置文件的末尾加入以下腳本代碼來實(shí)現(xiàn):
[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
# 此時(shí)需要退出系統(tǒng)再重新登錄,在/tmp/history/目錄下才有記錄
通過上面的腳本代碼可以看出來,在系統(tǒng)的/tmp下就新建了個(gè)history目錄(這個(gè)目錄可以自定義),在目錄中記錄了所有的登陸過系統(tǒng)的用戶和IP地址,這也是監(jiān)測(cè)系統(tǒng)安全的方法之一。在進(jìn)行一系列的操作之后,我們進(jìn)入/tmp/history目錄查看歷史記錄:
[root@server ~]# cd /tmp
[root@server tmp]# ll
總計(jì) 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
總計(jì) 4
d-wx------ 2 root root 4096 10-10 21:16 root
[root@server history]# cd root/
[root@server root]# ll
總計(jì) 4
-rw------- 1 root root 37 10-10 21:16 root@192.168.1.96_history.2012-10-10_21:16:42
其實(shí)我們可以通過登陸IP地址來記錄所有用戶登錄所操作的歷史操作!具體操作就是在/etc/profile配置文件的末尾加入以下腳本代碼來實(shí)現(xiàn):
復(fù)制代碼
代碼如下:[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
# 此時(shí)需要退出系統(tǒng)再重新登錄,在/tmp/history/目錄下才有記錄
通過上面的腳本代碼可以看出來,在系統(tǒng)的/tmp下就新建了個(gè)history目錄(這個(gè)目錄可以自定義),在目錄中記錄了所有的登陸過系統(tǒng)的用戶和IP地址,這也是監(jiān)測(cè)系統(tǒng)安全的方法之一。在進(jìn)行一系列的操作之后,我們進(jìn)入/tmp/history目錄查看歷史記錄:
復(fù)制代碼
代碼如下:[root@server ~]# cd /tmp
[root@server tmp]# ll
總計(jì) 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
總計(jì) 4
d-wx------ 2 root root 4096 10-10 21:16 root
[root@server history]# cd root/
[root@server root]# ll
總計(jì) 4
-rw------- 1 root root 37 10-10 21:16 root@192.168.1.96_history.2012-10-10_21:16:42
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章