LinuxCommandLibrary

systemctl-service-log-target

Set systemd service log target

TLDR

Show the current log target for a service

$ systemctl service-log-target [service_name]
copy

Set the log target to console (send logs to stderr)
$ systemctl service-log-target [service_name] console
copy

Set the log target to journal (send logs to systemd-journald)
$ systemctl service-log-target [service_name] journal
copy

Set the log target to syslog (send logs to /dev/log)
$ systemctl service-log-target [service_name] syslog
copy

Allow systemd to choose an appropriate log target
$ systemctl service-log-target [service_name] auto
copy

Disable all log output
$ systemctl service-log-target [service_name] null
copy

SYNOPSIS

This is not a standalone command but a configuration directive within a systemd service unit file.

Example:
[Service]
LogTarget=VALUE

PARAMETERS

console
    Directs output to the system console or the controlling terminal of the service process. Visible on physical or virtual consoles.

journal
    Sends output to the systemd journal, which is the default and recommended logging mechanism for systemd services. This allows logs to be easily queried and managed using journalctl.

kmsg
    Routes output to the kernel message buffer. This is generally used for early boot messages or critical system events that need to be logged very early in the boot process.

syslog
    Forwards output to the traditional syslog daemon (e.g., rsyslog, syslog-ng), which can then process and store logs according to its own configuration.

null
    Discards all output, effectively sending it to /dev/null. Useful for services whose output is not required to be logged.

/path/to/file
    An absolute file path to which the output will be appended. This allows services to write their logs directly to a specific file on the filesystem. Ensure proper permissions and log rotation.

DESCRIPTION

The LogTarget= directive is a configuration option within systemd service unit files (typically found in the [Service] section). It defines the primary destination for the standard output and standard error streams of processes managed by the service. This setting provides granular control over where a service's runtime messages and errors are directed, which is crucial for monitoring, debugging, and system auditing.

By default, if not explicitly set, LogTarget= will direct output to the systemd journal when StandardOutput= is set to inherit, null, journal, kmsg, or syslog (or omitted). Otherwise, it defaults to console. Understanding and correctly configuring LogTarget= is essential for effective systemd service management and ensuring that logs are captured and available in the desired location for analysis.

CAVEATS

The behavior of LogTarget= is heavily influenced by other standard output/error directives like StandardOutput= and StandardError=. For instance, if StandardOutput=journal, then LogTarget= will generally default to journal. If a file path is specified, ensure the service has appropriate write permissions to that path and that log rotation is configured (e.g., via logrotate) to prevent disk exhaustion. Overuse of console can flood the console, making system debugging difficult. Using kmsg should be reserved for critical system-level events.

DEFAULT BEHAVIOR AND PRECEDENCE

If LogTarget= is not explicitly set in a service unit, its default value is derived from the setting of StandardOutput=. It defaults to journal when StandardOutput= is inherit, null, journal, kmsg, or syslog (or when StandardOutput= is omitted). In other cases (e.g., when StandardOutput=console or StandardOutput=file), it defaults to console. LogTarget= provides a final override for the output destination after StandardOutput= and StandardError= have been processed.

SECURITY CONSIDERATIONS FOR FILE PATHS

When using an absolute file path with LogTarget=, it is crucial to consider the security implications. Ensure that the specified directory is adequately protected and that the service user has only the necessary permissions to write to the log file. Avoid writing to world-writable locations or paths that could be exploited by malicious actors to inject code or fill the disk.

HISTORY

The LogTarget= directive is an integral part of systemd's comprehensive approach to service management and logging, introduced as part of the systemd init system. Systemd, which began development around 2010 and gained widespread adoption, aims to unify and simplify service configuration and logging across Linux distributions. Prior to systemd, logging mechanisms were often more fragmented, relying heavily on traditional syslog daemons and varied approaches for capturing service output. LogTarget=, along with other StandardOutput=/StandardError= settings, provides a structured and flexible way to integrate service logs into the systemd journal, aligning with systemd's goal of centralizing system information.

SEE ALSO

systemd.service(5), systemd.exec(5), systemctl(1), journalctl(1), logrotate(8)

Copied to clipboard