博客
关于我
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的文件夹操作
查看>>
linux的用户管理
查看>>
Linux的目录结构及ls命令介绍
查看>>
Linux的诞生:一场自由与协作的技术革命
查看>>
linux监控与管理常用命令
查看>>
Linux目录中/和~的区别
查看>>
Linux目录结构简析
查看>>
linux相关log日志按文件大小进行切割
查看>>
Linux硬盘检测工具详解
查看>>
Linux磁盘IO状态分析实战
查看>>
linux磁盘分割
查看>>
linux磁盘清理
查看>>
Linux磁盘管理--RAID原理
查看>>
linux禁止Root远程登陆
查看>>
linux移动文件命令
查看>>
linux程序分析工具介绍(三)——sar
查看>>
linux程序打印cr3寄存器,linux – 每次从内核模块读取时,为什么CR3寄存器内容会有所不同?...
查看>>
linux程序段错误原理,Linux 下c 程序段错误分析
查看>>
linux端口汇聚,linux端口聚合
查看>>
Linux端口状态含义
查看>>