Skip to main content
Engineering LibreTexts

08-C.8: Managing Linux Services

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    2.4 Given a scenario, manage services.
    4.1 Given a scenario, analyze system properties and remediate accordingly.

    Daemons and Services

    Services are essential processes that usually run in the background, rather than being under the direct control of an interactive user, waiting for requests from other software programs, or to carry out essential tasks at the appropriate time. Many services are implemented as daemons.

    The video talks about the Linux systemctl command and how to control system services.

    In Linux, a daemon is a program that runs as a background process. Traditionally, the process names of a daemon end with the letter d, for clarification that the process is in fact a daemon, and for differentiation between a daemon and a normal computer program. For example, syslogd is a daemon that implements system logging facility, and sshd is a daemon that serves incoming SSH connections.

    pbmac@ubuntu $ systemctl list-unit-files --type service -all
    UNIT FILE                                  STATE           VENDOR PRESET
    accounts-daemon.service                    enabled         enabled      
    alsa-restore.service                       static          enabled      
    alsa-state.service                         static          enabled      
    alsa-utils.service                         masked          enabled      
    anacron.service                            enabled         enabled      
    apparmor.service                           enabled         enabled      
    apport-autoreport.service                  static          enabled      
    apport-forward@.service                    static          enabled      
    apport.service                             generated       enabled      

    This example shows some of the services running on a Linux system. Services can be listed by using the systemctl command. There are numerous options that provide a variety of output formats

    In a Linux environment, the parent process of a daemon is often, but not always, the init process. A daemon is usually created either by a process forking a child process and then immediately exiting, thus causing init to adopt the child process, or by the init process directly launching the daemon. In addition, a daemon launched by forking and exiting typically must perform other operations, such as dissociating the process from any controlling terminal (tty). Such procedures are often implemented in various convenience routines such as daemon(3) in Unix.

    Since 2015, the majority of Linux distributions have adopted systemd, having replaced other systems such as the UNIX System V and BSD init systems. systemd has faced mixed reception from Linux users over a variety of concerns.

    Systems often start daemons at boot time which will respond to network requests, hardware activity, or other programs by performing some task.

    Managing Daemons and Services

    As a Linux administrator it is important to understand services. There is an entire lifecycle that services, and actually all Linux processes, go through. Many services get started at boot time, while others are initiated at specific times or days to perform a necessary task. We will take a look at configuring and managing these services and daemons through the use of a couple of important Linux commands.

    In the Linux realm there are two methods that are used to initialize Linux. One is named SysVinit, the other is systemd.

    As was mentioned in the previous module, Linux Boot Process, towards the end of Stage 2 GRUB2 loads the selected kernel into memory and turns control of the computer over to the kernel. All of the kernels are in a self-extracting, compressed format to save space. The kernels are located in the /boot directory, along with an initial RAM disk image, and device maps of the hard drives.

    After the selected kernel is loaded into memory and begins executing, it must first extract itself from the compressed version of the file before it can perform any useful work. Once the kernel has extracted itself, it loads one of the initialization methods, and turns control over to it.

    The startup process follows the boot process and brings the Linux computer up to an operational state in which it is usable for productive work.


    systemd is a software suite that provides an array of system components for Linux, and is the mother of all processes. It is responsible for bringing the Linux host up to a state in which productive work can be done. Some of its functions, which are far more extensive than the old init program, are to manage many aspects of a running Linux host, including mounting filesystems, and starting and managing system services required to have a productive Linux host.

    Its main aim is to unify service configuration and behavior across Linux distributions; systemd's primary component is a "system and service manager"—an init system used to bootstrap user space and manage user processes. It also provides replacements for various daemons and utilities, including device management, login management, network connection management, and event logging. The name systemd adheres to the Unix convention of naming daemons by appending the letter d.

    systemd daemons make it is easier to supervise and control processes and parallelized job execution. systemd also provides the systemctl mmand and cgroups to make system administration easier: 1) systemctl provides the administrator with more detailed error messages including runtime and start-up errors; and cgroups - "control groups" -allow for the grouping of processes into a hierarchy for easier management.

    Unit files: What are They?

    The unit files on your system determine how systemd will start and run. Each corresponds to a single activity or component — or unit in systemd terms. Each unit file is a simple text file describing a unit, what it does, what needs to run before or afterward, and other details.

    Unit files can be stored in a few different places on your system. systemd looks for system unit files in this order:

    1. /etc/systemd/system
    2. /run/systemd/system
    3. /usr/lib/systemd/system

    Unit files in the earlier directories override later ones. This is a useful scheme, because it lets you make changes in the /etc directory, where configuration is expected. You should avoid making changes in /usr. Your system installs package data there that’s not expected to change.

    systemd can also run in a user context, and manage resources per user in addition to system-side. Unit files for user units are stored similarly in /etc/systemd/user, /run/systemd/user, and /usr/lib/systemd/user. The order of precedence works similarly.

    You can examine some details about these unit files using the systemctl command. If you want to see a list of all the unit files installed on the system, run this command:

    systemctl list-unit-files

    Each unit file contains options in the form OptionName=value, separated into sections marked like [SectionName]. These options describe how the unit works, and how systemd deals with it.

    There are numerous types of units systemd understands. The two most common for system owners to deal with are service units and target units. To list unit files on your system of each of these types, use the systemctl command:

    systemctl list-unit-files --type service
    systemctl list-unit-files --type target

    Service units

    These are the units that describe a process systemd can start and monitor. Service units are the most common units you’ll use daily. They are controlled using systemctl as root:

    systemctl [command] NAME.service

    Typical commands include:

    • start: starts a systemd unit
    • stop: attempts to “nicely” end a service
    • status: provides detailed information on a service
    • restart: restarts (stops and then starts) the specified service
    • enable: hooks (links) a unit to various places, for instance to run at boot
    • disable: unhooks (unlinks) a unit, so it is not activated

    The following example is the sshd.service unit file. It allows systemd to control the sshd daemon that allows remote access to your system via SSH (Secure Shell).

    Description=OpenSSH server daemon
    Documentation=man:sshd(8) man:sshd_config(5) sshd-keygen.service
    ExecStart=/usr/sbin/sshd -D $OPTIONS
    ExecReload=/bin/kill -HUP $MAINPID

    This unit features both some common unit file options, and service unit-specific options. Common options seen here include a description and where to find documentation. There is obviously more in this unit file than this article can explain. But as you can probably guess from the ExecStart option, this unit runs the sshd daemon when started.

    Service Owned Processes

    One interesting feature of systemd is that it monitors processes it starts with service units. To find out what processes are being monitored, use the systemctl status command. For instance, here is output from that command checking the status of the sshd.service unit:

    ● sshd.service - OpenSSH server daemon
       Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: disabled)
       Active: active (running) since Tue 2015-10-20 14:51:24 EDT; 1 weeks 0 days ago
       Docs: man:sshd(8)
       Main PID: 1090 (sshd)
       CGroup: /system.slice/sshd.service
               └─1090 /usr/sbin/sshd -D

    We can see that the process started by this service has process ID (PID) 1090. This process will continue to be monitored by systemd.

    Notice also that the service has placed this process in a specially named control group. The control group (or “cgroup”) allows systemd to manage associated processes together. In future articles in the series, we’ll explore how this feature allows you to tune performance and resource limits.

    Thanks to knowing the processes owned by this service, systemd can also help you manage errant services. Normally when you stop a service, you’ll use the systemctl command as mentioned earlier. For example, to stop the web server service:

    systemctl stop httpd.service

    But what if the service doesn’t respond or cooperate? In this case, systemctl has a built in kill switch:

    systemctl kill httpd.service

    Target Units

    Target units are used to link and group other units together to describe a desired system state. Some of these units may be services. Others may be additional target units with their own groups of units.

    Here’s an example, the unit file:

    Description=Multi-User System
    Conflicts=rescue.service rescue.service

    Notice that this target unit file doesn’t contain a command to execute. Instead, it functions purely as a way to connect other units (in this case, mostly targets) together. In this case:

    • The requires that run successfully when is run.
    • If the rescue.service or units run, they will cause this unit to stop, and vice versa.
    • The unit starts after starts, and after rescue.service and are run, if those are started.

    Additionally this target unit includes the AllowIsolate option. This option allows your system to treat the unit as a boot target, using the systemctl isolate command.

    Target units group other units together not just with their unit file contents. A target can also have a .wants directory that links to units which will be started along with the target. For instance, the /usr/lib/systemd/system/ file has an associated folder /usr/lib/systemd/system/ This directory contains links to units (not just services but other targets as well) that will run when this target is run. Each of those may have its own dependencies, or unit file options like Requires above, as well.

    Adapted from:
    "Daemons" by UnknownWikipedia is licensed under CC BY-NC-SA 3.0
    "systemd" by Multiple ContributorsWikipedia is licensed under CC BY-SA 3.0
    "An introduction to the Linux boot and startup processes" by David Both, is licensed under CC BY-SA 4.0
    "Using the systemctl command to manage systemd units" by David Both, is licensed under CC BY-SA 4.0
    "systemd unit file basics" by Bryan Sutherland, Fedora Magazine is licensed under CC BY-SA 3.0

    08-C.8: Managing Linux Services is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?