Ubuntu

How to use systemctl in Ubuntu?

The “systemctl” refers to a command line utility whose main objective is to use and manage the “systemd” initialization services. It comes with a collection of daemons, utilities, and libraries that are helpful for managing services. The “systemd” system manager controls the system services (24/7) and manages settings in the form of configuration files.

This post pens down the use of the “systemctl” command in Ubuntu 22.04 with the following guidelines:

How to Use systemctl in Ubuntu?

The working of the “systemctl” command relies on its syntax.

Syntax:
The generalized syntax of the “systemctl” command is written here:

$ systemctl [Options...] [Command NAME...]

The above syntax contains the following parameters:

  • systemctl: Represents the main “systemctl” command.
  • Options: Helps to get the desired output.
  • Command Name: Specifies the service commands.

Let’s see the practical implementation of the above syntax for controlling “systemd” services.

Systemctl Enable Services

The “enable” service tool is used as a command with the “systemctl” to enable the particular service in Ubuntu:

Note: The “systemctl” command needs the superuser privileges, i.e., “sudo” to deal with the system services.

$ sudo systemctl enable cron.service

The “cron” service has been enabled now permanently.

Systemctl Disable Services

To disable, use the “systemctl” command with the “disable” option as seen below:

The output shows that the “cron” service has been disabled permanently and will never enable after the boot.

Systemctl Start Services

The service command “start” alongside the “systemctl” starts the services temporarily. It will go back into the previous status after booting up:

$ sudo systemctl start apache2.service

The “apache2” service has been started and is ready to use.

Systemctl Stop Services

Use the “stop” with the “systemctl” tool to stop the specified service for a short interval of time (temporarily):

$ sudo systemctl stop apache2.service

Systemctl Show Services Status

For checking the current working status and details of the services, execute the “systemctl” command combined with the “status/show” service utility:

$ sudo systemctl status apache2.service

The “apache2” service is enabled but stopped temporarily.

Systemctl Active Units List

The “systemd” uses the “units” which generally manage and control the task/action. Execute the “systemctl” command followed by the “list-units” service command and the “type” flag in this way:

$ systemctl list-units --type=service

Systemctl Specific Units State

Instead of all active units list the user can also list down the specified state of the units using the “state” flag of the “systemctl” command:

$ systemctl list-units --type=service --state=exited

The active “systemd” units having existing status have been displayed in the terminal.

Conclusion

In Ubuntu, the “systemctl” command is used to control and manage the “systemd” services with a combination of service commands. The service command includes “start”, “stop”, “enable”, “disable”, and “status” to manage, change, and check the status of the “systemd” services. This guide has enlisted a brief detail of the objective and usage of the “systemctl” command.

About the author

Adnan Shabbir