systemd commands cheat sheet

systemd commands cheat sheet serves as an essential guide for system administrators and Linux users who manage services and system states with systemd. This comprehensive overview covers the most frequently used commands and options to control and monitor services, analyze logs, and manipulate system targets. Understanding these commands is crucial for efficiently handling startup processes, troubleshooting, and optimizing system performance. The systemd suite replaces older init systems, offering more granular control and faster boot times. This article details a variety of systemd commands, from basic service management to advanced troubleshooting utilities, providing a valuable resource for mastering systemd administration. The following sections will walk through service management, systemctl utilities, journalctl for logs, target and unit control, and miscellaneous commands that simplify systemd usage.

    • Service Management Commands
    • Systemctl Utilities
    • Journalctl: Viewing and Filtering Logs
    • Working with Targets and Units
    • Additional Essential systemd Commands

Service Management Commands

Managing services is one of the primary tasks when working with systemd. The systemctl utility is the core command for managing services, allowing administrators to start, stop, enable, disable, and check the status of systemd services efficiently. These commands help ensure that critical services are running and configured to start automatically when the system boots.

Starting and Stopping Services

The ability to start or stop services on demand is fundamental for system management. systemd commands cheat sheet highlights the core syntax for these actions using systemctl.

    • Start a service: systemctl start [service_name]
    • Stop a service: systemctl stop [service_name]
    • Restart a service: systemctl restart [service_name]
    • Reload service configuration: systemctl reload [service_name]

Enabling and Disabling Services

Controlling whether a service starts automatically at boot is vital for system configuration and security. The systemctl enable and disable commands handle this task.

    • Enable a service (start at boot): systemctl enable [service_name]
    • Disable a service (prevent auto-start): systemctl disable [service_name]
    • Check if service is enabled: systemctl is-enabled [service_name]

Checking Service Status

Verifying the current state of a service helps diagnose issues and ensure smooth operation.

    • Service status: systemctl status [service_name]
    • List all active services: systemctl list-units --type=service --state=running

Systemctl Utilities

Beyond basic service control, systemctl offers a variety of utilities to manage system states, reload configurations, and analyze dependencies. These commands are integral to mastering systemd’s powerful features.

Reloading systemd Manager Configuration

When unit files or configurations are changed, systemctl can reload the manager configuration without restarting the system.

    • Reload systemd manager configuration: systemctl daemon-reload

Power Management Commands

Systemd integrates with system power management, enabling controlled shutdowns, reboots, and suspends using systemctl commands.

    • Shut down the system: systemctl poweroff
    • Reboot the system: systemctl reboot
    • Put the system to sleep: systemctl suspend
    • Hibernate the system: systemctl hibernate

Listing and Managing Units

Units are systemd’s abstraction for resources such as services, sockets, devices, and mounts. Managing these units is essential for system administration.

    • List all loaded units: systemctl list-units
    • List failed units: systemctl --failed
    • Show detailed information about a unit: systemctl show [unit_name]

Journalctl: Viewing and Filtering Logs

systemd includes the journalctl command for querying and displaying system logs collected by the journal service. This tool is crucial for troubleshooting and monitoring system behavior.

Basic Log Viewing

Accessing system logs is straightforward with journalctl, which can display logs from the current boot or previous boots.

    • View all logs: journalctl
    • View logs from current boot: journalctl -b
    • Follow new log entries in real time: journalctl -f

Filtering Logs by Service and Time

Filtering logs by specific criteria helps isolate relevant information when diagnosing issues.

    • Filter logs by service: journalctl -u [service_name]
    • Show logs since a particular time: journalctl --since "YYYY-MM-DD HH:MM:SS"
    • Show logs until a particular time: journalctl --until "YYYY-MM-DD HH:MM:SS"

Advanced Log Options

Additional options enhance log readability and export capabilities.

    • Output logs in JSON format: journalctl -o json
    • Limit lines displayed: journalctl -n [number]
    • Show kernel messages only: journalctl -k

Working with Targets and Units

Targets in systemd define synchronization points that group units together, representing system states like multi-user, graphical, or rescue modes. Managing targets is essential for controlling system runlevels and boot processes.

Changing System Targets

Switching between targets allows transitioning the system into different operational modes without rebooting.

    • List available targets: systemctl list-units --type=target
    • Switch to a target: systemctl isolate [target_name]
    • Set default target for boot: systemctl set-default [target_name]

Understanding and Managing Unit Files

Unit files define how systemd manages resources. Editing and controlling these files is crucial for customized system management.

    • View unit file location: systemctl status [unit_name] (location shown in output)
    • Edit unit files: systemctl edit [unit_name]
    • Reload unit files after editing: systemctl daemon-reload

Additional Essential systemd Commands

Beyond core service and log management, several other systemd commands boost administrative efficiency and system diagnostics.

Masking and Unmasking Services

Masking a service prevents it from being started manually or automatically, providing a safeguard against unwanted execution.

    • Mask a service: systemctl mask [service_name]
    • Unmask a service: systemctl unmask [service_name]

Checking System and Service Dependencies

Understanding dependencies helps troubleshoot startup issues and analyze system behavior.

    • Show dependencies of a unit: systemctl list-dependencies [unit_name]
    • Show reverse dependencies: systemctl list-dependencies --reverse [unit_name]

Analyzing Boot Performance

systemd provides tools to measure and analyze the boot process, useful for optimizing boot times.

    • Display boot time summary: systemd-analyze
    • Show critical chain of units: systemd-analyze critical-chain
    • Plot boot sequence (SVG output): systemd-analyze plot > boot.svg

Frequently Asked Questions

What is systemd and why is it important?
systemd is a system and service manager for Linux operating systems that initializes the system and manages system processes after booting. It is important because it improves boot speed, manages services efficiently, and provides a standardized interface for service management.
How do I start, stop, and restart a service using systemd?
Use the following commands: Start a service with 'sudo systemctl start <service_name>', stop a service with 'sudo systemctl stop <service_name>', and restart a service with 'sudo systemctl restart <service_name>'.
How can I enable or disable a service to start at boot with systemd?
Enable a service to start at boot using 'sudo systemctl enable <service_name>' and disable it with 'sudo systemctl disable <service_name>'.
What command shows the status of a service in systemd?
Use 'systemctl status <service_name>' to view the current status, logs, and other information about the service.
How do I list all active services using systemd?
Run 'systemctl list-units --type=service --state=running' to display all currently active (running) services.
How can I view the logs of a service managed by systemd?
Use 'journalctl -u <service_name>' to view the logs related to a specific service.
What is the command to reload systemd manager configuration without rebooting?
Execute 'sudo systemctl daemon-reload' to reload systemd manager configuration after unit files have been changed.
How do I mask and unmask a service in systemd and what does it do?
Mask a service with 'sudo systemctl mask <service_name>' to prevent it from being started manually or automatically. Unmask it with 'sudo systemctl unmask <service_name>'.
How can I check which services are enabled to start at boot using systemd?
Run 'systemctl list-unit-files --type=service | grep enabled' to list all services enabled to start at boot.
What command do I use to reboot or shut down the system using systemd?
Use 'sudo systemctl reboot' to reboot and 'sudo systemctl poweroff' to shut down the system safely.