通常,我們運維管理人員需要知道一臺服務器上有哪些用戶登錄過,在服務器上執行了哪些命令,幹了哪些事情,這就要求記錄服務器上所用登錄用戶的操作信息,這對於安全維護來說很有必要。廢話不多說了,下面直接記錄做法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | 1)查看及管理當前登錄用戶 使用w命令查看當前登錄用戶正在使用的進程信息,w命令用於顯示已經登錄系統的用戶的名稱,以及它們正在做的事。該命令所使用的信息來源於 /var/run/utmp 文件。w命令輸出的信息包括: -> 用戶名稱 -> 用戶的機器名稱或 tty 號 -> 遠程主機地址 -> 用戶登錄系統的時間 -> 空閒時間(作用不大) -> 附加到 tty (終端)的進程所用的時間(JCPU時間) -> 當前進程所用時間(PCPU時間) -> 用戶當前正在使用的命令 [root@ test ~] # w 13:54:14 up 2 days, 2:53, 4 users , load average: 0.02, 0.02, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root tty1 - Mon11 3:44m 0.11s 0.06s - bash test pts /0 172.16.255.202 10:11 1:48m 0.11s 0.03s vim userinfo.text nanli pts /3 172.16.255.196 12:01 1:52m 0.00s 0.00s - bash work pts /4 172.116.55.13 12:08 0.00s 0.02s 0.00s w 此外,可以使用 "who am i" 查看使用該命令的用戶及進程,使用 who 查看所有登錄用戶進程信息,這些查看命令大同小異; 2、使用pkill強制退出登錄的用戶 使用pkill可以結束當前登錄用戶的進程,從而強制退出用戶登錄,具體使用可以結合w命令; -> 使用w查看當前登錄的用戶,注意TTY所示登錄進程終端號 -> 使用 "pkill –9 -t TTY終端號" 結束該進程所對應用戶登錄(可根據FROM的IP地址或主機號來判斷) [root@ test ~] # pkill -9 pts/4 [root@ test ~] # w 13:59:23 up 2 days, 2:56, 4 users , load average: 0.02, 0.02, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root tty1 - Mon11 3:44m 0.11s 0.06s - bash test pts /0 172.16.255.202 10:11 1:48m 0.11s 0.03s vim userinfo.text 2)查看所有登錄用戶的操作歷史 在Linux系統的環境下,不管是root用戶還是其它的用戶只有登陸系統後用進入操作我們都可以通過命令 history 來查看歷史記錄。可是假如一臺服務器多人登陸,一天因爲某人誤操作了刪除 了重要的數據。這時候通過查看歷史記錄(命令: history )是沒有什麼意義了(因爲 history 只針對登錄用戶下執行有效,即使root用戶也無法得到其它用戶histotry歷史)。那有沒有什麼 辦法實現通過記錄登陸後的IP地址和某用戶名所操作的歷史記錄呢?答案肯定是有的! 通過在 /etc/profile 文件底部添加以下代碼就可以實現: [root@ test ~] # cat /etc/profile ...... #記錄每個用戶的操作信息 export PS1= '[\u@\h \w]# ' history 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 /opt/history ] then mkdir /opt/history chmod 777 /opt/history fi if [ ! -d /opt/history/ ${LOGNAME} ] then mkdir /opt/history/ ${LOGNAME} chmod 300 /opt/history/ ${LOGNAME} fi export HISTSIZE=4096 DT=` date + "%Y%m%d_%H%M%S" ` export HISTFILE= "/opt/history/${LOGNAME}/${USER_IP} history.$DT"
或者&&
複製代碼: 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 /var/log/history ]; then mkdir /var/log/history chmod 777 /var/log/history fi if [ ! -d /var/log/history/${LOGNAME} ]; then mkdir /var/log/history/${LOGNAME} chmod 300 /var/log/history/${LOGNAME} fi export HISTSIZE=4096 DT=`date +"%Y%m%d_%H:%M:%S"` export HISTFILE="/var/log/history/${LOGNAME}/${USER}@${USER_IP}_$DT" chmod 600 /var/log/history/${LOGNAME}/*history* 2>/dev/null ####wangyongzhi##### IP=`awk -F"=" '/IPADDR/{print $2}' /etc/sysconfig/network-scripts/ifcfg-eth1` export PS1="[\u@$IP \w]\$ " [root@ test ~] # source /etc/profile #使得上面配置生效 上面腳本在系統的 /opt 下新建個 history 目錄,記錄所有登陸過系統的用戶和IP地址(文件名),每當用戶登錄/退出會創建相應的文件,該文件保存這段用戶登錄時期內操作歷史,可以用這個 方法來監測系統的安全性。 ------------------------------------------------------------------------------------------------------------------------------------------ 上面的顯示跟默認的linux終端顯示不太習慣。現在要求終端裏切換路徑後,只顯示當前的簡介路徑,不顯示全部路徑,並且後面帶上 #或$符號,那麼只需要將上面的第一行 PS1參數後面的設置如下: 1)只顯示當前簡介路徑,不顯示全路徑,顯示 #號。注意下面在"#"符號後面空出一格,這樣終端的"#"符號跟命令之間就有了一格的距離,習慣而已! PS1= "[\u@\h \W]# " 2)只顯示當前簡介路徑,不顯示全路徑,顯示$號。注意下面的 "$" 符號後面空出一格。 PS1= "[\u@\h \W]\$ " 這裏我在腳本選擇第(1)種帶 "#" 號顯示(也可以兩種都不選,直接將第一行PS1的設置給去掉,這樣就是默認的了終端顯示.線上使用的話,推薦使用這種默認的),生效後的終 端顯示內容和linux默認顯示的一樣。即 export PS1= "[\u@\h \W]# " ------------------------------------------------------------------------------------------------------------------------------------------ 比如:使用nanli賬號操作: [root@ test ~] # su - nanbo [nanbo@ test ~] # echo "hahahahah" hahahahah [nanbo@ test ~] # cd /usr/local/ [nanbo@ test local ] # ls bin etc games include lib lib64 libexec libzip man openssl sbin share src [nanbo@ test local ] # cat /etc/passwd [nanbo@ test local ] # ls /var/log/messages /var/log/messages 然後退出nanli賬號,查看用戶操作信息 [nanbo@ test local ] # logout [root@ test ~] # cd /opt/ [root@ test opt] # ls history rh [root@ test opt] # cd history/ [root@ test history ] # ls nanbo root [root@ test history ] # cd nanbo/ [root@ test nanbo] # ls 172.16.255.193 history .20170816_150403 [root@ test nanbo] # cat 172.16.255.193\ history.20170816_150403 #1502867049 echo "hahahahah" #1502867052 cd /usr/local/ #1502867053 ls #1502867056 cat /etc/passwd #1502867062 ls /var/log/messages |
過一段時間,root操作記錄也會有
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | [root@ test ~] # cd /opt/history/ [root@ test history ] # ls nanbo root [root@ test history ] # cd root/ [root@ test root] # ll total 32K d-wx------ 2 root root 4.0K Aug 16 23:07 . drwxrwxrwx 4 root root 4.0K Aug 16 15:04 .. -rw------- 1 root root 2.4K Aug 16 16:43 192.168.1.193 history .20170816_134444 -rw------- 1 root root 1.2K Aug 16 17:05 192.168.1.193 history .20170816_150350 -rw------- 1 root root 251 Aug 16 18:43 192.168.1.202 history .20170816_184256 -rw------- 1 root root 18 Aug 16 20:54 192.168.1.213 history .20170816_205434 -rw------- 1 root root 329 Aug 16 23:07 192.168.1.213 history .20170816_210614 -rw------- 1 root root 185 Aug 16 15:24 172.29.20.24 history .20170816_150535 [root@ test root] # cat 192.168.1.193\ history.20170816_134444 #1502861816 cat /etc/profile #1502861851 ls #1502861862 vim /etc/profile #1502861881 source /etc/profile #1502861887 cd /usr/local/ #1502861894 vim /etc/profile #1502861906 source /etc/profile |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
還有一種方案:這個方案會在每個用戶退出登錄 時把用戶所執行的每一個命令都發送給日誌守護進程rsyslogd,你也可通過配置“/etc/rsyslog.conf”進一步將日誌發送給日誌服務器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 操作如下: 把下面內容添加到 /etc/profile 文件底部 [root@elk-node1 ~] # vim /etc/profile ........ #設置history格式 export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [` who am i 2> /dev/null | \ awk '{print $NF}' | sed -e 's/[()]//g' `] " #登錄時清空當前緩存 echo "" > .bash_history #記錄shell執行的每一條命令 export PROMPT_COMMAND='\ if [ -z "$OLD_PWD" ]; then export OLD_PWD=$PWD; fi ; if [ ! -z "$LAST_CMD" ] && [ "$(history 1)" != "$LAST_CMD" ]; then logger -t ` whoami `_shell_cmd "[$OLD_PWD]$(history 1)" ; fi ; export LAST_CMD= "$(history 1)" ; export OLD_PWD=$PWD;' [root@elk-node1 ~] # sudo /etc/profile 測試: 分別使用kevin,grace賬號登陸這臺服務器進行一些操作。 然後發現 /var/log/message 日誌文件中已經記錄了這兩個用戶的各自操作了~ [root@elk-node2 ~] # tail -20 /var/log/messages Oct 24 14:16:04 elk-node2 root_shell_cmd: [ /root ] 321 [2016-10-24 14:16:04] [gateway] tail -100 /var/log/messages Oct 24 14:19:09 elk-node2 root_shell_cmd: [ /root ] 322 [2016-10-24 14:18:51] [gateway] vim /etc/profile Oct 24 14:19:12 elk-node2 su : (to kevin) root on pts /0 Oct 24 14:19:25 elk-node2 root_shell_cmd: [ /root ] 315 [2016-10-24 14:19:23] [gateway] tail -f /var/log/messages Oct 24 14:19:40 elk-node2 kevin_shell_cmd: [ /home/kevin ] 2 [2016-10-24 14:19:40] [gateway] echo "123456" > test Oct 24 14:19:43 elk-node2 kevin_shell_cmd: [ /home/kevin ] 3 [2016-10-24 14:19:43] [gateway] uptime Oct 24 14:19:45 elk-node2 kevin_shell_cmd: [ /home/kevin ] 4 [2016-10-24 14:19:45] [gateway] who Oct 24 14:19:47 elk-node2 kevin_shell_cmd: [ /home/kevin ] 5 [2016-10-24 14:19:47] [gateway] last Oct 24 14:19:48 elk-node2 root_shell_cmd: [ /root ] 323 [2016-10-24 14:19:12] [gateway] su - kevin Oct 24 14:19:52 elk-node2 su : (to grace) root on pts /0 Oct 24 14:20:00 elk-node2 grace_shell_cmd: [ /usr/local/src ] 2 [2016-10-24 14:20:00] [gateway] ls Oct 24 14:20:03 elk-node2 grace_shell_cmd: [ /usr/local/src ] 3 [2016-10-24 14:20:03] [gateway] date Oct 24 14:20:11 elk-node2 grace_shell_cmd: [ /usr/local/src ] 4 [2016-10-24 14:20:11] [gateway] free -m Oct 24 14:20:12 elk-node2 root_shell_cmd: [ /root ] 324 [2016-10-24 14:19:52] [gateway] su - grace Oct 24 14:20:23 elk-node2 root_shell_cmd: [ /root ] 316 [2016-10-24 14:20:18] [gateway] tail -f /etc/sudoers Oct 24 14:20:30 elk-node2 root_shell_cmd: [ /root ] 317 [2016-10-24 14:20:24] [gateway] tail -f /var/log/messages Oct 24 14:20:35 elk-node2 root_shell_cmd: [ /root ] 318 [2016-10-24 14:20:35] [gateway] tail -100 /var/log/messages Oct 24 14:20:46 elk-node2 su : (to kevin) root on pts /0 Oct 24 14:23:42 elk-node2 root_shell_cmd: [ /root ] 325 [2016-10-24 14:20:46] [gateway] su - kevin Oct 24 14:23:45 elk-node2 root_shell_cmd: [ /root ] 326 [2016-10-24 14:23:45] [gateway] cat /etc/profile |
***************當你發現自己的才華撐不起野心時,就請安靜下來學習吧***************