本文共 2853 字,大约阅读时间需要 9 分钟。
连接时间日志通常由/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
进程统计日志用于监控用户的操作行为,非常有助于发现异常情况。可以通过以下命令查看进程信息:
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 # 关闭进程统计日志监控
系统日志由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
默认配置文件位于/etc/syslog.conf
,主要配置文件包括:
/etc/syslog.conf
:主配置文件/etc/sysconfig/syslog
:辅助配置文件修改配置文件时,注意以下内容:
示例配置:
[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
日志文件随时间增长会占用大量存储空间,日志转储可以通过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/