linux:记录操作日志

这个方案会在每个用户退出登录 时把用户所执行的每一个命令都发送给日志守护进程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 /]#
    原文作者:随风化作雨
    原文地址: https://www.jianshu.com/p/e50d3678b549
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞