systemctl Command in Linux

Introduction

In this lab, you will gain hands-on experience with the systemctl command, the go-to tool for Linux system administration and managing system services. You'll delve into the purpose and core functionality of systemctl, master the art of managing system services through diverse systemctl commands, and learn how to automate service startup. This lab covers the fundamental aspects of service management, encompassing starting, stopping, enabling, and disabling services, as well as monitoring their status and logs. This is designed to give you practical experience in system configuration and settings by using systemctl.

Understand the Purpose and Functionality of systemctl

In this section, you'll explore the purpose and underlying functionality of the systemctl command, the cornerstone of system service management in Linux.

The systemctl command is an integral part of the systemd init system, now the standard init system in many modern Linux distributions, including Ubuntu 22.04. Systemd offers a streamlined method for managing system services, empowering you to start, stop, enable, and disable services, alongside the capability to inspect their status and logs.

Let's begin by examining the fundamental usage of systemctl:

## List all running services
sudo systemctl list-units --type=service

## Example output:
## UNIT                           LOAD   ACTIVE SUB     DESCRIPTION
## accounts-daemon.service        loaded active running Accounts Service
## acpid.service                  loaded active running ACPI event daemon
## apparmor.service               loaded active exited  AppArmor initialization
## ...

This command reveals all currently active services on your system. You'll observe the service name, its load state, active state, sub-state, and a concise description for each service.

To examine the status of a specific service, leverage the status subcommand:

## Check the status of the sshd service
sudo systemctl status sshd.service

## Example output:
## ● sshd.service - OpenSSH server daemon
##      Loaded: loaded (/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
##      Active: active (running) since Fri 2023-04-28 12:34:56 UTC; 1 day 2h ago
##        Docs: man:sshd(8)
##              man:sshd_config(5)
##    Main PID: 12345 (sshd)
##       Tasks: 1 (limit: 4915)
##      Memory: 12.8M
##      CGroup: /system.slice/sshd.service
##              └─12345 /usr/sbin/sshd -D

This output presents comprehensive details regarding the sshd service, including its current state, the time it was initiated, and the process ID of the active service.

You can also wield systemctl to initiate, halt, or restart a service:

## Start the sshd service
sudo systemctl start sshd.service

## Stop the sshd service
sudo systemctl stop sshd.service

## Restart the sshd service
sudo systemctl restart sshd.service

These commands grant you control over the lifecycle of system services.

In the upcoming section, you will discover how to harness additional systemctl commands for advanced system service management.

Manage System Services Using systemctl Commands

In this part, you will explore diverse systemctl commands to effectively manage system services.

Let's begin by configuring a service to automatically launch upon system boot:

## Enable the sshd service to start automatically on boot
sudo systemctl enable sshd.service

## Example output:
## Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service → /lib/systemd/system/sshd.service.

This command establishes a symbolic link to the service unit file, guaranteeing that the sshd service will initiate automatically each time the system boots.

To prevent a service from automatically starting, utilize the disable subcommand:

## Disable the sshd service from starting automatically on boot
sudo systemctl disable sshd.service

## Example output:
## Removed /etc/systemd/system/multi-user.target.wants/sshd.service.

Now, let's manually start and stop a service:

## Start the sshd service
sudo systemctl start sshd.service

## Stop the sshd service
sudo systemctl stop sshd.service

These commands empower you to dictate the running status of the sshd service.

Should you need to restart a service, employ the restart subcommand:

## Restart the sshd service
sudo systemctl restart sshd.service

This command will first halt the service and then promptly restart it.

Finally, you can scrutinize the status of a service using the status subcommand:

## Check the status of the sshd service
sudo systemctl status sshd.service

This command unveils the current state of the sshd service, indicating whether it's active, when it commenced, and any encountered error messages.

In the following section, you will discover how to orchestrate automatic service startup using systemctl.

Configure Automatic Service Startup with systemctl

In this final section, you will learn how to configure automatic service startup with systemctl, a vital aspect of system administration.

Systemd provides a robust mechanism for managing service startup and their dependencies. You can instruct services to automatically launch upon system boot, or to initiate and terminate based on the status of other services, enabling granular control over your system.

Let's initiate by creating a basic service unit file. Generate a new file named myservice.service within the ~/project directory:

sudo nano ~/project/myservice.service

Populate the file with the following content:

[Unit]
Description=My Custom Service
After=network.target

[Service]
ExecStart=/bin/bash -c "while true; do echo 'Running my service'; sleep 10; done"
Restart=always

[Install]
WantedBy=multi-user.target

This service unit file defines a straightforward service that executes an infinite loop, periodically printing a message every 10 seconds. The After=network.target directive ensures the service commences after the network is operational, and the Restart=always directive guarantees the service is automatically restarted if it unexpectedly terminates. This is common practice for Linux systemadmin tasks, ensuring stability.

Now, let's enable and launch the service:

## Enable the myservice.service to start automatically on boot
sudo systemctl enable ~/project/myservice.service

## Start the myservice.service
sudo systemctl start myservice.service

You can verify the service's status using the systemctl status command:

sudo systemctl status myservice.service

## Example output:
## ● myservice.service - My Custom Service
##      Loaded: loaded (/home/labex/project/myservice.service; enabled; vendor preset: enabled)
##      Active: active (running) since Fri 2023-04-28 12:34:56 UTC; 1 minutes ago
##    Main PID: 12345 (bash)
##       Tasks: 2 (limit: 4915)
##      Memory: 2.0M
##      CGroup: /system.slice/myservice.service
##              ├─12345 /bin/bash -c while true; do echo 'Running my service'; sleep 10; done
##              └─12346 sleep 10

This confirms that myservice.service is running smoothly and is configured to automatically launch during system boot. This can be very useful for a root user.

You've now mastered the art of configuring automatic service startup with systemctl. Congratulations on successfully completing this lab!

Summary

In this lab, you first grasped the purpose and functionality of the systemctl command, the primary tool for administering system services in Linux. You explored how to list all active services, scrutinize the status of a specific service, and decipher the detailed information provided for each service. Subsequently, you learned how to manage system services using a suite of systemctl commands, such as starting, stopping, restarting, and enabling services. Finally, you discovered how to configure automatic service startup with systemctl, ensuring that critical services are seamlessly launched upon system boot, and learned valuable skills for any aspiring systemadmin.

400+ Linux Commands