Getty Images/iStockphoto
20 systemctl commands for system and service management
System and service management are vital to ensure customer satisfaction and service delivery. These systemctl commands can help ease those management tasks for admins.
More administrators are working in cloud environments than ever, and they need to conduct a number of system and service management tasks.
The systemctl command manages both system and service configurations, enabling administrators to manage the OS and control the status of services. Further, systemctl is useful for troubleshooting and basic performance tuning.
Here are 20 ways of using the systemctl command to better understand and administer Linux systems.
A quick syntax review
First, let's recap the proper use of the systemctl command. The syntax is the following:
systemctl subcommand argument
For example, to restart a service, type the following:
# systemctl restart {servicename}
Many systemctl subcommands 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 (normal Bash tab completion). This displays the complete list of subcommands.
Note: Many modern Linux distributions disable the root user account. In that case, admins may need to precede the following commands with sudo.
Admins use systemctl for two primary reasons: system management and service management.
System management
In this context, system management refers to the OS configuration. For systemctl, this includes shutdown and startup options.
1. Reboot
Two fundamental tasks for systemctl are rebooting and shutting down the server. To reboot, type the following:
# systemctl reboot
2. Shut down
To initiate a shutdown process based on advanced configuration and power interface, type the following:
# systemctl poweroff
3. Display default
It's common for Linux servers to boot to the command-line interface (CLI), which, in systemd's terminology, is the multi-user.target setting. In many cases, however, admins may prefer the GUI (graphical.target).
To display the current default, type the following:
# systemctl get-default
4. Change default to GUI
To change the current default from multi-user.target (CLI) to the GUI target, type the following:
# systemctl set-default graphical.target
5. Switch to multi-user.target
To switch to multi-user.target without changing the default from graphical.target, type the following:
# systemctl isolate multi-user.target
6. Switch to rescue mode
To switch to rescue mode for troubleshooting, type the following:
# systemctl rescue
Service management
While systemctl can help manage the system itself, it's even more useful for managing services. Admins can display active services, start and stop services, and control whether services start when the system boots. The systemctl command also has troubleshooting options.
7. See service status
There are many ways to display the status of services. In some cases, admins may wish to see information about all services, while, in other situations, they may only be interested in managing a single specific service. Either way, systemctl can help.
To see the status of all services, type the following:
# systemctl list-units --type=service
8. List services by 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
9. Start a service
A common task for admins is restarting services. Nearly any time admins make a change to a configuration file, the related service must be restarted so it can reread the configuration file and integrate the changes.
The systemctl command manually starts a service with the following command:
# systemctl start {servicename}
10. Stop a service
To manually stop a service with systemctl, type the following:
# systemctl stop {servicename}
11. Restart a service
Instead of manually starting or restarting a service, it's faster to simply use the restart subcommand:
# systemctl restart {servicename}
12. Prevent service from starting
A stopped, or disabled, service can still be started if another service calls it. To prevent a service from starting in any case, use the mask subcommand. This 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. What if admins need to configure the service to start when the system boots? The term for that action is enable:
# systemctl enable {servicename}
14. Disable a service
Likewise, if admins need to configure a service to not start when the system boots, they can type the following command to disable the service:
# systemctl disable {servicename}
15. Confirm active status
The systemctl command confirms the current and startup status of specific services by using the command below, as well as the is-enabled command:
# 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, note that it's best to use the stop subcommand whenever possible. By default, systemctl kill sends signal 15, or SIGTERM 15, which sends a request to kill the system 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
I like to include the systemd-analyze command in systemctl management scenarios. While this is a different command, it 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
Keep in mind that some services may be delayed while they wait for other services to load. Still, this can be useful information for determining which services are slowing down the system's startup time.
Last thoughts
Today's administrators manage more on-premises and cloud-hosted Linux systems than ever. Service management and monitoring are critical to ensuring the satisfactory delivery of resources to consumers. The systemctl command is one of the most useful tools available.
System and service management of Linux VMs is a key skill for any administrator that works with cloud environments. Become familiar with the many subcommands, and watch your Linux administration productivity skyrocket.