博客
关于我
Linux系统日志管理
阅读量:792 次
发布时间:2023-02-05

本文共 2853 字,大约阅读时间需要 9 分钟。

Linux日志分类与管理指南

1. 连接时间日志

连接时间日志通常由/var/log/wtmp/var/run/utmp这两个文件记录。这些文件无法直接通过cat命令查看,因为它们由系统自动更新。要查看连接时间,可以使用以下命令:

whowac -p  # 查看每个用户的连接时间ac -a  # 查看所有用户的连接时间ac -d  # 查看用户每天的连接时间

示例输出:

[root@xhot ~]# whoroot tty1 2010-10-06 22:56root pts/0 2010-10-06 22:26 (218.192.87.4)root pts/1 2010-10-06 23:41 (218.192.87.4)root pts/3 2010-10-06 23:18 (218.192.87.4)
[root@xhot ~]# w01:01:02 up 2:36, 4 users, load average: 0.15, 0.03, 0.01USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot tty1 - 22:56 1:20m 0.16s 0.16s -bashroot pts/0 218.192.87.4 22:26 2:05m 0.18s 0.18s -bashroot pts/1 218.192.87.4 23:41 0:00s 0.41s 0.00s wroot pts/3 218.192.87.4 23:18 1:38m 0.03s 0.03s -bash

2. 进程监控日志

进程统计日志用于监控用户的操作行为,非常有助于发现异常情况。可以通过以下命令查看进程信息:

accton /var/account/pacct  # 启用进程统计日志监控lastcomm  # 查看进程统计日志

示例输出:

[root@xhot ~]# lastcommaccton S root pts/1 0.00 secs Thu Oct 7 01:20accton root pts/1 0.00 secs Thu Oct 7 01:20ac root pts/1 0.00 secs Thu Oct 7 01:14ac root pts/1 0.00 secs Thu Oct 7 01:14free root pts/1 0.00 secs Thu Oct 7 01:10lastcomm root pts/1 0.00 secs Thu Oct 7 01:09ifconfig root pts/1 0.00 secs Thu Oct 7 01:09

关闭进程统计日志监控:

accton  # 关闭进程统计日志监控

3. 系统和服务日志

系统日志由syslog服务管理,常用的日志文件包括:

  • /var/log/lastlog:记录最后一次用户成功登录的时间和IP地址。
  • /var/log/messages:记录系统和服务错误信息。
  • /var/log/secure:系统安全日志,记录用户登录认证和异常行为。
  • /var/log/btmp:记录登录失败信息。
  • /var/log/cron:记录crond计划任务执行情况。

查看示例:

[root@xhot ~]# cat /var/log/lastlogLpts/0218.192.87.4Lpts/1218.192.87.4Lpts/1218.192.87.4Lpts/0218.192.87.46Lpts/0218.192.87.4

4. Linux日志服务器配置

默认配置文件位于/etc/syslog.conf,主要配置文件包括:

  • /etc/syslog.conf:主配置文件
  • /etc/sysconfig/syslog:辅助配置文件

修改配置文件时,注意以下内容:

  • 消息类型:auth, authpriv, security, cron, daemon, kern, lpr, mail, news, syslog, user, uucp, local0~local7
  • 错误级别:debug, info, notice, warning, err, error, crit, alert, emerg, panic
  • 动作域:file, user, console, @remote_ip

示例配置:

[root@xhot ~]# grep -v "#" /etc/syslog.conf*.info;mail.none;authpriv.none;cron.none /var/log/messagesauthpriv.* /var/log/securemail.* -/var/log/maillogcron.* /var/log/cron*.emerg *uucp,news.crit /var/log/spoolerlocal7.* /var/log/boot.log

5. 日志转储服务

日志文件随时间增长会占用大量存储空间,日志转储可以通过logrotate工具和cron计划任务实现。配置文件位于/etc/logrotate.conf,示例如下:

[root@xhot ~]# cat /etc/logrotate.conf# see "man logrotate" for details# rotate log files weeklyweekly# keep 4 weeks worth of backlogsrotate 4createcompressinclude /etc/logrotate.d/var/log/wtmp {    monthly    create 0664 root utmp    rotate 1}

添加自定义转储规则:

# 为/var/log/httpd/access.log和/var/log/http/error.log设置转储参数/var/log/httpd/access.log /var/log/http/error.log {    rotate 5    mail    size=100k    sharedscripts    killall -HUP httpd    endscript    nocompress}

创建转储配置文件:

[root@xhot ~]# vim /etc/logrotate.d/test.log/var/log/test.log {    rotate 10    size=50k    compress    postrotate        killall -HUP syslog    endscript}

总结

通过以上方法,可以有效管理Linux系统的日志文件,实现日志分类、转储和监控。这些工具和方法能够帮助系统管理员更好地分析服务器运行状态和用户行为。

转载地址:http://bokfk.baihongyu.com/

你可能感兴趣的文章
Linux系统本地部署MongoDB数据库并实现远程访问方法指南
查看>>
Linux系统查看JDK的安装路径
查看>>
linux系统查看电脑设备型号,Linux系统查看硬件信息
查看>>
Linux系统查看系统信息命令(比较全)
查看>>
Linux系统状态分析与监控工具详解
查看>>
Linux系统环境变量和别名设置(永久生效和临时生效)
查看>>
Linux系统用户和权限管理
查看>>
linux系统监控与硬盘分区/格式化/文件系统管理
查看>>
Linux系统程序安装(三)源码包安装程序
查看>>
Linux系统突然所有命令都失效了,显示bash: xxxxx: command not found...
查看>>
Linux系统管理期末考试小结
查看>>
Linux系统终端与正确关机
查看>>
Linux系统编程:深入理解计算机软硬件体系和架构
查看>>
Linux系统调优实战
查看>>
Linux系统调用分析
查看>>
Linux系统轻量应用服务器CPU使用率高的排查与解决方案
查看>>
Linux系统通过netstat查看网络状态、端口状态
查看>>
Linux系统配置静态IP地址步骤
查看>>
linux系统重命名文件名,但文件名有空格如何处理
查看>>
linux线程
查看>>