歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 記錄Linux系統下所有用戶的操作信息

記錄Linux系統下所有用戶的操作信息

日期:2017/2/28 13:47:38   编辑:Linux教程

在日常運維中,我們需要清楚服務器上每個用戶登錄後都做了哪些操作,我們需要記錄下每個用戶的操作命令。
下面的內容設置可以實現在Linux下所有用戶,不管是遠程還是本地登陸,在本機的所有操作都會記錄下來,並生成包含“用戶/IP/時間”的文件存放在指定位置。

PS1="`whoami`@`hostname`:"'[$PWD]'
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 /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_IP} history.$DT"
chmod 600 /tmp/history/${LOGNAME}/*history* 2>/dev/null

將上面的內容追加到/etc/profile文件裡,source /etc/profile生效後。切換到一個路徑後,顯示的是全路徑,並且終端顯示中沒有#符號或$符號.
root@elk-node1:[/root]cd /usr/local/src/
root@elk-node1:[/usr/local/src]

上面的顯示跟默認的linux終端顯示不太習慣。現在要求終端裡切換路徑後,只顯示當前的簡介路徑,不顯示全部路徑,並且後面帶上#或$符號,那麼只需要將上面的第一行PS1參數後面的設置改下
1)只顯示當前簡介路徑,不顯示全路徑,顯示#號
PS1="[\u@\h \W]"#
2)只顯示當前簡介路徑,不顯示全路徑,顯示$號
PS1="[\u@\h \W]\$"

這裡,我選擇第(1)種帶#號顯示,生效後的終端顯示內容和linux默認顯示的一樣,如下:
[root@elk-node1]#cd /usr/local/
[root@elk-node1 local]#pwd #顯示的只是當前簡介路徑local,而不是全路徑/usr/local
/usr/local
[root@elk-node1 local]#

這樣,各個用戶的操作記錄的日志路徑如下:

[root@elk-node1 history]#pwd
/tmp/history
[root@elk-node1 history]#ll
total 4
d-wx------. 2 root root 82 Oct 21 19:44 root
d-wx------. 2 wangshibo wangshibo 4096 Oct 21 20:00 wangshibo
[root@elk-node1 history]#cd wangshibo/
[root@elk-node1 wangshibo]#ll
total 12
-rw-------. 1 wangshibo wangshibo 74 Oct 21 19:11 gateway history.20161021_190706
-rw-------. 1 wangshibo wangshibo 22 Oct 21 19:28 gateway history.20161021_192848
-rw-------. 1 wangshibo wangshibo 23 Oct 21 20:00 gateway history.20161021_200045
[root@elk-node1 wangshibo]#tail -f gateway\ history.20161021_200045
touch test
rm -f test

---------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------
還有一種方案:
這個方案會在每個用戶退出登錄 時把用戶所執行的每一個命令都發送給日志守護進程rsyslogd,你也可通過配置“/etc/rsyslog.conf”進一步將日志發送給日志服務器

操作如下:
把下面內容添加到/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


測試:
分別使用wangshibo,guohuihui賬號登陸這台服務器進行一些操作。
然後發現/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 wangshibo) 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 wangshibo_shell_cmd: [/home/wangshibo] 2 [2016-10-24 14:19:40] [gateway] echo "123456" > test
Oct 24 14:19:43 elk-node2 wangshibo_shell_cmd: [/home/wangshibo] 3 [2016-10-24 14:19:43] [gateway] uptime
Oct 24 14:19:45 elk-node2 wangshibo_shell_cmd: [/home/wangshibo] 4 [2016-10-24 14:19:45] [gateway] who
Oct 24 14:19:47 elk-node2 wangshibo_shell_cmd: [/home/wangshibo] 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 - wangshibo
Oct 24 14:19:52 elk-node2 su: (to guohuihui) root on pts/0
Oct 24 14:20:00 elk-node2 guohuihui_shell_cmd: [/usr/local/src] 2 [2016-10-24 14:20:00] [gateway] ls
Oct 24 14:20:03 elk-node2 guohuihui_shell_cmd: [/usr/local/src] 3 [2016-10-24 14:20:03] [gateway] date
Oct 24 14:20:11 elk-node2 guohuihui_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 - guohuihui
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 wangshibo) root on pts/0
Oct 24 14:23:42 elk-node2 root_shell_cmd: [/root] 325 [2016-10-24 14:20:46] [gateway] su - wangshibo
Oct 24 14:23:45 elk-node2 root_shell_cmd: [/root] 326 [2016-10-24 14:23:45] [gateway] cat /etc/profile

Copyright © Linux教程網 All Rights Reserved