收藏 分销(赏)

Linux日志管理详解.docx

上传人:二*** 文档编号:4575715 上传时间:2024-09-30 格式:DOCX 页数:8 大小:17.75KB 下载积分:5 金币
下载 相关 举报
Linux日志管理详解.docx_第1页
第1页 / 共8页
本文档共8页,全文阅读请下载到手机保存,查看更方便
资源描述
Linux系统日志管理i-连接时间的日志 连接时间日志一般由/var/log/wtmp和/var/run/utmp这两个文件记录,不过这 两个文件无法直接cat查看,并且该文件由系统自动更新,可以通过如下: w/who/finger/id/last/lastlog/ac 进行查看 [root@xhot ~]# who root ttyl 2010-10-06 22:56 root pts/O 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 ~]# w 01:01:02 up 2:36, 4 users, load average: 0.15, 0.03, 0.01 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root ttyl - 22:56 1:20m 0.16s 0.16s -bash root pts/O 218.192.87.4 22:26 2:05m 0.18s 0.18s -bash root pts/1 218.192.87.4 23:41 0.00s 0.41s 0.00s w root pts/3 218.192.87.4 23:18 1:38m 0.03s 0.03s -bash [root@xhot ~]# ac -p 〃查看每个用户的连接时间 u51 1.23 u55 0.04 root 95.21 〃可以看到root连接时间最长吧 xhot 0.06 userl 3.93 total 100.48 [root@xhot ~]# ac -a 〃查看所有用户的连接时间 total 100.49 [root@xhot ~]# ac -d 〃查看用户每天的连接时间 Sep 24 total 0.14 Sep 25 total 14.60 Sep 26 total 13.71 Sep 27 total 21.47 Sep 28 total 11.74 Sep 29 total 6.60 Sep 30 total 8.81 Oct 1 total 9.04 Oct 2 total 0.47 〃可以看到我国庆3、4、5号出去玩了 Oct 6 total 8.62 Today total 5.29 其他几个命令不做具体介绍了.进程监控日志 进程统计监控日志在监控用户的操作指令是非常有效的。当服务器最近发现经常 无故关机或者无故被人删除文件等现象时,可以通过使用进程统计日志查看: [root@xhot ~]# accton /var/account/pacct 〃开启进程统计日志监控 [root@xhot ~]# lastcomm 〃查看进程统计日志情况 accton S root pts/1 0.00 secs Thu Oct 7 01:20 accton root pts/1 0.00 secs Thu Oct 7 01:20 ac root pts/1 0.00 secs Thu Oct 7 01:14 ac root pts/1 0.00 secs Thu Oct 7 01:14 free root pts/1 0.00 secs Thu Oct 7 01:10 lastcomm root pts/1 0.00 secs Thu Oct 7 01:09 bash F root pts/1 0.00 secs Thu Oct 7 01:09 lastcomm root pts/1 0.00 secs Thu Oct 7 01:09 ifconfig root pts/1 0.00 secs Thu Oct 7 01:09 lastcomm root pts/1 0.00 secs Thu Oct 7 01:09 lastcomm root pts/1 0.00 secs Thu Oct 7 01:09 lastcomm root pts/1 0.00 secs Thu Oct 7 01:09 accton S root pts/1 0.00 secs Thu Oct 7 01:09 [root@xhot〜]# accton 〃关闭进程统计日志监控.系统和服务日志 系统日志服务是由一个名为syslog的服务管理的,如一下日志文件都是由syslog日志服务驱动的: /var/log/lastlog :记录最后一次用户成功登陆的时间、登陆IP等信息 /var/log/messages :记录Linux操作系统常见的系统和服务错误信息 /var/log/secure : Linux系统平安日志,记录用户和工作组变坏情况、用户登陆认证情况 /var/log/btmp :记录Linux登陆失败的用户、时间以及远程IP地址 /var/log/cron :记录crond计划任务服务执行情况 [root@xhot 〜]# cat /var/log/lastlog Linux日志服务介绍 1 .在Linux系统,大局部日志都是由syslog日志服务驱动和管理的 syslog服务由两个重要的配置文件控制管理,分别是/etc/syslog.conf主配置文件和/etc/sysconfig/syslog 辅助 配置文件,/etc/init.d/syslog是启动脚本,这里主讲主配置文件/etc/syslog.conf: /etc/syslog.conf 语句结构: [root@xhot ~]# grep -v/etc/syslog.conf 〃歹ij出非#打头的每一行 *.info;mail.none;authpriv.none;cron.none /var/log/messages authpriv.* /var/log/secure mail.* -/var/log/maillog cron.* /var/log/cron *.emerg * uucp,news.crit /var/log/spooler local7.* /var/log/boot.log 选择域(消息类型.错误级别)动作域.消息、类型:auth,authprivzsecurity;cron,daemon,kernjpr,mail, markznewszsyslogzuser,uucp,local0^local7. 错误级别:(8 ^)debug,info,notice,warning| warn;err|error;crit,alert,emerg|panic 动作域:file,user,console,@remote_ip 举如上的/etc/syslog. conf文件三个例子: *.info;mail.none;authpriv.none;cron.none /var/log/messages 表示info级别的任何消息都发送到/var/log/messages日志文件,但邮件系统、验证系统 和计划任务的错误级别信息就除外,不发送(none表示禁止) cron.* /var/log/cron表示所有级别的cron信息发到/var/log/cron文件 *.emerg *表示emerg错误级别(危险状态)的所有消息类型发给所有用户Linux日志服务器配置 此服务器的配置非常简单,只是修改一个文件的一个地方,然后重启服务即可: [root@xhot ~]# grep -v /etc/sysconfig/syslog SYSLOGD_OPTIONS=n-m 0 -rn 〃只要在这里添加、'-r〃就行咯 KLOGD_OPTIONS="-xn SYSLOG_UMASK=077 [root@xhot ~]# service syslog restart 关闭内核日志记录器:[确定] 关闭系统日志记录器:[确定] 启动系统日志记录器:[确定] 启动内核日志记录器:[确定] 对于发送消息到服务器的OS,只要在写/etc/syslog.conf主配置文件的时候,作用域 为@server-ip就行了,比方针对218.192.87.24这台日志服务器,把一台ubuntu系统的所有 info级别的auth信息发给日志服务器,那么对于ubuntu系统的/etc/syslog.conf文件最后一 行添加 auth.info @218.192.87.24 就 OK 了日志转储服务 系统工作到了一定时间后,日志文件的内容随着时间和访问量的增加而越来越多, 日志文件也越来越大。而且当日志文件超过系统控制范围时候,还会对系统性能 造成影响。转储方式可以设为每年转储、每月转储、每周转储、到达一定大小转储。 在Linux系统,经常使用logrotate〃工具进行日志转储,结合cron计划任务,可以轻松 实现日志文件的转储。转储方式的设置由、、/已”/1。9「01批6.811『'配置文件控制: [root@xhot ~]# cat /etc/logrotate.conf # see "man logrotate,' for details 〃可以查看帮助文档 # rotate log files weekly weekly 〃设置每周转储 # keep 4 weeks worth of backlogs rotate 4 〃最多转储4次 # create new (empty) log files after rotating old ones create 〃当转储后文件不存储时创立它 # uncomment this if you want your log files compressed # compress 〃以压缩方式转储 # RPM packages drop log rotation information into this directory include /etc/logrotate.d 〃其他日志文件的转储方式,包含在该目录下 # no packages own wtmp — well rotate them here /var/log/wtmp { 〃设置/var/log/wtmp日志文件的转储参数 monthly 〃每月转储 create 0664 root utmp 〃转储后文件不存在时创立它,文件所有者为root, 所属组为utmp,对应的权限为0664 rotate 1 〃转储一次 } # system-specific logs may be also be configured here. 举两个例子: 为/var/log/news/目录下的所有文件设置转储参数,每周转储,转储2次,转储时将老的日志文件放到/var/log/news/old目录下,假设日志文件不存在,那么跳过。完成后重启 news新闻组服务,转储时不压缩。那么可以在/etc/logrotate.conf文件的最后添加如下: /var/log/news/*{ monthly rotate 2 olddir /var/log/news/old missingok postrotate kill -HUP ' cat /var/run/inn.pid' endscript nocompress } 另一个例子:^j/var/log/ d/access.log ^D/var/log/ d/error.log 日志设置转储 参数。转储5次,转储时发送邮件给root@2calhost用户,当日志文件到达100KB时才转储,转储 后重启 d服务,那么可以直接在/etc/logrotate.conf文件的最后添加如下: /var/log/ d/access.log /var/log/ /error.log{ rotate 5 mail「oot@localhost size=100k sharedscripts /sbin/killall -HUP d endscript }自定义日志转储(/etc/logrotate.d/*) 通过下面一个例子将所有类型错误级别为info的日志转储到/var/log/test・log日志文件中,并 设置 /var/log/test.log达至ij 50KB后进行转储,转储1。次,转储时压缩,转储后重启syslog服 务: 1、修改/etc/syslog.conf文件使得如下: [root@xhot 〜]# tail -1 /etc/syslog.conf 〃查看该文件的最后一行 *.info /var/log/test.log 2、重启syslog服务: [root@xhot 〜]# /sbin/service syslog restart 关闭内核日志记录器:[确定] 关闭系统口志记录器:[确定] 启动系统日志记录器:[确定] 启动内核日志记录器:[确定] 3、创立/etc/logrotate,d/test・log日志转储参数配置文件,添加如下: [root@xhot 〜]# vim /etc/logrotate.d/test.log [root@xhot 〜]# cat /etc/logrotate.d/test.log /var/log/test.log{ rotate 10 size = 50k compress postrotate killall -HUP syslog endscript } 4、查看文件/etc/cron.daily/logrotate确保如下: [root@xhot 〜]# cat /etc/cron.daily/logrotate #!/bin/sh /usr/sbin/logrotate /etc/logrotate.conf EXITVALUE=$? if [ $ EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]H fi exit 0 5、查看转储后的文件 [root@xhot log]# pwd /var/log [root@xhot log]# Is test.log* ……〃结果等要转储的时候会发现压缩文件和原本的test.log文件
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服