systemctl: Total Control in Linux in 12 Steps

systemctl 12 commands that master Linux

systemctl: 12 commands that master Linux 🚀

The systemctl command has several features that are often overlooked. In addition to starting and stopping services in Linux, you can also list installed services and check their status. Let's do a quick review! 🚀

What Is the Systemctl Command?

The systemctl command is the central management tool for the systemd init system, primarily known as the tool used to start and stop services. But there's much more to it, as evidenced by its man page which has more than 1600 lines. 📖

Because systemctl is a management tool, not just a service launcher, you can use it to access useful information about your system and its systemd services. 💻

Most Linux distributions have adopted systemd, but some have opted to retain the traditional SystemV init system. If you're not sure which one your distribution uses, it's easy to find out. We'll use the stat command to look at the init file.

stat /sbin/init 
Using the stat command to see if a Linux installation uses SystemV or systemd.

The /sbin/init executable file is the first process launched in SystemV-based distributions. In systemd-based distributions, a symbolic link of that name points to the systemd file.

The first line of output shows us that on this Ubuntu test machine, /sbin/init is a symbolic link to the /lib/systemd/systemd file. Clearly, this Linux installation uses systemd. If this were a SystemV-based distribution, the line would simply contain “File: /sbin/init.”

Interrogating Services With systemctl

Services are defined in unit files, and you'll see the word "unit" scattered throughout systemctl options. As an example, we can obtain a list of services with the list-units command using the –type option.

systemctl list-units --type=service
The output of the systemctl list-units command, showing running and stopped services.

The output is displayed in the less file viewer, allowing you to navigate and use the / key to search.

  • Unit: The name of the unit file.
  • Carry: If the service's unit file was read into memory without syntax errors, this column will contain "loaded." This does not mean the service is active.
  • Asset: An overview of whether a service is active. An active service may not be running.
  • Sub: A more detailed view of whether a service is running. For example, an active service might be scheduled for a timer and may have exited its last run.
  • Description: A line of text intended to identify or describe the service.

The display only includes active services. To see all services, we need to include the –all option.

systemctl list-units --all --type=service
The output of the systemctl list-units command, showing all services.

If viewing everything is overwhelming, we can filter the output with the –state option.

systemctl list-units --type=service --state=running
The output of the systemctl list-units command, filtered to show only running services.

The state option will accept running, stopped, enabled, disabled, and failed.

To focus on failed services, use the –failed option.

systemctl list-units --failed
The output of the systemctl list-units command, filtered to show only failed services. There are no failed services.

There are no failed drives on this computer.

If you see any failed services, use the list-dependencies option to check for unmet dependencies.

systemctl list-dependencies sshd.service
The output of the systemctl list-dependencies command showing the dependencies for the sshd service.

Dependencies have a color-coded circle representing their status. This can be:

  • White Circle: Inactive or under maintenance
  • Green Dot: Asset.
  • White Point: Deactivating.
  • Red Dot: Failed or error.

To check if a single service is enabled, use the is-enabled command and provide the name of the service's unit file.

systemctl es-enabled htg-example.service
Using systemctl is-enabled to determine if a specific service is enabled.

Controlling Services With systemctl

Using systemctl to manage services is very simple and follows the same command format as the ones we've seen so far. The biggest difference is that you'll need to use sudo to make changes to service status. We haven't done this so far because we've only been reporting on service status.

To start a service, use the start command followed by the service name.

sweat systemctl start htg-example.service
Starting a service with the systemctl start command.

If all goes well, you'll be silently returned to the command prompt. If you'd prefer positive confirmation, you can verify this with the status command.

sweat systemctl status htg-example.service
Checking the status of a service with the systemctl status command.

Stopping a service is just as easy as starting it.

sweat systemctl stop htg-example.service
Stopping a service with the systemctl stop command.

You can restart a service without having to go through the two-step process of stopping and then starting it. restart command does it all for you.

sweat systemctl restart htg-example.service
Restarting a service with the systemctl restart command.

If you want a service to start at boot, you need to enable it.

sweat systemctl enable htg-example.service
Enabling a service with the systemctl enable command.

Note that this only specifies the service to start at boot; it doesn't start it immediately. If that's what you want, add the --now option.

sweat systemctl enable --now htg-example.service
Enabling and starting a service at the same time with the systemctl enable --now command.

When you no longer need a service to start at boot, disable it.

sweat systemctl disable htg-example.service
Disabling a service with the systemctl disable command.

You can use the journalctl command, another part of systemd, to search for entries related to your service. The -u (unit) option allows you to specify the service you're interested in. With the -S (since) option, you can display entries that have occurred since the specified time.

journalctl -S "08:00:00" -or htg-example.service
Using the journalctl command to display system log entries related to a specific service.

Any tool that helps you obtain information about the operation Your Linux distribution's internals will be invaluable, both for day-to-day management and for troubleshooting and diagnosing issues. The systemctl command isn't a single tool. It's more like a treasure trove of specialized tools, and it's well worth getting familiar with. 🔧🛠️

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most voted
Online Comments
See all comments