How do I read logs on a Linux server?

Use tail -f to watch a log file live, grep -C 3 to see a match with the lines around it, and zgrep for the rotated copies that are compressed. For a service that logs to systemd, run journalctl -u name --since "1 hour ago". You need sudo or the right group for the journal.

Last updated

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), .

For a log file, run tail -f on it to watch it live and grep -n -C 3 error on it to read the matches with their neighbors. For a service that logs to systemd, use journalctl -u name, and add --since to cut the time range. Both ways are below, on a server where a toy service called myapp writes to both places.

Look at a log file

Logs sit in /var/log. This one has the current log, one rotated copy and two compressed ones:

$ ls /var/log/myapp
app.log  app.log.1  app.log.2.gz  app.log.3.gz

tail -n 3 prints the last three lines. Never open a big log in an editor; ask for the end:

$ tail -n 3 /var/log/myapp/app.log
2026-11-25 10:00:03 INFO  db connection restored
2026-11-25 10:00:09 INFO  GET /orders 200
2026-11-25 10:01:30 INFO  GET /health 200

tail -f prints the end and then waits for new lines, so you can watch while you try something in another window. It runs until you press Ctrl+C. Here timeout 2 ends it after two seconds so that the output fits on the page:

$ timeout 2 tail -f -n 2 /var/log/myapp/app.log
2026-11-25 10:00:09 INFO  GET /orders 200
2026-11-25 10:01:30 INFO  GET /health 200

tail -f follows the file that was open when it started. When a tool such as logrotate renames the log and starts a new one, tail -f stays with the renamed file and goes quiet. Use tail -F for logs that rotate. It follows the name, and it retries until the new file exists.

Search with grep

grep -n ERROR prints the lines with that word and their numbers:

$ grep -n ERROR /var/log/myapp/app.log
4:2026-11-25 09:59:40 ERROR db connection timeout after 5000 ms
5:2026-11-25 09:59:41 ERROR GET /orders 500

The cause of an error is usually in the lines before it, and the effect in the lines after. -C 1 adds one line of context on both sides, -B only before and -A only after. The match has a colon after its number, the context lines a dash:

$ grep -n -C 1 'db connection timeout' /var/log/myapp/app.log
3-2026-11-25 09:59:02 WARN  slow query 1840 ms
4:2026-11-25 09:59:40 ERROR db connection timeout after 5000 ms
5-2026-11-25 09:59:41 ERROR GET /orders 500

Add -i when you do not know the case. Quote a pattern that has a space in it.

Search the rotated logs

A rotation moves app.log to app.log.1, compresses older copies to .gz and starts a new file. The error you are looking for may be in one of them. Plain grep reads a compressed file as binary and does not find anything in it. The count below is wrong, and the command says nothing about it:

$ grep -c ERROR /var/log/myapp/app.log.2.gz
0

zgrep unpacks while it searches, and it also reads plain files, so one command covers all copies. -c counts, and with several files it names each one:

$ zgrep -c ERROR /var/log/myapp/app.log*
/var/log/myapp/app.log:2
/var/log/myapp/app.log.1:2
/var/log/myapp/app.log.2.gz:2
/var/log/myapp/app.log.3.gz:1

The disk filled up twice in the older copies. -h drops the file names from the output:

$ zgrep -h 'disk full' /var/log/myapp/app.log.*.gz
2026-11-23 14:55:36 ERROR disk full: cannot write /var/lib/myapp/cache
2026-11-22 03:12:50 ERROR disk full: cannot write /srv/backups/db.dump

If lines like these appear, go to the disk space article next.

Read the systemd journal

Services started by systemd usually write to the journal, not to a file. journalctl -u myapp shows the log of one unit. --no-pager prints to the screen instead of opening less, and -o cat prints the message without the time and host name. On a terminal journalctl also colors lines by priority. The examples here are shown without colors, so the first command switches them off. You do not need that line to follow along:

$ export SYSTEMD_COLORS=false
$ journalctl -u myapp --no-pager -o cat
No journal files were opened due to insufficient permissions.

A normal user may not read the system journal. Run it with sudo, or ask the administrator to add your account to the group systemd-journal or adm:

$ sudo journalctl -u myapp --no-pager -o cat
Started myapp.service - Toy service for the logs article.
GET /health 200
GET /orders 200
slow query 1840 ms
db connection timeout after 5000 ms
GET /orders 500
db connection restored
GET /orders 200

The first line comes from systemd itself. The others are the service’s output. Without -o cat each line starts with the time, the host and the program, which is what you want when you compare servers. The next commands narrow the list. -p err keeps messages of priority error and worse. --since and --until take a time such as 2026-11-25 10:00, yesterday or 1 hour ago. -g filters by a pattern:

$ sudo journalctl -u myapp -p err --no-pager -o cat
db connection timeout after 5000 ms
GET /orders 500
$ sudo journalctl -u myapp --since '30 minutes ago' -g timeout --no-pager -o cat
db connection timeout after 5000 ms
$ sudo journalctl -u myapp --since '2 hours ago' --until '1 hour ago' --no-pager
-- No entries --

An empty range prints -- No entries --, which is not an error. -f follows the journal as tail -f follows a file, and -n 2 first shows the last two lines. timeout 3 ends it here:

$ sudo timeout 3 journalctl -fu myapp -n 2 -o cat
db connection restored
GET /orders 200

Mistakes people make

You search only the current log. The error from last night is in a rotated copy.

You use tail -f on a log that rotates and wonder why nothing arrives after midnight. Use -F.

You run journalctl without sudo and read the permission line as “there are no logs”. Run it again with sudo before you decide.

You read the whole journal. Always narrow it with -u and --since.

You cannot read a file in /var/log and run sudo chmod on it. Check who may read it first; file permissions explains the owner and group, and sudo for one command gives a safer way to read it with root rights.

To read a log on your own computer, the Files panel in Valakes opens in the terminal's directory and downloads over SFTP. It has no sudo, so it reaches only the files your login can read. Feature: Files

Questions

What is the difference between tail -f and tail -F?

tail -f follows the open file. When logrotate renames the log, tail -f keeps reading the renamed one. tail -F follows the name and opens the new file, so it keeps working through a rotation.

Where is the log of my service?

Ask the journal first, with journalctl -u name. If it is empty, the service writes its own file, often under /var/log in a folder named after it. The service's own configuration says where.

How do I read a log that is too big to open in an editor?

Do not open it. Print the end with tail -n, or search it with grep and show only the lines you need.

References

The app is free.

Get notified at launch

macOS is coming to the Mac App Store. iOS and Android later.