1、编辑vi /etc/profile
{#添加以下代码到/etc/profile配置文件下
#设置history格式
export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`who am i 2>/dev/null| \
awk '{print $NF}'|sed -e 's/[()]//g'`] "
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;'
然后保存shift+q输入wq退出!
}
2、重新加载profile配置文件
执行命令 source /etc/profile
3、配置rsyslog.conf文件
{编辑vi /etc/rsyslog.conf 文件,并且新增以下代码到rsyslog.conf文件下
*.info;mail.none;authpriv.none;cron.none;user.none /var/log/messages
user.* /var/log/history
然后保存shift+q输入wq退出!
}
4、重启rsyslog服务,加载history 日记
执行命令 systemctl restart rsyslog
5、测试
查看 cat /var/log/history 或者cd /var/log/history是否多了一个文件为history,说明配置成功
--------------------------------------------
1、通过明天切割生成日志logrotate实现
新建history文件,执行以下命令创建history文件
# vi /etc/logrotate.d/history
/var/log/history {
daily
copytruncate
rotate 7
compress
dateext
delaycompress
notifempty
missingok
create 644 root root
}
然后保存shift+q输入wq退出!
2、执行生成命令
执行命令 logrotate -f /etc/logrotate.d/history
3、查看log文件
执行cd /var/log/进入log文件,执行查看命令ls 此时log文件下多了一个history加日期文件,说明成功!
参考:
/etc/logrotate.conf 涉及了日记周期和保留时间
/etc/rsyslog.conf 涉及了日志的存放位置和记录哪些操作
需要继续配置,可百度查看logrotate.conf和rsyslog.conf参数及详情配置
Linux系统日志管理:配置history与logrotate
文章讲述了如何在Linux系统中编辑/etc/profile文件以设置history格式,并通过修改rsyslog.conf配置日志记录。接着,配置logrotate以每日切割历史记录,并测试确认配置生效。最后,讨论了logrotate.conf和rsyslog.conf在日志管理中的作用。
271

被折叠的 条评论
为什么被折叠?



