Skills / Linux administration / Logs and troubleshooting
Linux administration, lesson 5 of 8
Logs and troubleshooting
When something breaks on a Linux server, guessing wastes hours. The system and its services write down what they are doing in logs, and reading them calmly is the most valuable troubleshooting skill an administrator has.
Where logs live:
- Most modern systems use systemd, whose journal you read with
journalctl - Traditional log files live in
/var/log/: for example/var/log/syslogor/var/log/messagesfor general system messages,/var/log/auth.logfor sign-ins and sudo use on many systems - Applications often keep their own, such as
/var/log/nginx/error.logfor the nginx web server
Commands to know:
journalctl -u nginxshows logs for one service; add-fto follow new lines live, and--since "1 hour ago"to narrow the timesystemctl status nginxshows whether a service is running and its last few log linestail -n 50 file.logshows the last 50 lines;tail -ffollows the file livegrep -i "error" file.logfinds lines mentioning errors, in any capitals
A troubleshooting method:
- Define the problem exactly: what fails, since when, for whom?
- Check the obvious: is the service running? Is the disk full (
df -h)? Is memory exhausted (free -h)? - Read the logs around the time it started. The first error is usually the cause; later errors are often consequences
- Change one thing at a time, and note what you changed
- Write down the cause and the fix, so the next person (often future you) is faster
A full disk causes a surprising number of strange failures, so df -h is always worth a look.
Try it: On any Linux machine or free online Linux terminal, run systemctl status for one service, then use journalctl to show its logs from the last hour.
The tutor gives hints, not finished answers. Log in to use it.