Bookmark this page

Control System Services

Objectives

  • Control system daemons and network services with systemctl.

Start and Stop Services

You can manually start, stop, or reload services to update the service, update the configuration file, uninstall the service, or manually manage an infrequently used service.

Use the systemctl status command to verify the status of a service, if the service is running or stopped.

[root@host ~]# systemctl status sshd.service
● sshd.service - OpenSSH server daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-03-23 11:58:18 EDT; 2min 56s ago
...output omitted...

Use the systemctl start command as the root user (with the sudo command if necessary). If you run the systemctl start command with the service name only (without the service type), then the systemd service looks for .service files.

[root@host ~]# systemctl start sshd

To stop a running service, use the systemctl command stop option. The following example shows how to stop the sshd.service service:

[root@host ~]# systemctl stop sshd.service

Restart and Reload Services

When you restart a running service, the service first stops and then starts again. On the service restart, the new process gets a new ID during the startup and thus the process ID changes. To restart a running service, use the systemctl command restart option. The following example shows how to restart the sshd service:

[root@host ~]# systemctl restart sshd.service

Some services can reload their configuration files without requiring a restart, which is called a service reload. Reloading a service does not change the process ID that is associated with various service processes. To reload a running service, use the systemctl command reload option. The following example shows how to reload the sshd.service service after configuration changes:

[root@host ~]# systemctl reload sshd.service

If you are unsure whether the service has the function to reload the configuration file changes, use the systemctl command reload-or-restart option. The command reloads the configuration changes if the reloading function is available. Otherwise, the command restarts the service to implement the new configuration changes:

[root@host ~]# systemctl reload-or-restart sshd.service

List Unit Dependencies

Some services require other services to be running first, which creates dependencies on the other services. Other services start only on demand, rather than at boot time. In both cases, the systemd and systemctl commands start services as needed, whether to resolve the dependency or to start an infrequently used service. For example, if the printing system (CUPS) service is not running and you place a file into the print spool directory, then the system starts the CUPS-related daemons or commands to satisfy the print request.

[root@host ~]# systemctl stop cups.service
Warning: Stopping cups, but it can still be activated by:
  cups.path
  cups.socket

However, to stop all the printing services on a system, you must stop all three units. Disabling the service disables the dependencies.

The systemctl list-dependencies UNIT command displays a hierarchy mapping of dependencies to start the service unit. To list reverse dependencies (units that depend on the specified unit), use the --reverse option with the command.

[root@host ~]# systemctl list-dependencies sshd.service
sshd.service
● ├─system.slice
● ├─sshd-keygen.target
● │ ├─sshd-keygen@ecdsa.service
● │ ├─sshd-keygen@ed25519.service
● │ └─sshd-keygen@rsa.service
● └─sysinit.target
...output omitted...

Mask and Unmask Services

At times, some installed services on your system might conflict with each other. For example, many ways exist to manage mail servers (the postfix and sendmail services). Masking a service prevents an administrator from accidentally starting a service that conflicts with other services. Masking creates a link in the configuration directories to the /dev/null file, which prevents the service from starting. To mask a service, use the systemctl command mask option.

[root@host ~]# systemctl mask sendmail.service
Created symlink /etc/systemd/system/sendmail.service → /dev/null.

Then, verify the state of the service by using the systemctl list-unit-files command:

[root@host ~]# systemctl list-unit-files --type=service
UNIT FILE                                   STATE
...output omitted...
sendmail.service                            masked
...output omitted...

Attempting to start a masked service unit fails with the following output:

[root@host ~]# systemctl start sendmail.service
Failed to start sendmail.service: Unit sendmail.service is masked.

Use the systemctl unmask command to unmask the service unit.

[root@host ~]# systemctl unmask sendmail
Removed /etc/systemd/system/sendmail.service.

Important

You, or another unit file, can manually start a disabled service, but it does not start automatically at boot. A masked service does not start manually or automatically.

Enable Services to Start or Stop at Boot

Starting a service on a running system does not guarantee that the service automatically starts when the system reboots. Similarly, stopping a service on a running system does not prevent it from starting again when the system reboots. Creating links in the systemd configuration directories enables the service to start at boot. You can create or remove these links by using the systemctl command with the enable or disable option.

[root@root ~]# systemctl enable sshd.service
Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service → /usr/lib/systemd/system/sshd.service.

This command creates a symbolic link from the service unit file, usually in the /usr/lib/systemd/system directory, to the disk location where the systemd command looks for files, in the /etc/systemd/system/TARGETNAME.target.wants directory. Enabling a service does not start the service in the current session. To start the service and enable it to start automatically during boot, you can execute both the systemctl start and systemctl enable commands, or use the equivalent systemctl enable --now command.

[root@root ~]# systemctl enable --now sshd.service
Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service → /usr/lib/systemd/system/sshd.service.

To disable the service from starting automatically, use the systemctl disable command, which removes the symbolic link that was created when enabling a service. Disabling a service does not stop the service if it is currently running. To disable and stop a service, you can execute both the systemctl stop and systemctl disable commands, or use the equivalent systemctl disable --now command.

[root@host ~]# systemctl disable --now sshd.service
Removed /etc/systemd/system/multi-user.target.wants/sshd.service.

To verify whether the service is enabled or disabled, use the systemctl is-enabled command.

[root@host ~]# systemctl is-enabled sshd.service
enabled

Summary of systemctl Commands

You can start and stop services on a running system, and enable or disable them for an automatic start at boot time.

Table 9.3. Useful Service Management Commands

CommandTask
systemctl status UNIT View detailed information about a unit's state.
systemctl stop UNIT Stop a service on a running system.
systemctl start UNIT Start a service on a running system.
systemctl restart UNIT Restart a service on a running system.
systemctl reload UNIT Reload the configuration file of a running service.
systemctl mask UNIT Disable a service from being started, both manually and at boot.
systemctl unmask UNIT Make available a masked service.
systemctl enable UNIT Configure a service to start at boot time. Use the --now option to also start the service.
systemctl disable UNIT Disable a service from starting at boot time. Use the --now option to also stop the service.

References

systemd(1), systemd.unit(5), systemd.service(5), systemd.socket(5), and systemctl(1) man pages

For more information, refer to the Managing System Services with systemctl chapter in the Red Hat Enterprise Linux 9 Configuring Basic System Settings Guide at https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/configuring_basic_system_settings/index#managing-system-services-with-systemctl_configuring-basic-system-settings

Revision: rh124-9.3-770cc61