Filter journalctl Logs by Service Name - Complete Guide

Learn how to efficiently filter systemd journal logs by service name using journalctl commands. Master log analysis with practical examples and techniques.

Filtering Logs by Service Name in journalctl

Introduction to journalctl

The journalctl command is a powerful utility for querying and displaying messages from the systemd journal. It provides a comprehensive interface to view, filter, and analyze system logs in Linux distributions that use systemd as their init system. The systemd journal is a centralized logging system that collects logs from various sources including the kernel, system services, and applications.

Understanding systemd Services

Before diving into filtering logs by service name, it's essential to understand what systemd services are and how they relate to logging:

What are systemd Services?

Systemd services are units that define how system processes should be started, stopped, and managed. Each service has a unique name and configuration that determines its behavior. Services can be system services (like networking, SSH daemon) or user services.

Service Naming Convention

Services in systemd follow a specific naming convention: - Service files end with .service extension - Service names are typically descriptive (e.g., ssh.service, apache2.service, mysql.service) - The .service suffix is often optional when referencing services

Basic journalctl Syntax

The basic syntax for journalctl is:

`bash journalctl [OPTIONS] [MATCHES] `

Where: - OPTIONS are command-line flags that modify the behavior - MATCHES are filters that specify which log entries to display

Filtering by Service Name

Using the -u Option

The most common and straightforward method to filter logs by service name is using the -u (unit) option:

`bash journalctl -u service_name `

Basic Examples

`bash

View logs for SSH service

journalctl -u ssh

View logs for Apache web server

journalctl -u apache2

View logs for MySQL database

journalctl -u mysql

View logs for NetworkManager

journalctl -u NetworkManager `

Including .service Extension

While the .service extension is optional, it can be explicitly included:

`bash

These commands are equivalent

journalctl -u ssh journalctl -u ssh.service `

Advanced Filtering Options

Time-based Filtering

You can combine service filtering with time-based filters to narrow down your search:

| Option | Description | Example | |--------|-------------|---------| | --since | Show entries after specified time | journalctl -u ssh --since "2024-01-01 10:00:00" | | --until | Show entries before specified time | journalctl -u ssh --until "2024-01-01 15:00:00" | | --since + --until | Show entries within time range | journalctl -u ssh --since "1 hour ago" --until "30 minutes ago" |

#### Time Format Examples

`bash

Absolute timestamps

journalctl -u ssh --since "2024-01-15 09:00:00" journalctl -u ssh --since "2024-01-15"

Relative timestamps

journalctl -u ssh --since "1 hour ago" journalctl -u ssh --since "yesterday" journalctl -u ssh --since "1 week ago" journalctl -u ssh --since "30 minutes ago"

Combined time filtering

journalctl -u ssh --since "2024-01-01" --until "2024-01-31" `

Priority Level Filtering

Filter logs by priority level using the -p option:

| Priority Level | Numeric Value | Description | |----------------|---------------|-------------| | emerg | 0 | System is unusable | | alert | 1 | Action must be taken immediately | | crit | 2 | Critical conditions | | err | 3 | Error conditions | | warning | 4 | Warning conditions | | notice | 5 | Normal but significant condition | | info | 6 | Informational messages | | debug | 7 | Debug-level messages |

`bash

Show only error and more severe messages for SSH service

journalctl -u ssh -p err

Show warning and more severe messages

journalctl -u ssh -p warning

Show only critical messages

journalctl -u ssh -p crit `

Output Control Options

| Option | Description | Example | |--------|-------------|---------| | -n | Show last N lines | journalctl -u ssh -n 50 | | -f | Follow logs in real-time | journalctl -u ssh -f | | -r | Show logs in reverse order | journalctl -u ssh -r | | --no-pager | Don't use pager | journalctl -u ssh --no-pager | | -o | Output format | journalctl -u ssh -o json |

Multiple Service Filtering

Filtering Multiple Services Simultaneously

You can filter logs from multiple services by specifying multiple -u options:

`bash

View logs from both SSH and Apache services

journalctl -u ssh -u apache2

View logs from multiple system services

journalctl -u NetworkManager -u systemd-resolved -u dhcpcd `

Using Pattern Matching

For services with similar names, you can use shell globbing:

`bash

View all systemd-related services (requires shell expansion)

journalctl -u 'systemd-*' `

Output Formats

The -o option allows you to specify different output formats:

| Format | Description | Use Case | |--------|-------------|----------| | short | Default syslog-style output | General viewing | | short-iso | Short format with ISO 8601 timestamps | Precise timing | | short-precise | Short format with microsecond precision | Detailed timing analysis | | verbose | Shows all available fields | Debugging | | export | Binary export format | Backup/transfer | | json | JSON format | Programmatic processing | | json-pretty | Pretty-printed JSON | Human-readable JSON | | cat | Only the message field | Clean message viewing |

Output Format Examples

`bash

JSON format for programmatic processing

journalctl -u ssh -o json

Pretty JSON format

journalctl -u ssh -o json-pretty

Verbose format showing all fields

journalctl -u ssh -o verbose

Cat format showing only messages

journalctl -u ssh -o cat

Short format with ISO timestamps

journalctl -u ssh -o short-iso `

Practical Examples and Use Cases

Troubleshooting Service Issues

`bash

Check recent SSH service errors

journalctl -u ssh -p err --since "1 hour ago"

Monitor Apache service in real-time

journalctl -u apache2 -f

View last 100 lines of MySQL logs

journalctl -u mysql -n 100

Check service status during system boot

journalctl -u NetworkManager --since "today" -o short-iso `

Security Analysis

`bash

Monitor SSH login attempts

journalctl -u ssh --since "today" | grep -i "authentication"

Check for failed SSH connections

journalctl -u ssh -p warning --since "1 week ago"

Monitor firewall logs

journalctl -u ufw --since "yesterday" `

Performance Monitoring

`bash

Check database service performance issues

journalctl -u mysql -p warning --since "1 day ago"

Monitor web server errors

journalctl -u apache2 -p err --since "6 hours ago"

Check system service restarts

journalctl -u systemd-resolved --since "1 week ago" | grep -i "start" `

Advanced Filtering Techniques

Using Field Matching

Instead of -u, you can use field matching for more precise filtering:

`bash

Filter by systemd unit

journalctl _SYSTEMD_UNIT=ssh.service

Filter by process ID

journalctl _PID=1234

Filter by user ID

journalctl _UID=1000

Combine multiple field matches

journalctl _SYSTEMD_UNIT=ssh.service _PID=1234 `

Available Fields for Filtering

| Field | Description | Example | |-------|-------------|---------| | _SYSTEMD_UNIT | Systemd unit name | _SYSTEMD_UNIT=ssh.service | | _PID | Process ID | _PID=1234 | | _UID | User ID | _UID=0 | | _GID | Group ID | _GID=100 | | _COMM | Command name | _COMM=sshd | | _EXE | Executable path | _EXE=/usr/sbin/sshd | | _HOSTNAME | Hostname | _HOSTNAME=server01 |

Boolean Operations

You can combine multiple filters using boolean logic:

`bash

Show logs from SSH OR Apache

journalctl _SYSTEMD_UNIT=ssh.service + _SYSTEMD_UNIT=apache2.service

The + operator acts as OR between different match expressions

journalctl _SYSTEMD_UNIT=ssh.service _PID=1234 + _SYSTEMD_UNIT=apache2.service `

Disk Usage and Journal Management

Checking Journal Disk Usage

`bash

Show current journal disk usage

journalctl --disk-usage

Show detailed journal file information

journalctl --list-boots `

Journal Maintenance

`bash

Remove old journal entries (keep last 2 days)

sudo journalctl --vacuum-time=2d

Remove old journal entries (keep last 100MB)

sudo journalctl --vacuum-size=100M

Remove old journal entries (keep last 10 files)

sudo journalctl --vacuum-files=10 `

Common Service Names Reference

System Services

| Service Name | Description | Alternative Names | |--------------|-------------|-------------------| | ssh | SSH daemon | sshd, openssh-server | | apache2 | Apache web server | httpd | | nginx | Nginx web server | - | | mysql | MySQL database | mysqld, mariadb | | postgresql | PostgreSQL database | postgres | | NetworkManager | Network management | network-manager | | systemd-resolved | DNS resolution | - | | cron | Task scheduler | crond | | rsyslog | System logging | syslog |

Desktop Services

| Service Name | Description | |--------------|-------------| | gdm | GNOME Display Manager | | lightdm | Light Display Manager | | bluetooth | Bluetooth service | | cups | Printing service | | avahi-daemon | Network discovery |

Error Handling and Troubleshooting

Common Issues and Solutions

#### Service Not Found

`bash

If you get "No journal files were found"

Check if the service name is correct

systemctl list-units --type=service | grep service_name

Check if journald is running

systemctl status systemd-journald `

#### Permission Denied

`bash

Some logs require root privileges

sudo journalctl -u ssh

Or add user to systemd-journal group

sudo usermod -a -G systemd-journal username `

#### Large Log Output

`bash

Use pager controls:

Space: Next page

b: Previous page

q: Quit

/: Search

n: Next search result

Or limit output

journalctl -u ssh -n 50 `

Best Practices

Performance Considerations

1. Use Time Filters: Always use --since and --until when possible to limit the search scope 2. Limit Output: Use -n to limit the number of lines when you don't need the full log 3. Use Specific Service Names: Be as specific as possible with service names 4. Regular Maintenance: Regularly clean up old journal entries to prevent disk space issues

Security Considerations

1. Access Control: Ensure proper permissions for accessing sensitive service logs 2. Log Rotation: Configure appropriate log retention policies 3. Monitoring: Set up automated monitoring for critical service errors

Scripting and Automation

`bash #!/bin/bash

Example script to check service health

SERVICE_NAME="ssh" TIME_RANGE="1 hour ago"

Check for errors in the last hour

ERROR_COUNT=$(journalctl -u "$SERVICE_NAME" --since "$TIME_RANGE" -p err --no-pager -q | wc -l)

if [ "$ERROR_COUNT" -gt 0 ]; then echo "Warning: $ERROR_COUNT errors found in $SERVICE_NAME service" journalctl -u "$SERVICE_NAME" --since "$TIME_RANGE" -p err --no-pager else echo "$SERVICE_NAME service is running without errors" fi `

Integration with Other Tools

Combining with grep

`bash

Search for specific patterns in service logs

journalctl -u ssh --no-pager | grep "Failed password"

Case-insensitive search

journalctl -u apache2 --no-pager | grep -i "error" `

Combining with awk

`bash

Extract specific fields

journalctl -u ssh -o short --no-pager | awk '{print $1, $2, $3, $NF}'

Count occurrences

journalctl -u ssh --no-pager | awk '/Failed password/ {count++} END {print "Failed logins:", count+0}' `

Exporting for Analysis

`bash

Export to file for analysis

journalctl -u ssh --since "1 week ago" --no-pager > ssh_logs.txt

Export in JSON format

journalctl -u ssh -o json --no-pager > ssh_logs.json `

Conclusion

Filtering logs by service name in journalctl is a fundamental skill for system administration and troubleshooting. The -u option provides the primary method for service-specific log filtering, while additional options like time filtering, priority levels, and output formats enhance the precision and usefulness of log analysis.

Understanding how to effectively use journalctl for service log filtering enables administrators to: - Quickly identify and diagnose service-specific issues - Monitor service performance and behavior - Implement automated monitoring and alerting systems - Maintain system security through log analysis

Regular practice with these commands and techniques will improve your efficiency in managing and troubleshooting Linux systems using systemd.

Tags

  • Linux
  • journalctl
  • logging
  • system-administration
  • systemd

Related Articles

Popular Technical Articles & Tutorials

Explore our comprehensive collection of technical articles, programming tutorials, and IT guides written by industry experts:

Browse all 8+ technical articles | Read our IT blog

Filter journalctl Logs by Service Name - Complete Guide