Grid Guides

Explore How System On Grid Can Work For You

Grid Guide Topics

How to Use Systemctl to Manage Systemd Services and Units

Table Of Contents


    ## Table of Contents

    – [Introduction](#introduction
    – [Service Management](#service-management
    – [System State Overview](#system-state-overview
    – [Unit Management](#unit-management
    – [Edit Unit Files](#edit-unit-files
    – [Adjust System State with Targets](#adjust-system-state-with-targets
    – [Conclusion](#conclusion

    ## Introduction

    In this guide, you are going to learn how to use the systemctl command to manage systemd services and units, manage and check statuses of the services, check the state of a system and work with the configuration files.

    Systemctl is the central management tool to control the init system and service manager. Systemd is an init system and service manager for Linux operating system. It is an upgrade for the traditional SysV init systems. It is well-known for its power, flexibility, and capability to help users do their job with minimal hassle possible and for its massive adoption as it can make administering services simpler.

    ## Service Management

    For all the service management tasks, the target unit is service units with .service suffix. But, you can leave .service suffix for most of the service management commands as systemd can know that you want to operate on a service.

    ## Start and Stop Services

    Use the start command to start a systemd service. You need to use sudo before the command if you are executing with a non-root account user.
    “`
    $ sudo systemctl start application.service
    “`
    Or, you can simply use the below as systemd knows to show services files for service management commands.
    “`
    $ sudo systemctl start application
    “`
    But, In this guide, we use .service suffix for better understanding and clarity.

    Use the stop command to stop a currently running service.
    “`
    $ sudo systemctl stop application.service
    “`
    ## Restart and Reload Services

    Use the restart command to restart a running service.
    “`
    $ sudo systemctl restart application.service
    “`

    Use the reload command if the application can reload its configuration files without restarting it.
    “`
    $ sudo systemctl reload application.service
    “`
    Use reload-or-restart command when you are not sure if the service can reload its configuration. This command will reload the available configuration or restart the service to pick up the new configuration.
    “`
    $ sudo systemctl reload-or-restart application.service
    “`
    ## Enable and Disable Services

    Use the enable command to start services automatically at boot.
    “`
    $ sudo systemctl enable application.service
    “`
    Use the disable command to stop services from starting automatically at boot.
    “`
    $ sudo systemctl disable application.service
    “`
    ## Check The Status of Services

    Use the status command to check the status of a service if it has any problems and you may be required to take actions to resolve the errors.
    “`
    systemctl status application.service
    “`
    Output:

    ![applicationservicestatus](https://grid.media/assets/images/application-service-status.png

    You can also check a specific state of a service using different commands. Use the is-active command to check if a service is currently running/active or not.
    “`
    systemctl is-active application.service
    “`
    Use the is-enabled command to check if a service is enabled or not.
    “`
    systemctl is-enabled application.service
    “`
    Use the is-failed command to check if a service is in failed state or not.
    “`
    systemctl is-failed application.service
    “`
    ## System State Overview

    Now, you are going to learn a few systemctl commands that are useful to explore the current status of the system.

    ## Current Units

    Use the list-units command to know a list of all the active units that systemd knows.
    “`
    systemctl list-units
    “`
    Output:
    “`
    UNIT LOAD ACTIVE SUB DESCRIPTION
    atd.service loaded active running ATD daemon
    avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
    dbus.service loaded active running D-Bus System Message Bus
    dcron.service loaded active running Periodic Command Scheduler
    dkms.service loaded active exited Dynamic Kernel Modules System
    getty@tty1.service loaded active running Getty on tty1
    . . .
    “`

    You will get the same output if you call systemctl with no additional flags.
    “`
    systemctl
    “`
    You can get different types of information as output by adding additional flags. For example, if you want to know all the units that systemd has loaded and attempted to load into memory regardless of the current state of the unit (active/inactive, use –all flag.
    “`
    systemctl list-units –all
    “`
    Use the –state= flag to get only in-active units or active units.
    “`
    systemctl list-units –all –state=inactive
    “`
    The above command calls systemctl to display only the in-active units. Replace in-active with active to get only the active units.
    “`
    systemctl list-units –all –state=active
    “`
    And, by using –type= flag, you can call systemctl to display only the services which you are interested in.

    For example, use –type= flag to call systemctl to display only the active service units.
    “`
    systemctl list-units –type=service
    “`

    ### Listing All Unit Files

    Unlike list-units, list-unit-files displays every unit file available in the systemd path, including those that systemd has not attempted to load into memory.
    “`
    systemctl list-unit-files
    “`
    Output:

    ![unitfileslist](https://grid.media/assets/images/list-unit-files.png

    In the above output, you can see some of the unit files are in ‘static’ state. It means they don’t have an install section to enable a unit.

    ## Unit Management

    In this section, you are going to learn how to get precise information about a particular unit file using some additional commands.

    ## Display a Unit File

    Use cat command to display a unit file that systemd has loaded into its memory.

    For example, run the below command to display the unit file of the atd scheduling daemon.
    “`
    systemctl cat atd.service
    “`
    Output:

    ![displayunitfile](https://grid.media/assets/images/display-unit-file.png

    The above output is the unit file known to the currently running systemd process.

    ## Display Dependencies

    Use the list-dependencies command to display a unit’s dependency tree. It will display a hierarchy mapping dependencies that are required to start a unit in question. Here, dependencies include the units that are required by the units above it.
    “`
    systemctl list-dependencies sshd.service
    “`
    Output:

    ![sshdservice](https://grid.media/assets/images/sshd-service.png

    Add –reverse flag to the command to display reverse dependencies. And, add –before and –after flags to command to display a specific unit starting before and after themselves respectively.

    ## Check Unit Properties

    Use show command to display low-level properties of a unit. Using a key=value format, it will display a list of properties that are set for the specified unit.
    “`
    systemctl show sshd.service
    “`
    Output:

    ![checkunitproperties](https://grid.media/assets/images/check-unit-properties.png

    Use -p flag with the property name to display a single property. For example, use the below command to display the conflicts that the sshd.service unit has.
    “`
    systemctl show sshd.service -p conflicts
    “`
    Output:

    ![sshdserviceconflicts](https://grid.media/assets/images/sshd-service-conflicts.png

    ## Mask and Unmask Units

    The systemd can also mark a unit as absolutely unstartable by linking it to /dev/null. You can do this by using the mask command.
    “`
    sudo systemctl mask nginx.service
    “`
    In the above command, we have masked nginx services. It will prevent nginx service from starting either automatically or manually.

    If you try to start the nginx service, you can see the following output.
    “`
    sudo systemctl start nginx.service
    “`
    Output:

    ![sstartnginxservice](https://grid.media/assets/images/sstart-nginx-service.png

    Use the unmask command to unmask a specific unit.
    “`
    sudo systemctl unmask nginx.service

    “`

    ## Edit Unit Files

    The systemctl provides inbuilt mechanisms to edit unit files if you want to modify them. This feature is added in systemd version 218.

    Use edit command to open a unit file snippet for the unit.
    “`
    sudo systemctl edit nginx.service
    “`
    It will open a blank unit file to override or add directives to the unit definition. A directory will be created within the /etc/systemd/system directory with the .d affix. For example, nginx.service.d directory will be created for nginx.service directory. A snippet, override.conf, will be created within the directory. When the unit is loaded, the systemd will consolidate the snippet with the full unit file.

    Use –full flag to edit the full unit file instead of creating a snippet.
    “`
    sudo systemctl edit –full nginx.service
    “`
    The above command will open a current unit file in an editor, where you can edit it. The modified unit file will be written to /etc/systemd/system, which will take priority over the system’s unit definition (somewhere in /lib/systemd/system.

    Delete the unit’s .d configuration directory to remove any modifications you have made to the directory.
    “`
    sudo rm -r /etc/systemd/system/nginx.service.d
    “`
    Run the below command to remove a full modified unit file.
    “`
    sudo rm /etc/systemd/system/nginx.service
    “`
    After deleting the directory or file, reload the systemd process using the below command to no longer refer these files and revert for using the system files.
    “`
    sudo systemctl daemon-reload
    “`
    ## Adjust System State with Targets

    Target files are individual unit files that represent the state of a system. The files that define targets are identified by their suffix, .target. Target files are used to group other units.

    ## Get and Set the Default Target

    When booting the system, the systemd process has a default target.

    Use the set-default command to set a different default target.
    “`
    sudo systemctl set-default graphical.target
    “`
    Run the below command to display the default target for your system.
    “`
    systemctl get-default
    “`
    Output:

    ![systemctlgetdefault](https://grid.media/assets/images/systemctl-get-default.png

    ## List Available Targets

    Multiple targets can be active at one time. If a target is active, it means the systemd has attempted to start all the units that are tied to the target.

    Use the below command to display a list of available targets on your system.
    “`
    systemctl list-unit-files –type=target
    “`
    Use the below command to display all the active targets.
    “`
    systemctl list-units –type=target
    “`
    ## Isolate Target

    By using the isolate command, you can start all the units tied to a target and stop all that units that are not a part of the dependency tree.

    For example, if you are working in a graphical environment with graphical.target active, you can change the system into a multiuser command line by shutting down the graphical system and isolating the multi-user.target.

    And, make sure you are not stopping crucial services before isolating any target. Use the below command to display a list of dependencies of the multi-user.target.
    “`
    systemctl list-dependencies multi-user.target
    “`
    After checking the list of dependencies and satisfied with the units, you can isolate it using the below command.
    “`
    sudo systemctl isolate multi-user.target
    “`

    ## Use Shortcuts for Important Events

    The following are some of the systemctl shortcuts for essential events to enhance its functionality.

    Use rescue instead of isolate rescue.target to put the system into rescue.
    “`
    sudo systemctl rescue
    “`
    Use the below command to halt the system.
    “`
    sudo systemctl halt
    “`
    Use the below command to shut down the system.
    “`
    sudo systemctl poweroff
    “`
    Use the below command to reboot the system.
    “`
    sudo reboot
    “`
    ## Conclusion

    In this guide, you have learned how to use the systemctl command to manage systemd services and units, manage and check statuses of the services, check the state of a system and work with the configuration files.