How to Enable and Disable Services in Debian 12 Bookworm

This article explains how to enable and disable services in Debian 12 Bookworm.

Introduction

Debian 12 “Bookworm” is a robust and reliable operating system widely used for servers, desktops, and embedded systems. Like other Linux distributions, Debian manages system services through systemd, a modern system and service manager. Controlling services efficiently is crucial for maintaining system performance, security, and stability.

This guide will cover the methods to enable, disable, start, stop, and check the status of services in Debian 12 Bookworm.


Understanding Services in Debian 12

A service (also called a daemon) is a background process that runs on a Linux system, handling various tasks such as networking, logging, and security monitoring. systemd is responsible for managing these services and has replaced older service managers like SysVinit and Upstart in modern Linux distributions.

Key Service States in systemd

When working with services in Debian 12, you need to understand their states:

  • Active (running) – The service is currently running.
  • Inactive (dead) – The service is not running.
  • Enabled – The service starts automatically at boot.
  • Disabled – The service does not start at boot.
  • Masked – The service is completely disabled and cannot be started.

Checking Service Status

Before enabling or disabling a service, it is helpful to check its status.

systemctl status <service-name>

For example, to check the status of the SSH service:

systemctl status ssh

This will display details about the service, including whether it is active, failed, or inactive.


Enabling a Service

To ensure that a service starts automatically at boot, you need to enable it. Use the following command:

sudo systemctl enable <service-name>

For example, to enable the Apache web server:

sudo systemctl enable apache2

This command creates symbolic links in the appropriate systemd directories to start the service at boot.

Verifying Enabled Services

To verify that a service is enabled, use:

systemctl is-enabled <service-name>

Example:

systemctl is-enabled apache2

If the service is enabled, it will return enabled. Otherwise, it will return disabled or masked.


Disabling a Service

If you do not want a service to start automatically at boot, disable it using:

sudo systemctl disable <service-name>

For example, to disable the Apache web server:

sudo systemctl disable apache2

This prevents the service from starting automatically but does not stop a currently running service.

Verifying Disabled Services

To confirm that a service is disabled, run:

systemctl is-enabled <service-name>

If the service is disabled, it will return disabled.


Starting a Service

If a service is not running and you need to start it manually, use:

sudo systemctl start <service-name>

For example, to start the Apache service:

sudo systemctl start apache2

Stopping a Service

To stop a running service, use:

sudo systemctl stop <service-name>

For example, to stop the Apache service:

sudo systemctl stop apache2

Restarting and Reloading a Service

Restarting a Service

If you need to restart a service to apply changes, use:

sudo systemctl restart <service-name>

Example:

sudo systemctl restart apache2

Reloading a Service

Some services support reloading their configurations without restarting:

sudo systemctl reload <service-name>

Example:

sudo systemctl reload apache2

This command is useful when applying new configurations without interrupting ongoing operations.


Masking and Unmasking Services

Masking a Service

Masking a service prevents it from being started manually or automatically:

sudo systemctl mask <service-name>

Example:

sudo systemctl mask apache2

If you attempt to start a masked service, it will fail.

Unmasking a Service

To allow a service to be started again, unmask it using:

sudo systemctl unmask <service-name>

Example:

sudo systemctl unmask apache2

Listing All Services

To list all services on the system, use:

systemctl list-units --type=service

To list only active services:

systemctl list-units --type=service --state=active

To list failed services:

systemctl --failed

Managing Services with SysVinit (Optional)

While systemd is the default service manager in Debian 12, some legacy services may still use SysVinit scripts. You can manage these services using the service command:

  • Start a service:

    sudo service <service-name> start
    
  • Stop a service:

    sudo service <service-name> stop
    
  • Restart a service:

    sudo service <service-name> restart
    
  • Check service status:

    sudo service <service-name> status
    

Conclusion

Managing services in Debian 12 Bookworm is straightforward with systemd. Knowing how to enable, disable, start, stop, restart, and check the status of services ensures that your system remains secure, efficient, and reliable. By mastering these commands, you can effectively control system services, optimize performance, and troubleshoot potential issues.