Linux systemctl Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the systemctl command, the primary tool for managing system services in Linux. You will understand the purpose and functionality of systemctl, learn how to manage system services using various systemctl commands, and configure automatic service startup. The lab covers the essential aspects of service management, including starting, stopping, enabling, and disabling services, as well as viewing their status and logs. This lab is designed to provide you with practical experience in system configuration and settings using the powerful systemctl command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") linux/SystemInformationandMonitoringGroup -.-> linux/service("`Service Managing`") subgraph Lab Skills linux/nano -.-> lab-422947{{"`Linux systemctl Command with Practical Examples`"}} linux/service -.-> lab-422947{{"`Linux systemctl Command with Practical Examples`"}} end

Understand the Purpose and Functionality of systemctl

In this step, you will learn about the purpose and functionality of the systemctl command, which is the primary tool for managing system services in Linux.

The systemctl command is a part of the systemd init system, which is the default init system used by many modern Linux distributions, including Ubuntu 22.04. Systemd provides a unified way to manage system services, allowing you to start, stop, enable, and disable services, as well as view their status and logs.

Let's start by exploring the basic 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 lists all the running services on the system. You can see the service name, their load, active, and sub states, as well as a brief description of each service.

To view the status of a specific service, you can use 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 provides detailed information about the sshd service, including its current state, when it was started, and the process ID of the running service.

You can also use systemctl to start, stop, 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 allow you to control the lifecycle of system services.

In the next step, you will learn how to use additional systemctl commands to manage system services.

Manage System Services Using systemctl Commands

In this step, you will learn how to use various systemctl commands to manage system services.

Let's start by enabling a service to start automatically on 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 creates a symbolic link to the service unit file, ensuring that the sshd service will start automatically when the system boots up.

To disable a service from starting automatically, you can use 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 start and stop a service manually:

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

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

These commands allow you to control the running state of the sshd service.

If you need to restart a service, you can use the restart subcommand:

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

This command will first stop the service and then start it again.

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

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

This command will display the current state of the sshd service, including whether it is running, when it was started, and any error messages.

In the next step, you will learn how to configure automatic service startup with systemctl.

Configure Automatic Service Startup with systemctl

In this final step, you will learn how to configure automatic service startup with systemctl.

Systemd provides a powerful way to manage service startup and dependencies. You can configure services to start automatically when the system boots up, or to start and stop based on the state of other services.

Let's start by creating a simple service unit file. Create a new file named myservice.service in the ~/project directory:

sudo nano ~/project/myservice.service

Add the following content to the file:

[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 simple service that runs an infinite loop, printing a message every 10 seconds. The After=network.target line ensures that the service starts after the network is available, and the Restart=always line ensures that the service is automatically restarted if it ever stops.

Now, let's enable and start 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 check the status of the service 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 shows that the myservice.service is running and has been enabled to start automatically on system boot.

You have now learned how to configure automatic service startup with systemctl. Congratulations on completing this lab!

Summary

In this lab, you first learned about the purpose and functionality of the systemctl command, which is the primary tool for managing system services in Linux. You explored how to list all running services, check the status of a specific service, and understand the detailed information provided about each service. Next, you learned how to manage system services using various systemctl commands, such as starting, stopping, restarting, and enabling services. Finally, you discovered how to configure automatic service startup with systemctl, ensuring that essential services are launched at system boot.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like