How to View User-Level systemd Service Logs with journalctl: Permissions Fix for Ubuntu 16.04


2 views

When running user-level services under systemd (services defined in ~/.config/systemd/user/), you'll encounter permission issues when attempting to view logs with journalctl. The default configuration in Ubuntu 16.04 restricts access to user journals for security reasons.

The error message you're seeing occurs because:

journalctl --user -u test.service
Hint: You are currently not seeing messages from other users and the system.
  Users in the 'systemd-journal' group can see all messages.
No journal files were opened due to insufficient permissions.

This happens because your user account doesn't have proper access to the journal files stored in /var/log/journal/.

Option 1: Add User to systemd-journal Group

The simplest solution is to add your user to the systemd-journal group:

sudo usermod -aG systemd-journal $USER
# Then logout and login again for changes to take effect

Option 2: Enable Persistent Journal Storage

If the journal directory doesn't exist, you may need to create it first:

sudo mkdir -p /var/log/journal
sudo systemd-tmpfiles --create --prefix /var/log/journal
sudo systemctl restart systemd-journald

Option 3: Run journalctl with Elevated Privileges

For temporary access during debugging:

sudo journalctl --user -u test.service

After implementing any of the above solutions, you can use these commands:

# View logs for a specific user service
journalctl --user -u test.service

# Follow logs in real-time
journalctl --user -fu test.service

# Show logs since last boot
journalctl --user -b -u test.service

For reference, here's a sample user-level service:

[Unit]
Description=Test User Service

[Service]
ExecStart=/bin/bash -c 'while true; do echo "Service running"; sleep 10; done'

[Install]
WantedBy=default.target
  • Ensure Storage=persistent is set in /etc/systemd/journald.conf
  • Verify journal files exist: ls -l /var/log/journal/$(cat /etc/machine-id)/user-$(id -u).journal
  • Check user instance is running: systemctl --user status

When running user-level systemd services in Ubuntu, you'll encounter permission errors when trying to view logs with journalctl. The fundamental issue stems from how systemd handles user isolation and journal access permissions.

First, ensure your user is part of the necessary groups:

sudo usermod -a -G systemd-journal $(whoami)

Then enable persistent logging for user sessions:

sudo mkdir -p /var/log/journal
sudo systemd-tmpfiles --create --prefix /var/log/journal
sudo systemctl restart systemd-journald

Here's a sample user service unit file (~/.config/systemd/user/test.service):

[Unit]
Description=Test User Service

[Service]
ExecStart=/bin/bash -c 'while true; do echo "Test log entry $(date)"; sleep 10; done'
Restart=always

[Install]
WantedBy=default.target

After making these changes, you can properly view logs with several approaches:

Basic log viewing:

journalctl --user -u test.service

Follow logs in real-time:

journalctl --user -u test.service -f

Show logs with timestamps:

journalctl --user -u test.service -o short-precise
  • Ensure the user journal is enabled: sudo journalctl --verify
  • Check journal storage: sudo journalctl --disk-usage
  • If issues persist, try: systemctl --user daemon-reload

For production systems, consider adjusting journal retention:

# /etc/systemd/journald.conf
[Journal]
Storage=persistent
SystemMaxUse=1G
RuntimeMaxUse=200M
MaxRetentionSec=1month