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)If we want to save the logs on our machine, we could do
❯ ssh user@192.0.2.0 "journalctl -n5" > journald.exportThis has two problems:
- This would save the files in plain text, which depending on how many services the remote machine is running could end up being a considerable amount of data.
- This log format would also not be that easy to parse.
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"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"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,9MBoth export and json output formats would compress extremely well
Notes
- Another way to do this would be to just copy all the journal files file from
/var/log/journal, but the problem here is that there would be no way to filter only by the logs you want or by a certain date range