美文网首页linux整理
linux:记录操作日志

linux:记录操作日志

作者: 随风化作雨 | 来源:发表于2017-12-01 12:52 被阅读271次

    这个方案会在每个用户退出登录 时把用户所执行的每一个命令都发送给日志守护进程rsyslogd,你也可通过配置“/etc/rsyslog.conf”进一步将日志发送给日志服务器

    操作如下:

    把下面内容添加到/etc/profile文件底部

    [root@vm17218 /]# 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@vm17218 /]# source /etc/profile
    

    测试:

    分别使用baicells,root账号登陆这台服务器进行一些操作。
    然后发现/var/log/message日志文件中已经记录了这两个用户的各自操作了~

    [root@vm17218 /]# tail -30 ../var/log/messages
    ……
    May  2 04:22:26 vm17218 baicells_shell_cmd: [/etc]   14  [2017-05-02 04:22:22] [111.207.206.9] sudo source profile
    May  2 04:22:52 vm17218 baicells_shell_cmd: [/etc]   15  [2017-05-02 04:22:52] [111.207.206.9] ip add
    May  2 04:22:55 vm17218 baicells_shell_cmd: [/etc]   16  [2017-05-02 04:22:55] [111.207.206.9] ifconfig
    ……
    May  2 04:27:29 vm17218 root_shell_cmd: [/]   82  [2017-05-02 04:27:29] [03:32] source /etc/profile
    May  2 04:28:11 vm17218 root_shell_cmd: [/]   83  [2017-05-02 04:28:11] [03:32] tail -30 messages
    May  2 04:28:35 vm17218 root_shell_cmd: [/]   84  [2017-05-02 04:28:35] [03:32] tail ../var/log/messages
    [root@vm17218 /]#
    

    相关文章

      网友评论

        本文标题:linux:记录操作日志

        本文链接:https://www.haomeiwen.com/subject/ljaibxtx.html