| by Arround The Web | No comments

How to Use systemctl Command to Enable and Disable Services

Systemd is one of the widely used service managers on Linux that comes with the systemctl command line utility to manage systemd services such as starting a service, stopping, enabling, and disabling it.

In this guide, I will explain how to enable a service on Linux using the systemctl command, and how to disable it.

What Does Enabling a Service Mean?

Enabling a service is a different feature from starting a service. The systemctl start command only starts the service and keeps it enabled until it is manually stopped before boot or the system is rebooted. On the other hand, enabling a service means the service will be started on boot.

When enabled, a service creates a symbolic link in the target directory, ensuring the service will be enabled on boot. The target is specified in the [Install] section of the service file with the WantedBy directive.

In the above image, the target is multi-user.target which indicates the run level of a system. The multi-user.target means the service will be enabled when the system has reached the state of providing multi-user non-graphical sessions.

How to Enable a Service on Linux

Before enabling a service, first, check if it is already enabled or disabled using the is-enabled option with systemctl.

sudo systemctl is-enabled [Service-Name]

To enable one or more services to start on boot, use the systemctl command with the enable option.

sudo systemctl enable [Service-Name]

In the above commands, replace the [Service-Name] with the name of the service or path of the service.

For instance, to enable the SSH service.

sudo systemctl enable ssh.service

On enabling, it creates a multi-user.target.wants directory in the /etc/systemd/system which contains the symlink to the service file.

Enabling a service using the systemctl enable command does not activate the service. To enable the service and immediately start it, use the enable and –now options.

sudo systemctl enable --now [Service-Name]

How to Re-Enable a Service on Linux

Re-enabling a service means disabling the service first and enabling it again. It removes the symlinks of the service and recreates them.

sudo systemctl reenable [Service-Name]

Let’s re-enable the SSH service using the above command.

sudo systemctl reenable ssh.service

As can be seen in the output, the symlinked files from the /etc/systemd/system directory are removed first and then created again. It does not start or stop the service; the service will remain in its original state.

Note that re-enabling only takes the service names and does not accept the paths.

How to Disable a Service on Linux

Use systemctl with the disable option to disable one or more services.

sudo systemctl disable [Service-Name]

It does not take the path of the service file.

For example, let’s disable the ssh service.

sudo systemctl disable ssh.service

Disabling the service will not stop the service, as it will continue to run unless it is stopped manually or the system is rebooted.

To disable and stop the service immediately, use the –now option with systemctl.

sudo systemctl disable --now [Service-Name]

Conclusion

To set a service to enabled on boot, the systemctl command is used with the enable option. It takes one or more service/unit names or paths. In the tutorial, I covered how to enable a service and how to re-enable a service. Moreover, I have also taken into consideration mentioning the disabling of the service commands. To learn more about the systemctl command line utility, use the man systemctl command.

Share Button

Source: linuxhint.com

Leave a Reply