Chapter 11: Logs and time

Linux logs and time cheat sheet

The chapter on one page: what to remember, the commands to have at your fingertips, and flashcards for quick revision.

The chapter in eight sentences

Cheat sheet

Reading logs

CommandDoes
journalctl -u NAME -bA service, this boot
journalctl -p err -bErrors and worse
journalctl --since "1 hour ago"Time window
journalctl -f, -n 20, -g TEXTFollow, last, search
journalctl -k, dmesg -TKernel messages
grep Failed /var/log/secureAuthentication problems
last, lastbLogins, failed logins
logger -p local5.info "text"Write a message

Keeping logs

ItemDetail
Persistent journalmkdir /var/log/journal; systemctl restart systemd-journald; journalctl --flush
Limits/etc/systemd/journald.conf.d/10-X.conf: SystemMaxUse=
Clean upjournalctl --vacuum-size=100M · --vacuum-time=2weeks
rsyslog rule/etc/rsyslog.d/10-X.conf: local5.* /var/log/X.log; restart rsyslog
logrotate rule/etc/logrotate.d/X: weekly, rotate N, compress, create
Test / forcelogrotate -d FILE · logrotate -f FILE

Time

CommandDoes
timedatectlClock, zone, sync state
timedatectl list-timezonesZone names
timedatectl set-timezone Asia/ColomboSet zone
timedatectl set-ntp true|falseNTP on or off
chronyc sources / trackingSources / offset
/etc/chrony.confpool or server NAME iburst
systemctl restart chronydApply changes

Flashcards

journald versus rsyslog?
journald keeps a structured journal (journalctl); rsyslog writes plain text files in /var/log.
Log of one service, this boot, errors only?
journalctl -u NAME -b -p err
Follow the log live; filter by time?
journalctl -f · journalctl --since "1 hour ago" --until now
Which file holds logins and sudo records?
/var/log/secure
Write a test message to the logs?
logger -p user.notice "text"
Make the journal persistent?
mkdir /var/log/journal; systemctl restart systemd-journald; journalctl --flush
Add a custom log file for a facility?
/etc/rsyslog.d/10-NAME.conf with 'local5.* /var/log/NAME.log'; restart rsyslog
Test and force a log rotation?
logrotate -d FILE · logrotate -f FILE
Show clock, zone and sync state; set the zone?
timedatectl · timedatectl set-timezone Area/City
Check NTP sources and offset?
chronyc sources · chronyc tracking
Kernel messages?
dmesg -T · journalctl -k
Six-step diagnosis?
Symptom, status, log around the failure, widen, one change, verify.