Home
How to Manage and Optimize Arch Linux With Systemd
Systemd serves as the cornerstone of the Arch Linux ecosystem, functioning as both the init system and a comprehensive system manager. Since its adoption by Arch Linux years ago, it has fundamentally changed how users interact with their machines, replacing the traditional SysV init scripts with a more powerful, parallelized, and modular architecture. As the first process to run after the kernel boots (PID 1), systemd is responsible for bringing up the user space and managing every service, mount point, and socket that keeps the system functional.
For an Arch Linux user, mastering systemd is not just about learning a few commands; it is about understanding the orchestration of a modern Linux distribution. This exploration covers the architecture, practical management, and advanced optimization techniques of systemd.
Understanding the Systemd Architecture
The core philosophy of systemd revolves around the "Unit." Instead of treating services as isolated scripts, systemd treats everything as a resource with defined properties and dependencies.
The Concept of Units
In systemd, a unit file is a plain-text configuration that defines a specific system component. Arch Linux stores these files in two primary locations:
/usr/lib/systemd/system/: Default units provided by installed packages. These should never be edited directly./etc/systemd/system/: System-specific overrides and custom units. Files here take precedence over those in/usr/lib/.
Common unit types include:
- .service: Manages background daemons (e.g., NetworkManager, Docker).
- .socket: Enables socket-based activation, allowing a service to stay dormant until a connection is made.
- .target: Used for grouping other units or defining system states (e.g.,
graphical.targetfor a GUI environment). - .mount: Manages file system mount points, often generated dynamically from
/etc/fstab. - .timer: Provides scheduling capabilities, serving as a modern and more feature-rich replacement for cron.
The Role of PID 1
As PID 1, systemd is the "parent" of all processes. It uses Linux Control Groups (cgroups) to track processes. Unlike the old init systems, if a service spawns multiple child processes, systemd knows exactly which ones belong to that service, making it impossible for "zombie" processes to escape management when a service is stopped.
Essential Service Management with systemctl
The systemctl command is the primary interface for managing systemd. It is a Swiss Army knife for system administrators, allowing for real-time introspection and control.
Basic Unit Operations
Managing a service involves four primary actions: starting, stopping, enabling, and disabling.
- Starting and Stopping:
sudo systemctl start <unit>andsudo systemctl stop <unit>control the current session. - Persistence:
sudo systemctl enable <unit>creates a symbolic link so the service starts at boot.sudo systemctl disable <unit>removes that link. - The --now Flag: A common workflow in Arch is to enable and start a service simultaneously. Using
sudo systemctl enable --now <unit>saves time by performing both actions in one command.
Introspection and Status Checking
The most used command is likely systemctl status <unit>. On Arch, this provides a wealth of information:
- The active state (running, exited, or failed).
- The main PID.
- Recent log entries (via journald).
- The CGroup hierarchy.
If a unit fails, systemctl --failed provides a concise list of what went wrong during the boot process or current session. This is an essential first step in troubleshooting after a large system update.
Masking Units: The Ultimate Override
Sometimes, simply disabling a service isn't enough because another service might have it as a dependency and "pull" it into an active state. To prevent a unit from being started under any circumstances, use:
sudo systemctl mask <unit>
This links the unit file to /dev/null. To revert this, use systemctl unmask.
Masterful Logging with journalctl
One of the most significant advantages of systemd is the Integrated Journal (journald). It collects logs from the kernel, initrd, services, and stdout/stderr of all processes.
Efficient Log Retrieval
In my testing, the speed of journalctl surpasses traditional text-log grepping because the logs are stored in a structured binary format.
- Boot-specific Logs: Use
journalctl -bto see logs for the current boot. Usejournalctl -b -1to see logs from the previous boot—invaluable for diagnosing crashes. - Service Filtering:
journalctl -u <unit>isolates logs for a specific service. - Real-time Monitoring:
journalctl -fmimics thetail -fbehavior, showing new log entries as they arrive. - Time-based Queries:
journalctl --since "2023-10-01" --until "2023-10-02 12:00:00"or relative terms likejournalctl --since "1 hour ago".
Managing Journal Size
By default, the journal can grow quite large on Arch Linux. You can limit its size by editing /etc/systemd/journald.conf and setting SystemMaxUse=500M. Alternatively, you can manually vacuum the logs:
sudo journalctl --vacuum-size=500M
User-Level Service Management
Arch Linux excels at providing a per-user systemd instance. This allows users to run their own services (like a local web server, a syncing daemon, or a custom script) without requiring root privileges.
How to Use User Units
User units are stored in ~/.config/systemd/user/. To manage them, simply add the --user flag:
systemctl --user enable --now syncthing.service
These services start when the user first logs in and stop when the last session for that user is closed.
Lingering Services
If you want user services to start on boot and stay running even when you are logged out, you must enable "lingering" for your user:
sudo loginctl enable-linger <username>
This is particularly useful for headless Arch servers where you want to run user-managed Docker containers or media servers.
Engineering Custom Unit Files
Writing your own unit files is a common task for Arch users who want to automate their workflows. A well-crafted unit file ensures that your script runs with the correct dependencies and restarts automatically if it fails.
Anatomy of a Service File
Let's look at a typical custom service located at /etc/systemd/system/myscript.service: