What are useful systemd commands?

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.

1. systemdctl

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.

2. journalctl

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.

3. hostnamectl

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.

4. timedatectl

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.

5. localectl

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.

6. loginctl

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.

7. systemd-analyze

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.

8. systemd-cgls

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.

9. systemd-cgtop

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.

10. systemd-delta

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.

11. systemd-path

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.

12. systemd-nspawn

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.

13. systemd-machine-id-setup

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.

14. systemd-escape

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.

15. systemd-cat

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.

16. systemd-notify

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.

17. systemd-tmpfiles

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 /usr/lib/tmpfiles.d/, /etc/tmpfiles.d/, and /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.

18. systemd-run

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.

19. systemd-mount

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.

20. systemd-ask-password

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.

21. systemd-inhibit

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".

22. systemd-dissect

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.

23. systemd-resolve

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-resolved.

24. systemd-firstboot

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.

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.

Support Xmodulo

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.

Xmodulo © 2023 ‒ AboutWrite for Us ‒ Feed ‒ Powered by DigitalOcean