AI Hints
Home›Linux Tutorial›Watching the System
Linux · Lesson 99 of 120

System Logs

What happened, and when.

⏱ 2 min read▶ Video at 2:12:35🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

Linux keeps a diary. Every login, every error, every service starting or stopping, gets written to a log file. When something goes wrong, the logs are where you find out why.

System Logs
From the video: System Logs
  • Linux keeps a diary
  • Logins, errors, services
  • Logs tell you why

The log folder

Log files live in slash var slash log. Auth dot log records logins, and sudo use. It's private, so reading it needs sudo. Tail shows the last few entries. A failed password, and Sam using sudo to run apt update.

Terminal
ls /var/log
README            apt       bootstrap.log  dpkg.log  journal  private
alternatives.log  auth.log  btmp           faillog   lastlog  wtmp
sudo tail -n 3 /var/log/auth.log
Jan  5 06:30:19 laptop sshd[909]: Failed password for root from 203.0.113.7 port 50333 ssh2
Jan  5 07:02:10 laptop sshd[910]: Failed password for oracle from 203.0.113.7 port 50370 ssh2
Jan  5 08:15:44 laptop sudo:      sam : TTY=pts/0 ; PWD=/home/sam ; USER=root ; COMMAND=/usr/bin/apt update

Who is trying to get in?

Now, put your text skills to work. Grep finds the failed logins, and WC counts them. Eleven. Then awk pulls out the address after the word from, and sort with unique dash C counts them. Two addresses tried again and again. Those are attackers, guessing passwords.

Terminal
sudo grep "Failed password" /var/log/auth.log | wc -l
11
sudo grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn
      6 203.0.113.7
      4 198.51.100.4
      1 192.168.1.20

Logs worth knowing

A few logs come up again and again. Auth dot log for logins and sudo. Sys log for general system messages. And each service often has its own folder, like nginx for a web server. On system D machines, journal CTL reads them all in one place.

keyvalue
/var/log/auth.loglogins and sudo
/var/log/sysloggeneral messages
/var/log/nginx/a web server's logs
journalctlsystemd's log reader

Summary

Logs live in slash var slash log. Tail shows the newest entries, and grep, awk, sort, and unique turn them into answers. Next, a new section. Writing your own shell scripts.

Key takeaways
  • /var/log
  • sudo tail auth.log
  • grep | awk | sort | uniq -c
Next up: Your First Script — commands in a file