Linux 系统运行着许多子系统和应用程序。您可以使用系统日志记录从启动时就收集有关运行中系统的数据。有时,您只需要知道一切正常就好;而有时,您会使用此数据进行审计和调试,认识到磁盘或其他资源已耗尽以及用于许多其他目的。您可以在一个系统上收集日志数据,然后将其转发到另一系统进行处理。日志数据可以显示在终端(例如 root 用户的终端)上,但更多是保存在文件中,或通过套接字转发到日志服务器。毋庸置疑,日志记录是高度可配置的。
#Log anything 'warn' or higher.#Exclude authpriv, cron, mail, and news.These are logged elsewhere.#Don't log private authentication messages! *.warn;\ authpriv.none;cron.none;mail.none;news.none -/var/log/syslog #The authpriv file has restricted access. authpriv.* /var/log/secure #Log all the mail messages in one place. mail.* -/var/log/maillog #Log cron stuff cron.* /var/log/cron #Everybody gets emergency messages *.emerg * #Save news errors of level crit and higher in a special file. uucp,news.crit /var/log/spooler
#Kernel messages are first, stored in the kernel
#file, critical messages and higher ones also go
#to another host and to the console
#
kern.* /var/adm/kernel
kern.crit @log‑server
kern.crit /dev/console
kern.info;kern.!err /var/adm/kernel‑info
#Store all mail messages except info priority in /var/log/mail.
mail.*;mail.!=info /var/log/mail
root@attic4‑sl42:~#tail ‑n 20 /var/log/messages
Nov 19 21:39:57 attic4‑sl42 kernel: [ 1403.274747] usb 1‑1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Nov 19 21:39:57 attic4‑sl42 kernel: [ 1403.274751] usb 1‑1.1: Product: USB DISK 2.0
Nov 19 21:39:57 attic4‑sl42 kernel: [ 1403.274755] usb 1‑1.1: Manufacturer:
Nov 19 21:39:57 attic4‑sl42 kernel: [ 1403.274759] usb 1‑1.1: SerialNumber: 070B53DF11FC0170
Nov 19 21:39:57 attic4‑sl42 kernel: [ 1403.275036] usb‑storage 1‑1.1:1.0: USB Mass Storage device detected
Nov 19 21:39:57 attic4‑sl42 kernel: [ 1403.275801] scsi host12: usb‑storage 1‑1.1:1.0
Nov 19 21:39:57 attic4‑sl42 mtp‑probe: checking bus 1, device 12: "/sys/devices/pci0000:00/0000:00:12.2/usb1/1‑1/1‑1.1"
Nov 19 21:39:57 attic4‑sl42 mtp‑probe: bus: 1, device: 12 was not an MTP device
Nov 19 21:39:58 attic4‑sl42 kernel: [ 1404.301570] scsi 12:0:0:0: Direct‑Access USB DISK 2.0 PMAP PQ: 0 ANSI: 4
Nov 19 21:39:59 attic4‑sl42 kernel: [ 1405.651626] sd 12:0:0:0: [sdd] 30299520 512‑byte logical blocks: (15.5 GB/14.4 GiB)
Nov 19 21:39:59 attic4‑sl42 kernel: [ 1405.652236] sd 12:0:0:0: [sdd] Write Protect is off
Nov 19 21:40:00 attic4‑sl42 kernel: [ 1405.678810] sdd: sdd1
Nov 19 21:40:00 attic4‑sl42 kernel: [ 1405.683911] sd 12:0:0:0: [sdd] Attached SCSI removable disk
Nov 19 21:42:36 attic4‑sl42 kernel: [ 1562.844117] usb 1‑1.1: reset high‑speed USB device number 12 using ehci‑pci
Nov 19 21:42:37 attic4‑sl42 kernel: [ 1563.044142] usb 1‑1.1: reset high‑speed USB device number 12 using ehci‑pci
Nov 19 21:42:37 attic4‑sl42 kernel: [ 1563.245138] usb 1‑1.1: reset high‑speed USB device number 12 using ehci‑pci
Nov 19 21:42:37 attic4‑sl42 kernel: [ 1563.619154] usb 1‑1.1: reset high‑speed USB device number 12 using ehci‑pci
Nov 19 21:42:38 attic4‑sl42 kernel: [ 1563.919798] usb 1‑1.1: USB disconnect, device number 12
Nov 19 21:42:38 attic4‑sl42 kernel: <27>[ 1563.950967] udevd[3540]: inotify_add_watch(6, /dev/sdd, 10) failed: No such file or directory
Nov 19 21:56:48 attic4‑sl42 ‑‑ MARK ‑‑
#/etc/logrotate.conf ##logrotate is designed to ease administration of systems that generate large #numbers of log files.It allows automatic rotation, compression, removal, and #mailing of log files.Each log file may be handled daily, weekly, monthly, or #when it grows too large.##logrotate is normally run daily from root's crontab.##For more details, see "man logrotate".#rotate log files weekly: weekly #keep 4 weeks worth of backlogs: rotate 4 #create new (empty) log files after rotating old ones: create #uncomment if you want to use the date as a suffix of the rotated file #dateext #uncomment this if you want your log files compressed: #compress #some packages install log rotation information in this directory: include /etc/logrotate.d #Rotate /var/log/wtmp: /var/log/wtmp { missingok monthly create 0664 root utmp minsize 1M rotate 1 } #Rotate /var/log/btmp: /var/log/btmp { missingok monthly create 0600 root root rotate 1 } #Note that /var/log/lastlog is not rotated.This is intentional, and it should #not be.The lastlog file is a database, and is also a sparse file that takes #up much less space on the drive than it appears.#system-specific logs may be also be configured below:
Nov 19 15:48:31 attic4-sl42 kernel: [ 7.407406] EXT4-fs (sda6): re-mounted.Opts: (null) Nov 19 15:48:32 attic4-sl42 mtp-probe: checking bus 3, device 3: "/sys/devices/pci0000:00/0000:00:12.0/usb3/3-2/3-2.1" Nov 19 15:48:32 attic4-sl42 mtp-probe: bus: 3, device: 3 was not an MTP device Nov 19 15:48:32 attic4-sl42 mtp-probe: checking bus 3, device 4: "/sys/devices/pci0000:00/0000:00:12.0/usb3/3-2/3-2.4" Nov 19 15:48:32 attic4-sl42 mtp-probe: bus: 3, device: 4 was not an MTP device Nov 19 15:48:32 attic4-sl42 mtp-probe: checking bus 1, device 4: "/sys/devices/pci0000:00/0000:00:12.2/usb1/1-1/1-1.1" Nov 19 15:48:32 attic4-sl42 mtp-probe: bus: 1, device: 4 was not an MTP device Nov 19 15:48:36 attic4-sl42 root: /etc/rc.d/rc.inet1: /sbin/ifconfig lo 127.0.0.1 Nov 19 15:48:36 attic4-sl42 root: /etc/rc.d/rc.inet1: /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo Nov 19 15:48:36 attic4-sl42 root: /etc/rc.d/rc.inet1: /sbin/dhcpcd -t 10 eth0 Nov 19 15:48:36 attic4-sl42 dhcpcd[1112]: eth0: adding address fe80::4c2a:3f48:e0f7:cc90 Nov 19 15:48:41 attic4-sl42 sshd[1255]: Server listening on 0.0.0.0 port 22.Nov 19 15:48:41 attic4-sl42 sshd[1255]: Server listening on :: port 22.Nov 19 15:48:41 attic4-sl42 ntpd[1262]: ntpd 4.2.8p8@1.3265-o Fri Jun 3 23:08: 22 UTC 2016 (1): Starting Nov 19 15:48:41 attic4-sl42 ntpd[1262]: Command line: /usr/sbin/ntpd -g -p /var /run/ntpd.pid Nov 19 15:48:41 attic4-sl42 ntpd[1264]: proto: precision = 0.230 usec (-22) Nov 19 15:48:41 attic4-sl42 ntpd[1264]: Listen and drop on 0 v6wildcard [::]:12 3 Nov 19 15:48:41 attic4-sl42 ntpd[1264]: Listen and drop on 1 v4wildcard 0.0.0.0:123 Nov 19 15:48:41 attic4-sl42 ntpd[1264]: Listen normally on 2 lo 127.0.0.1:123 Nov 19 15:48:41 attic4-sl42 ntpd[1264]: Listen normally on 3 eth0 192.168.1.24:123 Nov 19 15:48:41 attic4-sl42 ntpd[1264]: Listen normally on 4 lo [::1]:123 Nov 19 15:48:41 attic4-sl42 ntpd[1264]: failed to init interface for address fe80::8616:f9ff:fe04:7a2a%2 Nov 19 15:48:41 attic4-sl42 ntpd[1264]: Listening on routing socket on fd #21 for interface updates Nov 19 15:48:41 attic4-sl42 acpid: starting up with netlink and the input layer Nov 19 15:48:41 attic4-sl42 acpid: 1 rule loaded Nov 19 15:48:41 attic4-sl42 acpid: waiting for events: event logging is off Nov 19 15:48:42 attic4-sl42 dbus[1226]: [system] Activating service name='org.freedesktop.PolicyKit1' (using servicehelper) Nov 19 15:48:42 attic4-sl42 ntpd[1264]: failed to init interface for address fe80::8616:f9ff:fe04:7a2a%2
清单 8 的最后一行表明 Network Time Protocol 守护程序 (ntpd) 失败。在这种情况下,无法初始化 IP V6 接口,因为该系统仅使用 IP V4 连接。
root@attic4-sl42:~#grep "attic4-sl42 ntpd" /var/log/messages | tail -9 Nov 19 21:17:12 attic4-sl42 ntpd[1131]: Command line: /usr/sbin/ntpd -g -p /var/run/ntpd.pid Nov 19 21:17:12 attic4-sl42 ntpd[1133]: proto: precision = 0.220 usec (-22) Nov 19 21:17:13 attic4-sl42 ntpd[1133]: Listen and drop on 0 v6wildcard [::]:123 Nov 19 21:17:13 attic4-sl42 ntpd[1133]: Listen and drop on 1 v4wildcard 0.0.0.0:123 Nov 19 21:17:13 attic4-sl42 ntpd[1133]: Listen normally on 2 lo 127.0.0.1:123 Nov 19 21:17:13 attic4-sl42 ntpd[1133]: Listen normally on 3 eth0 192.168.1.24:123 Nov 19 21:17:13 attic4-sl42 ntpd[1133]: Listen normally on 4 lo [::1]:123 Nov 19 21:17:13 attic4-sl42 ntpd[1133]: Listen normally on 5 eth0 [fe80::8616:f9ff:fe04:7a2a%2]:123 Nov 19 21:17:13 attic4-sl42 ntpd[1133]: Listening on routing socket on fd #22 for interface updates
root@attic4-sl42:~#tail -n 1 -f /var/log/messages Nov 20 09:24:58 attic4-sl42 kernel: [43705.563240] sd 15:0:0:0: [sdd] Attached SCSI removable disk Nov 20 09:26:23 attic4-sl42 kernel: [43790.820125] usb 3-2.4: USB disconnect, device number 5 Nov 20 09:27:13 attic4-sl42 sshd[6059]: Accepted password for ian from 192.168.1.40 port 58184 ssh2 Nov 20 09:29:08 attic4-sl42 kernel: [43955.890670] usb 3-2.4: new low-speed USB device number 6 using ohci-pci Nov 20 09:29:08 attic4-sl42 kernel: [43955.989492] usb 3-2.4: New USB device found, idVendor=046d, idProduct=c50e Nov 20 09:29:08 attic4-sl42 kernel: [43955.989501] usb 3-2.4: New USB device strings: Mfr=1, Product=2, SerialNumber=0 Nov 20 09:29:08 attic4-sl42 kernel: [43955.989506] usb 3-2.4: Product: USB Receiver Nov 20 09:29:08 attic4-sl42 kernel: [43955.989509] usb 3-2.4: Manufacturer: Logitech Nov 20 09:29:08 attic4-sl42 kernel: [43956.003175] input: Logitech USB Receiver as /devices/pci0000:00/0000:00:12.0/usb3/3-2/3-2.4/3-2.4:1.0/0003:046D:C50E.0004/input/input19 Nov 20 09:29:08 attic4-sl42 kernel: [43956.054049] hid-generic 0003:046D:C50E.0004: input,hidraw1: USB HID v1.11 Mouse [Logitech USB Receiver] on usb-0000:00:12.0-2.4/input0 Nov 20 09:29:08 attic4-sl42 mtp-probe: checking bus 3, device 6: "/sys/devices/pci0000:00/0000:00:12.0/usb3/3-2/3-2.4" Nov 20 9:29:08 attic4-sl42 mtp-probe: bus: 3, device: 6 was not an MTP device
[ian@attic4‑ce7 ~]$ cat /etc/rsyslog.conf
#rsyslog configuration file
#For more information see /usr/share/doc/rsyslog‑*/rsyslog_conf.html
#If you experience problems, see https://www.rsyslog.com/doc/v8-stable/troubleshooting/index.html####MODULES ####
#The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock #provides support for local system logging (e.g. via logger command)
$ModLoad imjournal #provides access to the systemd journal
#$ModLoad imklog #reads kernel messages (the same are read from journald)
#$ModLoad immark #provides ‑‑MARK‑‑ message capability
#Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514
#Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514
####GLOBAL DIRECTIVES ####
#Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog
#Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
#File syncing capability is disabled by default.This feature is usually not required,
#not useful and an extreme performance hit
#$ActionFileEnableSync on
#Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/.conf
#Turn off message reception via local log socket;
#local messages are retrieved through imjournal now.
$OmitLocalLogging on
#File to store the position in the journal
$IMJournalStateFile imjournal.state
####RULES ####
#Log all kernel messages to the console.
#Logging much else clutters up the screen.
#kern./dev/console
#Log anything (except mail) of level info or higher.
#Don't log private authentication messages!
.info;mail.none;authpriv.none;cron.none /var/log/messages
#The authpriv file has restricted access.
authpriv./var/log/secure
#Log all the mail messages in one place.
mail.‑/var/log/maillog
#Log cron stuff
cron./var/log/cron
#Everybody gets emergency messages
.emerg :omusrmsg:
#Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler
#Save boot messages also to boot.log
local7./var/log/boot.log
####begin forwarding rule ###
#The statement between the begin ... end define a SINGLE forwarding
#rule.They belong together, do NOT split them.If you create multiple
#forwarding rules, duplicate the whole block!
#Remote Logging (we use TCP for reliable delivery)
#
#An on‑disk queue is created for this action.If the remote host is
#down, messages are spooled to disk and sent when it is up again.
#$ActionQueueFileName fwdRule1 #unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g #1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on #save messages to disk on shutdown
#$ActionQueueType LinkedList #run asynchronously
#$ActionResumeRetryCount ‑1 #infinite retries if host is down
#remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#.*` remote‑host:514
####end of the forwarding rule ###
您将在文件中间看到熟悉的 syslog.conf 条目,其中包含 rsyslog 设施可以理解的其他内容。有关更多详细信息,可参见手册页、信息页或 HTML 文档。
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
#Entries in this file show the compile time defaults.
#You can change settings by editing this file.
#Defaults can be restored by simply deleting this file.
#
#See journald.conf(5) for details.
[Journal]
#Storage=auto
#Compress=yes
#Seal=yes
#SplitMode=uid
#SyncIntervalSec=5m
#RateLimitIntervalSec=30s
#RateLimitBurst=1000
#SystemMaxUse=
#SystemKeepFree=
#SystemMaxFileSize=
#SystemMaxFiles=100
#RuntimeMaxUse=
#RuntimeKeepFree=
#RuntimeMaxFileSize=
#RuntimeMaxFiles=100
#MaxRetentionSec=
#MaxFileSec=1month
#ForwardToSyslog=no
#ForwardToKMsg=no
#ForwardToConsole=no
#ForwardToWall=yes
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg
[root@atticf26 ~]#systemctl list‑units "journal" ‑‑no‑pager
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt‑journal‑core.service loaded active running Creates ABRT problems fro
systemd‑journal‑flush.service loaded active exited Flush Journal to Persiste
systemd‑journald.service loaded active running Journal Service
systemd‑journald‑audit.socket loaded active running Journal Audit Socket
systemd‑journald‑dev‑log.socket loaded active running Journal Socket (/dev/log)
systemd‑journald.socket loaded active running Journal Socket
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high‑level unit activation state, i.e. generalization of SUB.
SUB = The low‑level unit activation state, values depend on unit type.
6 loaded units listed.Pass ‑‑all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list‑unit‑files'.