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/syslog or /var/log/messages for general system messages, /var/log/auth.log for sign-ins and sudo use on many systems
  • Applications often keep their own, such as /var/log/nginx/error.log for the nginx web server

Commands to know:

  • journalctl -u nginx shows logs for one service; add -f to follow new lines live, and --since "1 hour ago" to narrow the time
  • systemctl status nginx shows whether a service is running and its last few log lines
  • tail -n 50 file.log shows the last 50 lines; tail -f follows the file live
  • grep -i "error" file.log finds lines mentioning errors, in any capitals

A troubleshooting method:

  1. Define the problem exactly: what fails, since when, for whom?
  2. Check the obvious: is the service running? Is the disk full (df -h)? Is memory exhausted (free -h)?
  3. Read the logs around the time it started. The first error is usually the cause; later errors are often consequences
  4. Change one thing at a time, and note what you changed
  5. 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.

Check yourself

1. Which command shows the logs of one systemd service?

2. Many errors appear in a log. Which one usually points to the cause?

3. Which command quickly checks whether the disk is full?