How to Manage systemd Services in Debian 12 Bookworm
Categories:
3 minute read
Introduction
Debian 12 Bookworm, like its predecessors, relies on systemd as its default init system. systemd is a system and service manager for Linux that provides powerful tools for managing services, controlling system state, and improving boot performance. Understanding how to manage systemd services in Debian 12 is essential for administrators and power users.
In this guide, we will cover the key aspects of systemd service management, including starting, stopping, enabling, disabling, and monitoring services.
Understanding systemd Services
A systemd service is defined by a service unit file, typically found in /etc/systemd/system/
or /lib/systemd/system/
. These unit files define how a service should be started, stopped, restarted, and monitored.
A typical systemd service unit file looks like this:
[Unit]
Description=Example Service
After=network.target
[Service]
ExecStart=/usr/bin/example-daemon
Restart=always
User=example-user
Group=example-group
[Install]
WantedBy=multi-user.target
Now, let’s move on to managing services with systemd.
Managing Services with systemctl
Checking the Status of a Service
To check whether a service is running, use:
systemctl status <service-name>
For example, to check the status of the Apache web server:
systemctl status apache2
Starting and Stopping Services
To start a service manually:
systemctl start <service-name>
To stop a service:
systemctl stop <service-name>
For example:
systemctl start apache2
systemctl stop apache2
Restarting and Reloading Services
To restart a service:
systemctl restart <service-name>
To reload the configuration of a running service without restarting:
systemctl reload <service-name>
Example:
systemctl restart apache2
systemctl reload apache2
Enabling and Disabling Services
Enabling a service ensures that it starts automatically at boot time:
systemctl enable <service-name>
To disable a service from starting on boot:
systemctl disable <service-name>
Example:
systemctl enable apache2
systemctl disable apache2
Checking If a Service Is Enabled
To verify whether a service is enabled:
systemctl is-enabled <service-name>
For example:
systemctl is-enabled apache2
Masking and Unmasking Services
Masking a service prevents it from being started manually or automatically:
systemctl mask <service-name>
Unmasking a service allows it to be started again:
systemctl unmask <service-name>
Example:
systemctl mask apache2
systemctl unmask apache2
Viewing Logs with journalctl
systemd uses the journalctl
command to access logs generated by services.
Viewing Logs for a Specific Service
To view logs for a particular service:
journalctl -u <service-name>
For example:
journalctl -u apache2
Viewing the Most Recent Logs
To see only the latest logs:
journalctl -u <service-name> --no-pager | tail -n 50
Viewing Logs in Real-Time
To follow logs in real-time:
journalctl -u <service-name> -f
Managing systemd Unit Files
Editing a Systemd Unit File
To edit a service unit file:
sudo systemctl edit --full <service-name>
Make your changes and save the file. After editing, reload the systemd daemon:
sudo systemctl daemon-reload
Creating a Custom Service
To create a new custom service, create a unit file:
sudo nano /etc/systemd/system/myservice.service
Add the following content:
[Unit]
Description=My Custom Service
After=network.target
[Service]
ExecStart=/usr/local/bin/myscript.sh
Restart=always
User=root
Group=root
[Install]
WantedBy=multi-user.target
Then, enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable myservice
sudo systemctl start myservice
Troubleshooting systemd Services
Checking for Failed Services
To list all failed services:
systemctl --failed
Debugging Service Failures
If a service fails to start, check its logs with:
journalctl -u <service-name> --no-pager | tail -n 50
For detailed debugging, run:
systemctl status <service-name>
Restarting systemd Daemon
If you make changes to unit files, reload the systemd daemon:
sudo systemctl daemon-reload
Conclusion
Managing systemd services in Debian 12 Bookworm is straightforward with systemctl
and journalctl
. Whether starting, stopping, enabling, or troubleshooting services, systemd provides powerful tools to help administrators control their system efficiently. By mastering these commands, you can ensure smooth operation and quick recovery of critical services in your Debian environment.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.