Linux Sysadmin · Lesson 4 · 20 min

Logs & troubleshooting

Servers keep a diary. Every login, crash, error and cron job gets written down. When something breaks, a good admin doesn't guess. They read the logs. It's the closest thing Linux has to a detective's notebook. 🕵️

You will learn

  • Where logs live on Rocky vs Ubuntu, and who is allowed to read them
  • journalctl, the modern log reader that works the same on both
  • dmesg: the kernel's own messages about hardware and drivers
  • Searching logs with grep, tail and pipes
  • A real troubleshooting routine: find why a web server won't start, then fix it

Two ways to read the diary

Modern Linux keeps logs in two places at once:

WhatRocky / RHELUbuntu / Debian
General system messages/var/log/messages/var/log/syslog
Logins, SSH, sudo/var/log/secure/var/log/auth.log
Cron jobs/var/log/croninside /var/log/syslog (search for CRON)
Software installs/var/log/dnf.log/var/log/apt/history.log
Web server/var/log/httpd//var/log/apache2/
Who can read them?Only root, so use sudoroot and the adm group. The first user made at install is in adm.

A log line always tells you when, which computer, which program [process ID], and what happened:

Sep 27 18:12:37 rocky sshd[2403]: Failed password for invalid user admin from 203.0.113.45 port 41822 ssh2
  when            host  program[PID]  message

Reading files: tail, grep, less

Rocky / RHEL
sudo tail -n 20 /var/log/secure
sudo grep "Failed password" /var/log/secure
sudo tail -f /var/log/messages
Ubuntu / Debian
tail -n 20 /var/log/auth.log
grep "Failed password" /var/log/auth.log
tail -f /var/log/syslog

tail -f (follow) keeps watching and prints new lines the moment they appear. Admins leave it running in one window while testing in another. Ctrl+C stops it.

Detective trick: count the attackers

Any server on the internet gets constant password-guessing from bots. Chain tools with pipes to find out who's knocking:

Rocky / RHEL
sudo grep "Failed password" /var/log/secure | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | sort | uniq -c
Ubuntu / Debian
grep "Failed password" /var/log/auth.log | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | sort | uniq -c

Read it left to right: find the failed lines → pull out just the IP addresses (grep -o prints only the match) → sort them so duplicates sit together → count them (uniq -c).

journalctl: one tool, both families

Same on both
journalctl -n 20                 # the last 20 lines of everything
journalctl -u sshd -n 20         # just one service (the unit)
journalctl -p err                # only errors and worse
journalctl --since "1 hour ago"
journalctl -f                    # follow live, like tail -f
journalctl -xeu httpd            # jump to the end of one service, with explanations

Remember that service names differ: -u sshd vs -u ssh, -u crond vs -u cron, -u httpd vs -u apache2.

dmesg: the kernel's diary

The kernel (the engine from Linux Basics, lesson 1) keeps its own log of hardware events: disks being found, USB sticks plugged in, network cards coming up, and serious errors like a disk failing or memory running out.

Rocky / RHEL
dmesg | tail
dmesg -T | grep -i error    # -T = human-readable times

Normal users are allowed to read it.

Ubuntu / Debian
sudo dmesg | tail
sudo dmesg -T | grep -i error

Ubuntu restricts it to admins: without sudo you get “Operation not permitted.”

The same messages are also in the journal: journalctl -k (kernel) works on both. You'll use dmesg to spot a brand-new disk in lesson 7.

The troubleshooting routine

When a service misbehaves, pros walk through the same steps every time:

  1. Is it running? systemctl status NAME. The last few log lines appear right at the bottom.
  2. What did it say? journalctl -xeu NAME for the full story.
  3. Read the error slowly. It usually names the file, the line number, and the problem.
  4. Fix one thing, then test the config before restarting. Apache has httpd -t (Rocky) and apache2ctl configtest (Ubuntu).
  5. Restart and verify: sudo systemctl restart NAME, then test it for real (e.g. curl localhost).
Remember AI Basics, lesson 1

Pasting an error message into an AI chat is a great way to understand it. But log lines can contain usernames and IP addresses, so remove anything private first.

Try it: the website is down! 🚨

Someone edited the web server's config last night and now the site won't start. Use the logs to find out why, then fix it. (Editing a file opens a mini editor: type, then Ctrl+O to save and Ctrl+X to exit, or use the buttons.)

Quick check

1. On Ubuntu, which file records SSH logins and sudo use?

2. What does tail -f do?

3. A service won't start. What's the best first step?

Finished the missions and the quiz? Mark it done to track your progress.