# How do I read logs on a Linux server?

Read logs on a Linux server: follow a file with tail -f, search with grep -C and zgrep, and read systemd services with journalctl -u, --since and -f.

Source: https://valakes.com/learn/read-server-logs/

Last updated: 2026-11-25

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.

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

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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/check-disk-space/) 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:

```console
$ 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`:

```console
$ 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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/file-permissions/) explains the owner and group, and [sudo for one command](https://valakes.com/learn/sudo-for-one-command/) gives a safer way to read it with root rights.

## In Valakes

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](https://valakes.com/features/files/)

[Read the docs: Files](https://docs.valakes.com/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

- [tail(1) on Ubuntu 24.04, the options -n, -f and -F](https://manpages.ubuntu.com/manpages/noble/man1/tail.1.html)
- [grep(1) on Ubuntu 24.04, the options -n, -C and -c](https://manpages.ubuntu.com/manpages/noble/man1/grep.1.html)
- [zgrep(1) on Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/man1/zgrep.1.html)
- [journalctl(1) in the systemd manual](https://www.freedesktop.org/software/systemd/man/latest/journalctl.html)

## Related articles

- [How do I check disk space on a Linux server?](https://valakes.com/learn/check-disk-space/): Run df -h to see how full each file system is, then run du -h --max-depth=1 on the full one to find the folder that uses the space. df reads the file system itself and du adds up the files it can reach, so the two can disagree.
- [How do I find the largest files on a Linux server?](https://valakes.com/learn/find-large-files/): Run find /path -type f -size +100M to list the files above 100 MiB, or du -ah /path | sort -h | tail to see the biggest files and folders together. Add -xdev so find stays on one disk. If df says the disk is full and du cannot find the space, look for deleted files that a program still holds open with lsof +L1.
- [How do I check memory and CPU usage on a Linux server?](https://valakes.com/learn/memory-and-cpu-usage/): Run free -h and read the available column. It is the memory that new programs can still get without swapping. Then run ps -eo pid,user,rss,comm --sort=-rss to see which process holds the most, and sort by -pcpu to see which one burns the CPU.
- [How do I let a user run one command with sudo?](https://valakes.com/learn/sudo-for-one-command/): Write a rule that names the command with its full path and exact arguments, put it in its own file under /etc/sudoers.d/, check it with visudo -cf, and install it with mode 440. Do not use wildcards in the arguments, and do not allow editors or any program that can start a shell.
- [How do I change permissions recursively on Linux?](https://valakes.com/learn/file-permissions/): Do not use one number for the whole tree. Run find site -type d -exec chmod 755 {} + for folders and find site -type f -exec chmod 644 {} + for files, then chown -R to set the owner and group. chmod -R 777 lets every account on the machine change your files.
