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 bothdmesg: the kernel's own messages about hardware and drivers- Searching logs with
grep,tailand 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:
- The journal, collected by systemd and read with
journalctl. It works the same on both families. - Plain text files in
/var/log, written by a program calledrsyslog. Here the file names differ:
| What | Rocky / RHEL | Ubuntu / Debian |
|---|---|---|
| General system messages | /var/log/messages | /var/log/syslog |
| Logins, SSH, sudo | /var/log/secure | /var/log/auth.log |
| Cron jobs | /var/log/cron | inside /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 sudo | root 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
sudo tail -n 20 /var/log/secure sudo grep "Failed password" /var/log/secure sudo tail -f /var/log/messages
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:
sudo grep "Failed password" /var/log/secure | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | sort | uniq -c
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
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.
dmesg | tail
dmesg -T | grep -i error # -T = human-readable times
Normal users are allowed to read it.
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:
- Is it running?
systemctl status NAME. The last few log lines appear right at the bottom. - What did it say?
journalctl -xeu NAMEfor the full story. - Read the error slowly. It usually names the file, the line number, and the problem.
- Fix one thing, then test the config before restarting. Apache has
httpd -t(Rocky) andapache2ctl configtest(Ubuntu). - Restart and verify:
sudo systemctl restart NAME, then test it for real (e.g.curl localhost).
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?
✓ Rocky calls it /var/log/secure. journalctl -u sshd works on both.
2. What does tail -f do?
✓ f = follow. Ctrl+C to stop.
3. A service won't start. What's the best first step?
✓ Read first, change second. The logs almost always tell you what's wrong.