Exporting journald logs via ssh

I was interested in extracting journalctl logs from a remote machine into my laptop via ssh.

Here are a few ways to do it.

We can just run the journalctl command though ssh with

❯ ssh user@192.0.2.0 "journalctl -n2"
Okt 23 19:02:06 v1111111111111111111 sshd-session[31289]: Accepted publickey for user from 192.0.1.0 port 62734 ssh2: ED25519 SHA256:WfaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaQcNw
Okt 23 19:02:06 v1111111111111111111 sshd-session[31289]: pam_unix(sshd:session): session opened for user user(uid=10)
Print the last 5 logs on the remote machine

If we want to save the logs on our machine, we could do

❯ ssh user@192.0.2.0 "journalctl -n5" > journald.export
Print the last 5 logs on the remote machine

This has two problems:

Journalctl export formats

Journalctl provides an export mode, which allows exporting logs for easy consumption without any special tools.

❯ ssh user@192.0.2.0 "journalctl -n5 --export=output"
Fetch the last 5 logs on the remote machine with output format

However for consuming this data on an external applications, it's better to export as json

❯ ssh user@192.0.2.0 "journalctl -n5 --export=json"
Fetch the last 5 logs on the remote machine in json format

Compressing before transfer

Since the size of the logs can be quite big (depending on how busy the machine is), it would be convenient if we could compress the logs before sending them. We can do that just adding a gzip call to our command

> ssh user@192.0.2.0 "journalctl -n10000 --output=export | gzip"  | wc -c | numfmt --to=iec
857K

> ssh user@192.0.2.0 "journalctl -n10000 --output=export "  | wc -c | numfmt --to=iec
8,9M

> ssh user@192.0.2.0 "journalctl -n10000 --output=export | gzip"  | gzip -d | wc -c | numfmt --to=iec
8,9M
Comparing journald export size between gziped and not gziped exports

Both export and json output formats would compress extremely well

Notes

Sources