How to check if a systemd unit is enabled in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check the status of systemd units in Linux. You will use the systemctl is-enabled command to determine if a unit is configured to start automatically at boot, and the systemctl list-units command to list active units. Finally, you will explore the location of systemd unit files in the /etc/systemd directory. This lab will provide you with essential skills for managing services and understanding the systemd initialization process.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/FileandDirectoryManagementGroup -.-> linux/cd("Directory Changing") linux/TextProcessingGroup -.-> linux/grep("Pattern Searching") linux/SystemInformationandMonitoringGroup -.-> linux/ps("Process Displaying") linux/SystemInformationandMonitoringGroup -.-> linux/service("Service Managing") subgraph Lab Skills linux/ls -.-> lab-558771{{"How to check if a systemd unit is enabled in Linux"}} linux/cat -.-> lab-558771{{"How to check if a systemd unit is enabled in Linux"}} linux/cd -.-> lab-558771{{"How to check if a systemd unit is enabled in Linux"}} linux/grep -.-> lab-558771{{"How to check if a systemd unit is enabled in Linux"}} linux/ps -.-> lab-558771{{"How to check if a systemd unit is enabled in Linux"}} linux/service -.-> lab-558771{{"How to check if a systemd unit is enabled in Linux"}} end

Check unit status with systemctl is-enabled

In this step, you will learn how to check the status of systemd units using the systemctl is-enabled command.

Systemd is a system and service manager for Linux. It manages processes after the initial boot process. A "unit" in systemd refers to a configuration file that describes how a service, device, mount point, or other system resource should be handled.

The systemctl is-enabled command checks if a unit is configured to start automatically at boot time. This is important for services you want to run whenever the system starts.

Let's check the status of the ssh service. The SSH (Secure Shell) service allows you to connect to your server remotely.

Open your terminal if it's not already open. You can find the Xfce Terminal icon on the left side of your desktop.

Type the following command and press Enter:

systemctl is-enabled ssh

You should see output similar to this:

enabled

This output indicates that the ssh service is configured to start automatically when the system boots.

Now, let's check the status of a service that might not be enabled by default, like apache2 (a web server).

Type the following command and press Enter:

systemctl is-enabled apache2

You might see output like this:

disabled

This means the apache2 service is not configured to start automatically at boot.

Understanding whether a service is enabled or disabled is crucial for managing your system and ensuring that necessary services are running after a reboot.

Remember, the systemctl is-enabled command only tells you if a service is configured to start at boot, not if it is currently running. You'll learn how to check if a service is running in a later step.

Click Continue to proceed to the next step.

List units with systemctl list-units

In this step, you will learn how to list active systemd units using the systemctl list-units command.

While systemctl is-enabled tells you if a unit is configured to start at boot, systemctl list-units shows you which units are currently loaded and active in the system's memory. This is useful for seeing what services, devices, and other resources are currently being managed by systemd.

Open your terminal if it's not already open.

Type the following command and press Enter:

systemctl list-units

You will see a long list of units. The output typically includes columns like:

  • UNIT: The name of the unit.
  • LOAD: Whether the unit's configuration has been loaded into memory.
  • ACTIVE: The high-level unit activation state (e.g., active, inactive, failed).
  • SUB: The low-level unit activation state (e.g., running, exited, waiting).
  • DESCRIPTION: A brief description of the unit.

The list can be quite extensive. To make it more manageable, you can filter the output. For example, to see only service units (units ending in .service), you can use:

systemctl list-units --type service

This command will show you a list of all currently active services.

You can also use grep to search for specific units within the output. For instance, to find the ssh service:

systemctl list-units | grep ssh

This will show you the line corresponding to the ssh.service unit if it is active.

The systemctl list-units command is a powerful tool for understanding the current state of your system and the processes being managed by systemd. Experiment with different options and grep to find the information you need.

Click Continue to move on to the next step.

Inspect unit files in /etc/systemd

In this step, you will explore where systemd stores its unit configuration files and how to view their contents.

Systemd unit files, which define how services and other resources are managed, are typically located in the /etc/systemd/system/ directory. This directory contains unit files installed by the system administrator or packages.

Let's navigate to this directory and list its contents.

Open your terminal if it's not already open.

First, change your current directory to /etc/systemd/system/ using the cd command:

cd /etc/systemd/system/

Now, list the files in this directory using the ls command:

ls

You will see a list of .service, .target, and other unit files. These files contain the instructions for systemd on how to handle each unit.

Let's inspect the content of the ssh.service file using the cat command. Remember, you are currently in the /etc/systemd/system/ directory, so you can refer to the file directly by its name.

cat ssh.service

You will see the content of the ssh.service file. This file is a plain text file with different sections and directives that tell systemd how to start, stop, and manage the SSH service.

For example, you might see sections like [Unit], [Service], and [Install].

  • The [Unit] section contains general information about the unit and its dependencies.
  • The [Service] section defines the commands to execute to start and stop the service.
  • The [Install] section contains information about how the unit should be installed and enabled.

Understanding the structure and content of these unit files is key to advanced system administration with systemd. While the details can be complex, simply knowing where to find them and how to view them is a great start.

You can use less instead of cat to view the file content page by page, which is helpful for larger files:

less ssh.service

Press q to exit less.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check if a systemd unit is enabled to start automatically at boot using the systemctl is-enabled command. You practiced this by checking the status of the ssh and apache2 services, understanding that "enabled" means the unit will start on boot, while "disabled" means it will not.

You also learned how to list active systemd units using the systemctl list-units command, which provides a broader view of currently running or loaded units on the system. Finally, you explored how to inspect the configuration files for systemd units located in the /etc/systemd directory, gaining insight into how units are defined and configured.