/var/log/ directory, including the following files:/var/log/messages: contains most system messages and error information, including kernel messages, system startup information, and system service start and stop information. This file is usually the primary source for system administrators to troubleshoot and monitor the system./var/log/secure: contains authentication and security-related log information, including user login and logout information, sudo command usage information, and SSH connection information. This file is usually the primary source for system administrators to perform security audits and monitoring./var/log/audit/audit.log: records security audit information in the system. This file is usually generated and maintained by the auditd daemon process, and can help system administrators monitor security events and activities in the system./var/log/dnf.log: records detailed information about software package installation, update, and removal operations performed by using the DNF package manager in the system./var/log/boot.log: contains log information during system startup, including startup script output, service start and stop information, and more. This file is usually the primary source for system administrators to troubleshoot startup failures and monitor the system./var/log/lastlog: records the last login information of all users in the system./var/log/wtmp: records the login and logout information of all users in the system, including usernames, login times, logout times, login sources, and login IP addresses.journalctl command to view system logs.journalctl
<service-name> with the name of the service you want to view:journalctl -u <service-name>
journalctl --since "2024-01-01 00:00:00" --until "2024-01-02 00:00:00"
journalctl -k
/run/log/journal/. To record all boot logs, you can manually create the /var/log/journal directory, after which the journald service will store boot logs there.mkdir -p /var/log/journal/reboot
journalctl --list-boots
IDX column indicates the boot sequence number. Negative numbers represent historical boot records, and 0 represents the current boot:IDX BOOT ID FIRST ENTRY LAST ENTRY-1 506feea161494cff925010ea7599ab5d Fri 2023-05-26 15:57:28 CST Fri 2023-05-26 16:00:10 CST0 59f4624ce2d94299be2afd80d3650c1a Fri 2023-05-26 16:00:18 CST Fri 2023-05-26 16:03:20 CST
journalctl command to manage system logs, including operations such as clearing and exporting logs.journalctl --vacuum-time=1s
journalctl --vacuum-time=1s --since "2024-01-01 00:00:00" --until "2024-01-02 00:00:00"
log.txt:journalctl > log.txt
<service-name> with the name of the service you want to export:journalctl -u <service-name> > service-log.txt
service-log.txt file.dnf package manager. Run the following command to update the software package index and install rsyslog:dnf install rsyslog #Install the rsyslog componentdnf update rsyslog #Update the rsyslog component
/etc/rsyslog.conf. In this file, you can define the sources, formats, and destinations of log messages, for example, configuring rsyslog to read logs from a specific Unix socket, read logs from journald, or send log messages to a remote server.vim /etc/rsyslog.conf:# Load modulesmodule(load="imuxsock") # Provides Unix socket supportmodule(load="imklog") # Provides kernel log support# Provide UDP syslog receptionmodule(load="imudp")input(type="imudp" port="514")# Provide TCP syslog receptionmodule(load="imtcp")input(type="imtcp" port="514")# Specify the location of the log fileauth,authpriv.* /var/log/auth.log*.*;auth,authpriv.none -/var/log/syslogdaemon.* -/var/log/daemon.logkern.* -/var/log/kern.log
systemctl restart rsyslog
cp /etc/rsyslog.conf /etc/rsyslog.conf.bak) so that you can quickly restore it if a configuration exception occurs. Restarting the rsyslog service briefly interrupts log collection, so confirm that it does not affect your business before proceeding.systemctl command to manage the rsyslog service, including starting, stopping, and restarting it.systemctl start rsyslogsystemctl stop rsyslogsystemctl restart rsyslog
systemctl enable rsyslog
systemctl status rsyslog
vim /etc/rsyslog.conf file, you can forward all local log messages to other servers through rsyslog. Add the following content to the configuration file:*.* @@192.168.1.1:514
*.* represents log messages from all facilities and all priorities, @@ indicates the use of a TCP connection, and 192.168.1.11:514 is the IP address and port of the remote server. The port must match the listening port configured in the remote server's rsyslog configuration. After modifying the configuration file, restart the rsyslog service for the changes to take effect.systemctl restart rsyslog
vim /etc/rsyslog.conf file, you can load specific modules and open specific ports to receive log messages from other servers. Add the following content to the configuration file:# Provide UDP syslog receptionmodule(load="imudp")input(type="imudp" port="514")# Provide TCP syslog receptionmodule(load="imtcp")input(type="imtcp" port="514")
systemctl restart rsyslog
imjournal module, rsyslog can obtain log data directly from systemd journal, replacing the traditional Unix domain socket input method (such as /dev/log). This mechanism enables rsyslog to directly access the native structured metadata of journal (such as _PID, _COMM, SYSTEMD_UNIT, and so on), thereby achieving efficient collection and structured processing of system logs.vim /etc/rsyslog.conf, add the following configuration to load the imjournal module (which is usually configured by default in TencentOS Server):module(load="imjournal" StateFile="imjournal.state")
systemd-journald ForwardToSyslog=yes option, you can forward Journal logs to rsyslog in real time. After this feature is enabled, journald pushes log messages to rsyslog through a Unix domain socket (usually /run/systemd/journal/syslog), allowing rsyslog to receive and process system logs in the traditional syslog protocol format, thereby achieving compatibility with existing syslog infrastructure and centralized log management.vim /etc/systemd/journald.conf, add or modify the following configuration to enable the ForwardToSyslog option (the default value is no):ForwardToSyslog=yes
systemctl restart systemd-journald
imjournal module in rsyslog and the ForwardToSyslog option in journald to ensure that all log messages can be correctly captured by rsyslog.vim /etc/rsyslog.conf file to forward all local journal log messages to other servers through rsyslog. Add the following content to the configuration file (using 192.168.1.1 as an example):*.* @@192.168.1.1:514
rsyslogd -N1 to pre-validate the configuration file. If no error is output after execution (only a validation completion message is returned), the configuration file syntax is valid. If a syntax error exists, the command returns the exact error line number and exception type, allowing you to directly locate and fix the issue./etc/systemd/journald.conf, run systemctl restart systemd-journald to restart the service and apply the new policy.--vacuum-size parameter (for example, keep only the most recent 500MB), or clean up by time window using the --vacuum-time parameter (for example, keep only the last two weeks of data) to quickly relieve storage pressure./etc/systemd/journald.conf configuration file and configure SystemMaxUse to limit the maximum disk usage of the journal, and configure SystemKeepFree to ensure that the file system always retains the specified minimum free space./etc/systemd/journald.conf, run systemctl restart systemd-journald to restart the service and apply the new policy.dnf install logrotate
/etc/logrotate.conf and in the /etc/logrotate.d/ directory. You can edit these files to configure log rotation rules./etc/logrotate.conf on the system is as follows:weeklyrotate 4createdateextinclude /etc/logrotate.d
Parameter | Description |
weekly | The log rotation interval is weekly. |
rotate 4 | A maximum of 4 backup files are retained. |
create | A new file is created for each rotation. |
dateext | Use the date as the prefix of rotation files. |
include /etc/logrotate.d | Include other configuration files in the /etc/logrotate.d directory. |
daily: Rotate log files every day.weekly: Rotate log files every week.monthly: Rotate log files every month.rotate N: Keep N log copies, where N is an integer.include /etc/logrotate.d directive merges all configuration files in that directory with the main configuration file /etc/logrotate.conf for execution. This means that the global rules defined in the main configuration file apply to all log files, while the individual configuration files in the /etc/logrotate.d/ directory can define specific rotation rules for particular services or applications./etc/logrotate.d directory:ls /etc/logrotate.d
btmp chrony dnf httpd iptraf-ng iscsiuiolog libvirtd libvirtd.qemu mariadb numad openstack-trove psacct rabbitmq-server wtmp
/var/log/myapp.log {rotate 7dailymissingoknotifemptycompressdelaycompresspostrotate/usr/bin/systemctl restart myapp.service > /dev/null 2>&1 || trueendscript}
/var/log/myapp.log. The details are as follows:rotate 7: Keep 7 old log files, and automatically delete any beyond that.daily: Rotate logs once a day.missingok: Ignore the error if the log file does not exist.notifempty: Do not rotate the log file if it is empty.compress: Compress old log files using gzip.delaycompress: Compress old log files during the next rotation.postrotate: Commands to be executed after rotation is complete, such as restarting a service.endscript: The end marker of the postrotate command.logrotate -f /etc/logrotate.conf
systemctl status logrotate.service
systemctl start logrotate.service
systemctl restart logrotate.service
/etc/logrotate.d/ directory, you can create a logrotate configuration file for a specific log.rsyslog for rsyslog, run the following command:touch /etc/logrotate.d/rsyslog
/var/log/cron/var/log/maillog/var/log/messages/var/log/secure/var/log/spooler{dailyrotate 7create}
/var/log directory are rotated daily, and log copies from the last 7 days are retained./etc/logrotate.conf or in the /etc/logrotate.d/ directory based on your actual requirements.フィードバック