Lab 23: Start Stop Automate Services

Objective Focus
Deploy, configure, and maintain systems
Start and stop services and configure services to start automatically at boot

Official RHCSA EX200 Exam Objectives

Systemd aka System Daemon

To understand services, we need to understand systemd.

Systemd is the software that initializes and manages the other software that provide services. It is the first process on boot, starting as PID 1.

We can use the command top to see this in action.

$ top
Units

Systemd has many components under it and those components are managed using Units.

Units are Systemd objects that are used to configure and maintain the many software components in a RHEL system. That also includes services.

I am going to list the types of Units below a table but our focus are the Service Unit type.

Unit Type Extension Description
Service .service Defines how to manage a system service or application. This is the most common unit type.
Socket .socket Describes a network or IPC socket that systemd can listen on. Services can be activated on demand when traffic arrives on these sockets.
Device .device Represents a kernel device exposed in the sysfs filesystem. These units can be used to manage device-specific actions or dependencies.
Mount .mount Defines a filesystem mount point, similar to an entry in /etc/fstab. Systemd can manage mounting and unmounting.
Automount .automount Defines a mount point that will be automatically mounted when accessed, similar to autofs.
Swap .swap Specifies a swap device or swap file that systemd should activate.
Target .target Used to group other units together and act as synchronization points during the boot process or for specific system states (e.g., multi-user.target, graphical.target).
Path .path Monitors a filesystem path for changes. When a change occurs, it can trigger the activation of another unit (e.g., a service).
Timer .timer Defines a timer that can activate another unit (typically a service) at a specific time or on a recurring basis, similar to cron jobs.
Slice .slice Used for resource management. Slices group processes together in a hierarchical manner within the Linux control groups (cgroups) for resource allocation.
Scope .scope Similar to slices, but created dynamically for external processes or sets of processes that are not managed by systemd itself (e.g., user sessions).
Systemctl Command
$ man systemctl
  • Control the systemd system and service manager

Systemctl provides a plethora of subcommands and switches for managing the system.

$ systemctl
  • The default output shows the services in memory. That is with no options.
Finding what we need!
$ systemctl list-unit-files -t service
  • Example: List unit files that are classified as a service type.
Check the Status of a Service
$ systemctl status *.service

$ systemctl status sshd.service

$ systemctl status NetworkManager.service
Start a Service
$ systemctl start *.service 

$ systemctl start sshd.service
Stop a Service
$ systemctl stop *.service 

$ systemctl stop sshd.service 
Automate a Service to start at boot
$ systemctl enable *.service 

$ systemctl enable sshd.service 
Edit a service
$ systemctl show sshd.service
  • This will show all the available properties of the sshd service. These properties can be edited or added the unit pertaining to the sshd.service.
$ systemctl edit sshd.service
  • Here we can add, remove, and edit and properties of a specified service.
Table of Systemctl subcommands
Category Subcommand Description Example Usage
Service Control start <unit> Initiates a specified service or unit. sudo systemctl start apache2.service
stop <unit> Halts a running service or unit. sudo systemctl stop apache2.service
restart <unit> Stops a service (if running) and then starts it again. sudo systemctl restart apache2.service
reload <unit> Tells a service to reload its config without restarting (if supported). sudo systemctl reload apache2.service
reload-or-restart <unit> Reloads config if possible, otherwise restarts. sudo systemctl reload-or-restart nginx.service
status <unit> Displays the current status of a service (active, loaded, logs). systemctl status sshd.service
Boot-Time Config enable <unit> Configures a service to start automatically at boot. sudo systemctl enable firewalld.service
disable <unit> Prevents a service from starting automatically at boot. sudo systemctl disable firewalld.service
is-enabled <unit> Checks if a service is configured to start at boot. systemctl is-enabled ufw.service
mask <unit> Completely disables a service, preventing any start. sudo systemctl mask apache2.service
unmask <unit> Reverses the effect of mask. sudo systemctl unmask apache2.service
System/Unit File Mgmt. daemon-reload Reloads the systemd manager configuration (after unit file changes). sudo systemctl daemon-reload
list-units Displays all currently active units. systemctl list-units
list-unit-files Lists all installed unit files and their enabled/disabled state. systemctl list-unit-files
list-dependencies <unit> Shows the dependency tree for a specific unit. systemctl list-dependencies graphical.target
--failed (option) Used with list-units to show only failed units. systemctl --failed
get-default Shows the default systemd target. systemctl get-default
set-default <target> Sets the default systemd target for booting. sudo systemctl set-default multi-user.target
System Power Control poweroff Shuts down and powers off the system. sudo systemctl poweroff
reboot Reboots the system. sudo systemctl reboot
suspend Puts the system into a suspend (sleep) state. sudo systemctl suspend
hibernate Puts the system into a hibernate state. sudo systemctl hibernate
Systemctl

That is all for this lab. Understanding services makes your Linux system more useful and flexible.