linux 记录所有用户bash操作日志

默北 安全214,2492字数 670阅读2分14秒阅读模式

记录所有用户登录系统的任何操作日志,以便有据可查。

1.编辑 /etc/profile文件。文章源自运维生存时间-https://www.ttlsa.com/safe/linux-record-all-user-bash-operation-log/

# vim /etc/profil

2. 在其后添加如下内容文章源自运维生存时间-https://www.ttlsa.com/safe/linux-record-all-user-bash-operation-log/

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

3. 加载文件生效文章源自运维生存时间-https://www.ttlsa.com/safe/linux-record-all-user-bash-operation-log/

# source /etc/profile

/var/log/history 日志的存放位置,该目录下以每个用户为名创建一个文件夹,每次用户退出后都会产生以用户名、登录IP、时间的日志文件,以及用户本次的所有操作。文章源自运维生存时间-https://www.ttlsa.com/safe/linux-record-all-user-bash-operation-log/

root用户不记录在此。文章源自运维生存时间-https://www.ttlsa.com/safe/linux-record-all-user-bash-operation-log/ 文章源自运维生存时间-https://www.ttlsa.com/safe/linux-record-all-user-bash-operation-log/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 08/01/2018 15:05:59
  • 转载请务必保留本文链接:https://www.ttlsa.com/safe/linux-record-all-user-bash-operation-log/
评论  2  访客  2
    • crasybest
      crasybest 0

      # vim /etc/profil
      这里漏了一个e

      • 匿名
        匿名 9

      评论已关闭!