Help Docs Server Administration Linux Server Administration Understanding and Using systemd

Understanding and Using systemd

Modern Linux service management with `systemd`. Learn `systemctl` to start, stop, enable & check services. Intro to `journalctl` logs.

This article provides an overview of systemd, a fundamental component of modern Linux distributions. You will learn what systemd is, why it’s important, and how to use its basic commands to manage services on your Liquid Web server.

What is systemd?

systemd is an init system and system and service manager for Linux operating systems. Think of it as the first process that starts after the Linux kernel boots, and it is responsible for initializing the system and managing various background processes, also known as daemons or services, throughout the system’s operation.

For many years, most Linux distributions used a traditional init system, often referred to as SysVinit. However, systemd was developed to provide several improvements, including:

  • Faster boot times: systemd can start services in parallel, significantly reducing the time it takes for your server to boot up.
  • Better dependency management: It has a more sophisticated way of handling dependencies between services, ensuring they start in the correct order.
  • Improved logging and monitoring: systemd includes its own logging system called the journal (journalctl), which provides a centralized and structured way to view system logs.
  • Modern features: It offers features like service monitoring and automatic restarting of crashed services, snapshotting the system state, and managing system resources.

Due to these advantages, systemd has been adopted by the vast majority of major Linux distributions, including CentOS, Ubuntu, Debian, Fedora, and AlmaLinux, effectively replacing older init systems. Understanding systemd is therefore essential for modern Linux server administration.

The primary command-line utility used to interact with systemd is systemctl.

Basic systemd uses: managing services

Services in systemd are defined by unit files, which typically end with the .service extension (e.g., httpd.service, mysqld.service). Here are the most common systemctl commands for managing these services:

1. Starting a service

To start a service, use the systemctl start command. For example, to start a hypothetical service named “foo”:

sudo systemctl start foo.service

Note: While adding .service is explicit, systemctl is often smart enough to infer it if you just type sudo systemctl start foo.

2. Stopping a service

To stop a running service, use the systemctl stop command:

sudo systemctl stop foo.service

3. Restarting a service

To stop and then immediately start a service again (useful for applying configuration changes), use systemctl restart:

sudo systemctl restart foo.service

4. Restarting a service only if it is running

If you want to restart a service but only if it’s currently active, use systemctl condrestart (conditional restart). This is often safer in scripts where the service might not always be running.

sudo systemctl condrestart foo.service

Alternatively, try-restart can be used, which is often an alias for condrestart.

5. Reloading a service’s configuration

Some services support reloading their configuration files without a full restart, which can prevent downtime.

sudo systemctl reload foo.service

If the service does not support reloading, this command might be ignored or result in an error. In such cases, a full restart is necessary.

6. Enabling a service to start on boot

To ensure a service automatically starts when your server boots up, you need to “enable” it:

sudo systemctl enable foo.service

This command creates symbolic links in the appropriate system directories that systemd uses to start services at boot.

7. Disabling a service from starting on boot

Conversely, to prevent a service from starting automatically at boot, you “disable” it:

sudo systemctl disable foo.service

This removes the symbolic links created by enable.

Informational systemd commands

These commands help you get information about your services and the systemd system itself.

1. Listing all loaded units (including services)

To see a list of all active unit files that systemd has loaded, including services, sockets, targets, etc., simply run:

systemctl

For a list of only active service units, you can use:

systemctl list-units --type=service

To list all installed unit files (enabled or disabled):

systemctl list-unit-files

2. Checking the status of a service

To get detailed information about a specific service, including whether it’s running, its Process ID (PID), recent log entries, and its enabled status, use systemctl status:

systemctl status foo.service

Example output:

● foo.service - My Example Service
     Loaded: loaded (/etc/systemd/system/foo.service; enabled; vendor preset: disabled)
     Active: active (running) since Thu 2025-05-29 14:20:00 EDT; 5min ago
   Main PID: 12345 (foo_process)
      Tasks: 1 (limit: 11101)
     Memory: 10.5M
        CPU: 15ms
     CGroup: /system.slice/foo.service
             └─12345 /usr/bin/foo_process

May 29 14:20:00 server.example.com systemd[1]: Started My Example Service.
May 29 14:20:01 server.example.com foo_process[12345]: Foo service initialized.

Key information from the status output:

  • Loaded: Shows the path to the service unit file and whether it’s enabled to start at boot.
  • Active: Indicates if the service is currently active (running), inactive (dead), activating, deactivating, or in a failed state.
  • Main PID: The main process ID of the service.
  • Recent log entries: The last few lines from the journal related to this service.

3. Checking if a service is enabled

To quickly check if a service is configured to start on boot, without viewing the full status:

systemctl is-enabled foo.service

This command will output enabled, disabled, static (cannot be enabled/disabled but is implicitly part of boot), masked (completely disabled, cannot be started manually), or other states.

4. Checking if a service is active (running)

To quickly determine if a service is currently running:

systemctl is-active foo.service

This will output active, inactive, or activating.

5. Checking if a service has failed

To see if a service has entered a failed state:

systemctl is-failed foo.service

This will output active (if not failed), failed, inactive, or activating.

Other useful systemd concepts and commands

  • Targets: systemd uses “targets” which are similar to runlevels in older init systems. A target is a group of units that are started together. For example, multi-user.target is a common target for a server environment. You can change targets using systemctl isolate multi-user.target. The default target is usually a symbolic link like /etc/systemd/system/default.target.
  • Journaling with journalctl:systemd has its own logging system, the journal. The journalctl command is used to query and display these logs.
    • View all logs (may be very long): sudo journalctl
    • View logs in real-time (like tail -f): sudo journalctl -f
    • View logs for a specific service: sudo journalctl -u foo.service
    • View logs since a specific time: sudo journalctl --since "1 hour ago" or sudo journalctl --since "2025-05-29 10:00:00"
    • View kernel messages: sudo journalctl -k
  • Masking services: If you want to completely prevent a service from starting, even manually, you can “mask” it. This links the service file to /dev/null.

To mask:

sudo systemctl mask foo.service

To unmask:

sudo systemctl unmask foo.service

Conclusion

systemd is a powerful and essential tool for managing your Linux server. While it has many advanced features, mastering these basic systemctl commands will allow you to effectively control and monitor the services running on your Liquid Web server. For more in-depth information, you can always refer to the manual pages by typing man systemctl, man systemd, or man journalctl in your server’s command line.

Was this article helpful?