Getty Images/iStockphoto

Tip

20 systemctl commands for system and service management

Linux administrators are overseeing more systems than ever. Managing system and service settings can be a challenge, but the systemctl command can make those tasks easier.

System and service management are key components of ensuring customer satisfaction and service delivery. The Linux systemctl command streamlines these management tasks for admins.

More Linux administrators are working in cloud environments than ever before, and they need to complete various system and service management tasks. The systemctl command manages both system and service configurations, enabling administrators to manage the OS and control service configurations. Additionally, systemctl is useful for troubleshooting and basic performance tuning.

This article presents 20 of the most common uses of the systemctl command, helping you understand its functionality and apply your knowledge to crucial configuration tasks.

A quick syntax review

First, let's recap the proper use of the systemctl command. The basic syntax pattern is the following:

systemctl subcommand argument

For example, to restart the sshd service, type the following:

# systemctl restart sshd

In this example, the subcommand -- also known as a parameter -- is restart. The argument is the {servicename} value, which is sshd (SSH) in this case. Common parameters include start, stop, restart and status.

Some parameters accept additional options. For example, you can specify the enable parameter and then add the --now option to cause it to start immediately, which enables you to skip using a separate systemctl start {service-name} command.

Many systemctl parameters exist, and this article covers only a handful. To see all available subcommands, try this trick: Type systemctl, press the spacebar once and then press the Tab key twice. This is normal Bash tab completion. This trick displays the complete list of subcommands or parameters.

Many modern Linux distributions disable the root user account. In that case, admins must precede the following commands with sudo.

The systemctl command is critical for Linux administrators responsible for system and service management tasks.

20 uses of the systemctl command

Let's evaluate 20 ways to use the systemctl command to understand and administer Linux systems better.

1. Start a service

A common task for admins is restarting services. Whenever admins modify a configuration file, they must restart the related service so it can reread the file and apply the changes.

The systemctl command manually starts a service with the following command:

# systemctl start {servicename}

2. Stop a service

To manually stop a service with systemctl, type the following:

# systemctl stop {servicename}

3. Restart a service

Instead of manually stopping and then starting a service, it's faster to use the restart subcommand:

# systemctl restart {servicename}

4. Reboot the system

Rebooting a server is a fundamental task for systemctl.

To reboot, type the following:

# systemctl reboot

5. Shut down the system

To initiate a shutdown process, type the following:

# systemctl poweroff

6. Display the default interface

It's common for Linux servers to boot to the CLI, which, in systemd terminology, is the multi-user.target mode. In many cases, however, admins might prefer the GUI (graphical.target).

To display the current default, type the following:

# systemctl get-default

7. Change the default interface to the GUI

To change the current default from multi-user.target CLI to the GUI target, type the following:

# systemctl set-default graphical.target

8. Switch to the multi-user.target interface

To switch to multi-user.target without changing the default from graphical.target, type the following:

# systemctl isolate multi-user.target

9. Switch to rescue mode

To switch to rescue mode for troubleshooting, type the following:

# systemctl rescue

10. Display service status

You can display the status of services in many ways. In some cases, admins might want to view information about all services. In others, they might only want to manage a single service. Either way, systemctl can help.

To see the status of all services, type the following:

# systemctl list-units --type=service

11. List services by current status

To list services by status, type the following:

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

Possible values for --state= include running, stopped, enabled, disabled and failed.

# systemctl list-units --failed

12. Prevent a service from starting

A service that is stopped or disabled can still be started if another service calls it. To prevent a service from starting in any case, use the mask subcommand. This setting links the service configuration to the /dev/null file.

# systemctl mask {servicename}

13. Enable a service

Starting and stopping a service only applies to the current runtime. If admins need to configure the service to start when the system boots, they can use the enable command for that action:

# systemctl enable {servicename}

14. Disable a service

Likewise, if admins need to configure a service not to start when the system boots, they can type the disable command:

# systemctl disable {servicename}

15. Confirm active status

The systemctl command confirms the current and startup status of specific services by using the command below with the is-enabled parameter:

# systemctl is-active {servicename}

16. Confirm enabled status

systemctl also uses the following command to confirm the status of specific services:

# systemctl is-enabled {servicename}

17. Kill a service with signal 15

Terminate services by using the kill subcommand. However, it's best to use the stop subcommand whenever possible. By default, systemctl kill sends signal 15, which sends a request to terminate the service and enables the system to clean up as it does so.

Here's the kill example for signal 15:

# systemctl kill {servicename}

18. Kill a service with signal 9

To force the system to kill a service immediately, admins can send signal 9 by typing the following command:

# systemctl kill -s 9 {servicename}

19. Analyze services

You might want to include the systemd-analyze command in systemctl management scenarios. While this is a different command, it still relates to service management.

The basic systemd-analyze command reports system boot time broken down into how long the kernel took to load before entering userspace and how long the userspace components took to load. This is a basic measure of startup time.

# systemd-analyze

20. Display service start times

In the context of services, filtering the systemd-analyze command by service startup time is even more useful. To see a list displaying service start times, type the following:

# systemd-analyze blame

Some services might be delayed while they wait for other services to load. Still, this can be helpful information for determining which services are slowing down the system's startup time.

Final thoughts

Today's administrators manage more on-premises and cloud-hosted Linux systems than ever. Service management and monitoring are crucial to ensuring the timely delivery of resources to consumers. The systemctl command is a key administration tool that enables essential system configuration tasks and service management. These elements are the core of any Linux system's role in both on-premises or cloud deployments.

Use the following best practices to get the most from the systemctl command:

  • Run systemctl commands using sudo privilege escalation.
  • Restart services after any configuration file changes.
  • Use the systemctl status {servicename} command for troubleshooting, configuration auditing and verifying service functionality.
  • Disable unnecessary services to reduce the system's attack surface.
  • Mask unnecessary services to prevent them from being accidentally started by other processes or users, providing a more secure and controlled configuration.
  • Use the systemctl --failed command to identify and troubleshoot failed units.

Managing system and service settings is a crucial skill for any Linux administrator.

Damon Garn owns Cogspinner Coaction and provides freelance IT writing and editing services. He has written multiple CompTIA study guides, including the Linux+, Cloud Essentials+ and Server+ guides, and contributes extensively to Informa TechTarget, The New Stack and CompTIA Blogs.

Next Steps

Linux performance monitoring utilities

Dig Deeper on Network management and monitoring