歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux多用戶登錄自動保存history到統一目錄

Linux多用戶登錄自動保存history到統一目錄

日期:2017/2/27 15:54:56   编辑:Linux教程
生產環境下,多個普通用戶登錄,登錄後自動記錄history操作到某個統一目錄保存。

具體要求:
1) 每個用戶登錄後自動創建子目錄及history記錄文件;
2) 允許用戶創建history記錄文件並追加內容,不允許修改和刪除;
3) 不允許用戶修改和刪除其他用戶的子目錄;
4) 不允許用戶查看其他用戶的子目錄內容;

解決方案:
編寫 /etc/profile 文件,添加下面內容
# securiry record history
# add by shenxiaoran

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/user_history ];then
    mkdir /tmp/user_history
    chown root.root /tmp/user_history
    chmod 777 /tmp/user_history
    chattr +a /tmp/user_history
fi

if [ ! -d /tmp/user_history/${LOGNAME} ];then
    mkdir -p /tmp/user_history/${LOGNAME}
fi

export HISTTIMEFORMAT='%F %T '
export HISTSIZE='40960'
time=$(date '+%Y%m%d-%H:%M:%S')
export HISTFILE="/tmp/user_history/${LOGNAME}/${USER_IP}[$time]"
chmod 600 /tmp/user_history/${LOGNAME}/*history* 2>/dev/null

保存
# source /etc/profile 生效

測試:
創建普通用戶,passwd 用戶口令,使用普通用戶登錄

普通用戶登錄後不會創建history記錄文件,退出後立即生成,如下所示:
# ll /tmp/user_history/shen/
total 8
-rw------- 1 shen shen 227 Mar 12 15:21 192.168.11.50[20150312-15:21:30]
-rw------- 1 shen shen 259 Mar 12 16:36 192.168.11.50[20150312-15:40:58]
Copyright © Linux教程網 All Rights Reserved