Last updated on March 17, 2023 by Dan Nanni
Systemd is a modern init system and system and service manager for Linux. It is designed to be compatible with the traditional System V and LSB (Linux Standard Base) init scripts while providing advanced features for managing system services and resources. It has become the default init system for many popular Linux distributions, such as Fedora, Ubuntu, Debian, openSUSE, and Arch Linux.
In this tutorial, I will go over some of the most useful systemd commands. I will explain what each systemd command does, as well as some example usage.
systemctl is a command-line tool that allows you to interact with the systemd system and service manager on systemd-enabled Linux. It provides a wide range of functionalities to control and manage services, units, and system states.
systemctl status [service]: Check the status of a service or unit.
systemctl start [service]: Start a service or unit.
systemctl stop [service]: Stop a service or unit.
systemctl restart [service]: Restart a service or unit.
systemctl reload [service]: Reload a service's configuration without restarting it (if supported).
systemctl enable [service]: Enable a service to start at boot.
systemctl disable [service]: Disable a service from starting at boot.
systemctl list-units: List all units (services, mounts, devices, etc.) managed by systemd.
systemctl list-unit-files: List all unit files and their enabled/disabled state.
systemctl daemon-reload: Reload the systemd configuration, required after modifying service files.
journalctl is the command for querying and displaying logs from the systemd journal on Linux systems. The systemd journal is a centralized, structured logging system that collects and stores log messages from various system components, including the kernel, services, and applications.
journalctl provides numerous options for filtering and viewing log entries, making it a powerful tool for troubleshooting and monitoring system events.
journalctl: Show the full journal log.
journalctl -u [service]: Show logs for a specific service or unit.
journalctl -f: Continuously show new log entries in real-time (follow mode).
journalctl --since "YYYY-MM-DD HH:MM:SS": Show log entries since a specific date and time.
journalctl --until "YYYY-MM-DD HH:MM:SS": Show log entries until a specific date and time.
journalctl -p [priority]: Show log entries with a specific priority (e.g., emerg, alert, crit, err, warning, notice, info, debug).
journalctl -k: Show kernel log messages.
journalctl --disk-usage: Show the total disk usage of the journal.
hostnamectl is a command-line utility for managing the hostname and related settings. It allows you to view and modify the system hostname, as well as configure the static, transient, and pretty hostnames. Additionally, it provides information about the current operating system, kernel, and system architecture.
hostnamectl: Show the current hostname and related information.
hostnamectl set-hostname [hostname]: Set the system hostname.
hostnamectl set-chassis [type]: Set the system chassis type (e.g., desktop, laptop, server, vm).
hostnamectl set-deployment [environment]: Set the system deployment environment (e.g., development, staging, production).
timedatectl is a command used to manage date, time, and time zone settings. It provides an interface for viewing and modifying the system clock, adjusting the time zone, and configuring the Network Time Protocol (NTP) for time synchronization.
timedatectl: Show the current time, date, and time zone information.
timedatectl set-time [time]: Set the system time.
timedatectl set-timezone [timezone]: Set the system time zone.
timedatectl set-ntp [true/false]: Enable or disable network time synchronization (NTP).
localectl is a command for managing system locale and keyboard layout settings. It allows you to view and modify the system's language, character encoding, and keyboard layout settings. This utility makes it easy to configure the appropriate locale and keyboard settings for your system, ensuring proper language support and input functionality.
localectl: Show the current locale and keyboard settings.
localectl set-locale [locale]: Set the system locale.
localectl list-locales: List available locales.
localectl set-keymap [keymap]: Set the system console keymap.
localectl list-keymaps: List available console keymaps.
localectl set-x11-keymap [layout] [model] [variant] [options]: Set the X11 (graphical) keymap. Specify layout, model, variant, and options as needed.
loginctl is a command for managing user sessions and seats. It provides an interface to interact with and control various aspects of user sessions, such as listing active sessions, showing user and session status, terminating sessions, and locking or unlocking sessions.
loginctl: List user sessions.
loginctl user-status [user]: Show the status of a specific user.
loginctl show-session [session_id]: Show detailed information about a specific session.
loginctl show-user [user]: Show detailed information about a specific user.
loginctl terminate-user [user]: Terminate all sessions for a specific user.
loginctl terminate-session [session_id]: Terminate a specific session.
loginctl lock-session [session_id]: Lock a specific session.
loginctl unlock-session [session_id]: Unlock a specific session.
systemd-analyze is a command for analyzing and diagnosing system boot performance and service dependencies within the systemd system and service manager. It provides various options to inspect the boot process, identify slow-starting services, and visualize the critical chain of units during the boot sequence. By using
systemd-analyze, you can gain insights into your system's boot performance and identify potential bottlenecks or issues that need to be addressed.
systemd-analyze: Show the time taken to boot the system.
systemd-analyze blame: Show a list of services sorted by the time taken to initialize them.
systemd-analyze critical-chain: Show a tree of the time-critical chain of units during boot.
systemd-analyze plot > boot.svg: Generate a graphical representation of the boot process, saved as an SVG file.
systemd-cgls is a command for displaying the hierarchy of control groups (cgroups) and their associated processes on Linux. Control groups are a kernel feature that allows you to organize, manage, and limit resource usage for groups of processes.
systemd-cgls provides an easy way to visualize the cgroup hierarchy in a tree-like structure, making it convenient to examine how resources are allocated and managed across different services and system components.
systemd-cgls: Show a tree view of all control groups and their processes.
systemd-cgtop is a command that shows the top control groups (cgroups) by their resource usage. It provides a real-time, dynamic view of resource consumption, such as CPU, memory, and I/O, for each cgroup. This utility is similar in concept to the
top command, but focuses specifically on the resource usage of cgroups.
systemd-cgtop: Display a real-time, top-like view of control groups and their resource usage (CPU, memory, and I/O).
systemd-delta is a command that analyzes and displays the differences between the vendor-supplied, user-supplied, and runtime systemd unit files. It helps you identify the differences and changes made to unit files, which can be useful for troubleshooting or understanding customizations in your system's configuration.
systemd-delta: Show the differences between vendor-supplied, user-supplied, and runtime unit files.
systemd-delta --type=masked: Show only masked units.
systemd-delta --type=extended: Show only units with extended configuration.
systemd-path is a command that displays the paths and directories used by the systemd system. It provides information about various system directories, such as configuration, state, cache, logs, and runtime data directories, among others. By using
systemd-path, you can easily identify and locate the directories used by systemd and the associated services, which can be helpful when working with configuration files, logs, or other system data. It can also help ensure that you are working with the correct paths when troubleshooting or customizing your systemd configuration.
systemd-path: Show the paths of various system directories (e.g., state, cache, logs, etc.).
systemd-path [name]: Show the path of a specific directory by name (e.g., systemd-path logs).
systemd-nspawn is a command that provides lightweight container functionality. It allows you to create, start, and manage isolated environments, called containers, by using the existing Linux kernel and systemd features such as cgroups, namespaces, and capabilities.
systemd-nspawn is not as feature-rich as Docker or LXC, but it offers a simple and straightforward approach to running containers for various purposes, such as testing, development, or sandboxing applications. It can start containers from directories or disk images, and it supports network isolation and resource management.
systemd-nspawn -D [directory]: Start a container with the specified directory as its root filesystem.
systemd-nspawn --image [image]: Start a container with the specified disk image as its root filesystem.
systemd-nspawn -bD [directory]: Start a container with the specified directory as its root filesystem and boot it using the contained init system.
systemd-nspawn --private-network: Start a container with its own private network namespace.
systemd-machine-id-setup is a command for initializing the
/etc/machine-id file on Linux. The
/etc/machine-id file contains a unique, 128-bit identifier for the system, which is used by various components of the operating system and applications to track the system and its state. The machine ID is intended to be unique for each system and remain unchanged throughout the system's lifetime, even if hardware components are replaced or the system is reinstalled. The machine ID is not meant to be a secret and can be used by applications for various purposes, such as tracking system usage or maintaining system-specific configurations. In general, you don't need to manually run
systemd-machine-id-setup, as it is automatically executed during the system installation or first boot. However, in some cases, you may need to run it to generate or regenerate the machine ID, such as when creating custom system images or cloning virtual machines.
systemd-machine-id-setup: Generate a new machine ID and store it in
systemd-escape is a command that helps you convert arbitrary strings into a format suitable for use as systemd unit names, file paths, or other identifiers. It is particularly useful when working with strings that contain special characters, whitespace, or other elements that are not allowed or can cause conflicts in systemd unit names.
systemd-escape [string]: Escape the specified string for use in a systemd unit name.
systemd-escape --unescape [string]: Unescape a previously escaped string.
systemd-cat is a command that allows you to send the output of any command or program directly to the systemd journal. This can be useful for capturing the output of short-lived processes or scripts in the systemd journal, making it easier to analyze, filter, and search for log entries related to those processes. By using
systemd-cat, you can pipe the output of a command or script directly to the journal, or you can run a command with
systemd-cat as a wrapper. This tool also supports adding custom metadata, such as log priority or identifier, to the captured log entries, which can help with organizing and filtering logs in the journal.
systemd-cat provides a simple and convenient way to integrate custom logging and output from various sources into the centralized systemd journal, enabling better log management and analysis.
systemd-cat [command]: Run the specified command and forward its output to the systemd journal.
systemd-notify is a command that enables services to send status updates, progress reports, or other notifications to the systemd system. It is particularly useful for implementing service readiness protocols, signaling service state changes, or reporting service-specific status information. Services can use
systemd-notify to send messages to systemd over a communication channel called the notification socket. This utility supports various message types, such as READY, STATUS, ERRNO, MAINPID, and WATCHDOG. For example, a service can use the READY message to inform systemd that it has successfully started and is ready to process requests. By using
systemd-notify, services can communicate with systemd more effectively, ensuring proper service management, monitoring, and coordination. This utility is particularly useful for long-running services or those with complex initialization procedures that need to signal their state to systemd.
systemd-notify --ready: Send a notification to systemd indicating that the service is ready.
systemd-tmpfiles is a command-line tool and associated configuration system for managing temporary files and directories on systemd-enabled Linux. It provides a standardized and flexible way to create, clean, and remove temporary files and directories during system boot, shutdown, or on a periodic basis.
systemd-tmpfiles uses configuration files located in
/run/tmpfiles.d/ directories to define the rules and actions for managing temporary files. These configuration files contain directives specifying the file paths, access permissions, ownership, and other attributes, as well as the cleanup policies, such as aging and size limits.
systemd-tmpfiles is usually run automatically by systemd at various stages of the system's lifecycle, but it can also be invoked manually to apply the specified actions, such as creating or cleaning temporary files and directories. By using
systemd-tmpfiles, you can ensure proper management and cleanup of temporary files and directories, helping maintain a clean and organized system, reducing the risk of data leaks, and conserving disk space.
systemd-tmpfiles --create: Create volatile files and directories as defined in the configuration files.
systemd-tmpfiles --clean: Clean up old, expired files and directories as defined in the configuration files.
systemd-run is a command that allows you to run a command or a script as a transient systemd service or scope unit on systemd-enabled Linux systems. Transient units are created dynamically at runtime and automatically removed when they are no longer needed or when the system is rebooted. This command provides a wide range of options for configuring the created units, including resource limits, dependencies, environment variables, and restart policies, among others.
systemd-run --unit=[name] [command]: Run the specified command as a transient service with the given name.
systemd-run --scope [command]: Run the specified command as a transient service in a new scope.
systemd-mount is a command-line utility that allows you to mount and manage file systems using the systemd system. It provides a convenient way to mount file systems as transient .mount units, which are automatically managed by systemd. The command also supports various options for configuring the mounted file systems, such as mount options, time-outs, and dependencies on other systemd units.
systemd-mount [device] [path]: Mount the specified device at the given path, creating a mount unit under systemd.
systemd-mount --umount [path]: Unmount the specified path, stopping the associated systemd mount unit.
systemd-ask-password allows you to query user passwords or passphrases in a secure and consistent manner on systemd-enabled Linux. This command is typically used by system services or scripts that require user authentication or secret input, such as mounting encrypted file systems or unlocking secure key stores.
systemd-ask-password works by displaying a password prompt to the user, either on the console, in a graphical dialog, or over a remote connection, depending on the environment and available password agents. The entered password or passphrase is then securely passed to the requesting service or script.
systemd-ask-password: Prompt the user for a password or passphrase with the given message.
systemd-inhibit allows you to execute a program or script while temporarily blocking specific system operations, such as shutdown, reboot, or sleep. It is particularly useful for ensuring that critical tasks or maintenance operations are not interrupted by a sudden system state change.
systemd-inhibit works by creating an "inhibitor lock" that prevents the specified system operations from taking place while the program or script is running. You can specify the type of inhibitor lock and a descriptive reason for its use. Common types of locks include "shutdown", "sleep", "idle", and "suspend".
systemd-inhibit --what=[operation] [command]: Run the specified command while inhibiting the specified operation (e.g., shutdown, sleep, idle).
systemd-dissect is a command-line tool that provides the functionality to inspect and analyze disk images and file system trees. It is particularly useful for examining disk images that contain operating system files, such as those used for containers, virtual machines, or system installation. By using
systemd-dissect, you can easily inspect and analyze disk images and file system trees, which can be helpful when working with containers, virtual machines, or creating custom system images. It provides a convenient way to examine the contents and structure of disk images without having to mount them manually or use multiple tools for different tasks.
systemd-dissect [image]: Show a brief summary of the contents of the specified disk or OS image.
systemd-dissect --mount [image]: Mount the specified disk or OS image, showing a summary of its contents.
systemd-resolve is a command that is part of the
systemd-resolved service, which provides network name resolution and caching on systemd-enabled Linux. The command allows you to query and manage the DNS resolver and cache provided by
systemd-resolve [hostname]: Resolve the specified hostname to an IP address using the system DNS resolver.
systemd-resolve --status: Show the current status of the system DNS resolver, including the configured DNS servers and search domains.
systemd-firstboot is a command that allows you to initialize various basic system settings on systemd-enabled Linux. It is typically used during the first boot of a newly installed system or when creating custom system images for deployment. The utility can be used in both interactive and non-interactive modes. In interactive mode,
systemd-firstboot prompts the user for the required information, while in non-interactive mode, the settings are provided via command-line options or configuration files.
systemd-firstboot --setup-machine-id: Set up the machine ID on the first boot.
systemd-firstboot --setup-root-password: Set the root password on the first boot.
These are some of the most useful
systemd commands for managing various aspects of your Linux system. Remember that most of these commands require administrative privileges, so prepend
sudo when necessary.
This website is made possible by minimal ads and your gracious donation via PayPal or credit card
Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.