通過登陸IP記錄Linux所有使用者登入所操作的日誌

來源:互聯網
上載者:User


通過登陸IP記錄Linux所有使用者登入所操作的日誌 對於Linux使用者操作記錄一般通過命令history來查看記錄,但是如果在由於誤操作而刪除了
重要的資料的情況下,history命令就不會有什麼作用了。那麼依然要存有曆史操作記錄應該如何
來實現呢?  www.2cto.com  其實我們可以通過登陸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目錄查看記錄:  www.2cto.com  [root@server ~]# cd /tmp[root@server tmp]# ll總計 24drwx------ 2 root root 4096 2012-10-11 gconfd-rootdrwxrwxrwx 3 root root 4096 2012-10-11 historydrwx------ 2 root root 4096 08-11 01:11 keyring-Ki8IOJsrwxr-xr-x 1 root root    0 2012-10-11 mapping-rootsrw------- 1 root root    0 2012-10-11 scim-panel-socket:0-rootdrwx------ 2 root root 4096 2012-10-11 ssh-jPPigl3182drwx------ 2 root root 4096 10-10 21:16 ssh-KDmPtr3350[root@server tmp]# cd history/[root@server history]# ll總計 4d-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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.