0% found this document useful (0 votes)
4 views7 pages

03 Systemctl Practical Guide

This document serves as a practical guide for managing Linux services using systemd and the systemctl command. It covers key concepts, service lifecycle commands, troubleshooting workflows, and practical examples for various services. The guide is designed for hands-on study, providing commands and explanations suitable for networking and Linux labs.

Uploaded by

alaahhl2003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

03 Systemctl Practical Guide

This document serves as a practical guide for managing Linux services using systemd and the systemctl command. It covers key concepts, service lifecycle commands, troubleshooting workflows, and practical examples for various services. The guide is designed for hands-on study, providing commands and explanations suitable for networking and Linux labs.

Uploaded by

alaahhl2003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

systemctl - Managing Linux

Services with systemd


Start, stop, enable, inspect, and troubleshoot services on modern Linux

What you will learn


systemd concepts, service lifecycle commands, boot behavior, status and logs, units and targets,
troubleshooting workflows, and practical examples.

Designed as a practical reference for networking and Linux labs. Commands are shown with safe
examples you can adapt to your own environment.

Prepared for hands-on study

systemctl Practical Guide Page 1


Contents
1. systemd and systemctl basics
2. Service lifecycle
3. Enable vs start
4. Status and logs
5. Units and targets
6. Editing and reloading units
7. Troubleshooting workflow
8. Practical recipes
9. Quick reference

systemctl Practical Guide Page 2


1. systemd and systemctl basics
systemd is the service and system manager used by many modern Linux distributions. systemctl is
the command-line tool used to control and inspect systemd units.

What is a unit?
A unit is an object managed by systemd. The most familiar type is a .service unit, but systemd also
manages sockets, timers, mounts, paths, devices, and boot targets.

Unit type Example Purpose

service [Link] Long-running daemon or application

socket [Link] Socket activation

timer [Link] Scheduled activation

mount [Link] Filesystem mount

target [Link] Group of units representing a system state

Basic syntax
systemctl ACTION UNIT

Examples:
systemctl status ssh
systemctl restart nginx
systemctl enable docker

You can often omit the .service suffix, so systemctl status ssh normally resolves to [Link].

systemctl Practical Guide Page 3


2. Service lifecycle
Command Meaning

systemctl start nginx Start now.

systemctl stop nginx Stop now.

systemctl restart nginx Stop then start.

systemctl reload nginx Ask the service to reload configuration without a full restart, if
supported.

systemctl try-restart nginx Restart only if it is already running.

systemctl status nginx Show current state and recent log lines.

Use sudo for changes


sudo systemctl restart nginx
sudo systemctl stop docker

Reading status often works without root access, while changing service state normally requires
elevated privileges.

3. Enable vs start
start affects the service now. enable configures the service to start automatically when the
appropriate boot target is reached.

Goal Command

Run now sudo systemctl start docker

Start automatically after reboot sudo systemctl enable docker

Do both sudo systemctl enable --now docker

Stop now sudo systemctl stop docker

Do not start automatically sudo systemctl disable docker

Disable and stop immediately sudo systemctl disable --now docker

Important
A service can be active but disabled (running now, not configured for next boot) or inactive but
enabled (stopped now, configured to start later).

systemctl Practical Guide Page 4


4. Status and logs
Detailed status
systemctl status docker
systemctl status ssh --no-pager

Typical status output includes whether the unit is loaded, enabled/disabled state, active state, main
PID, recent log lines, and failure information.

Check only the active state


systemctl is-active docker
systemctl is-enabled docker
systemctl is-failed docker

Use journalctl for full logs


# Logs for one unit
journalctl -u docker

# Most recent 100 lines


journalctl -u docker -n 100

# Follow logs live


journalctl -u docker -f

# Logs since current boot


journalctl -u docker -b

# Errors from current boot


journalctl -b -p err

When a service fails, systemctl status gives a quick summary, while journalctl -u UNIT usually
provides the deeper error context.

5. Units and targets


List running services
systemctl --type=service --state=running

List all service units


systemctl list-units --type=service --all

List installed unit files


systemctl list-unit-files --type=service

Failed units
systemctl --failed

Boot target
systemctl get-default

# Common server target


sudo systemctl set-default [Link]

# Common graphical target


sudo systemctl set-default [Link]

systemctl Practical Guide Page 5


6. Editing and reloading units
If you change a systemd unit file or create an override, systemd must reread its unit definitions.
sudo systemctl daemon-reload

View the effective unit


systemctl cat nginx

Create a safe override


sudo systemctl edit nginx

The edit command creates a drop-in override rather than modifying the vendor unit file directly. This
is preferred because package upgrades are less likely to overwrite your changes.

After changing an override


sudo systemctl daemon-reload
sudo systemctl restart nginx

Reset a failed state


sudo systemctl reset-failed nginx

7. Troubleshooting workflow
When a Linux service does not start, use a consistent workflow:

• Run systemctl status UNIT --no-pager.


• Read journalctl -u UNIT -n 100 --no-pager.
• Check whether the configuration syntax is valid using the application-specific test command.
• Check ports, file permissions, paths, certificates, and dependency services.
• If you edited a unit file, run systemctl daemon-reload.
• Restart the service and recheck status.
• Confirm the expected port/process is actually listening.

Example: Nginx fails to start


sudo systemctl status nginx --no-pager
sudo journalctl -u nginx -n 100 --no-pager
sudo nginx -t
sudo ss -lntp | grep -E ":80|:443"
sudo systemctl restart nginx
systemctl is-active nginx

systemctl Practical Guide Page 6


8. Practical recipes
SSH service
# Ubuntu/Debian commonly uses [Link]
systemctl status ssh
sudo systemctl restart ssh

# RHEL-like systems commonly use [Link]


systemctl status sshd
sudo systemctl restart sshd

Docker service
systemctl status docker
sudo systemctl enable --now docker
journalctl -u docker -n 100

Prometheus / Grafana examples


systemctl status prometheus
systemctl status grafana-server

sudo systemctl restart grafana-server


journalctl -u grafana-server -f

See dependencies
systemctl list-dependencies docker

9. Quick reference
Command Purpose

systemctl status UNIT Current state + recent logs

systemctl start UNIT Start now

systemctl stop UNIT Stop now

systemctl restart UNIT Restart

systemctl reload UNIT Reload config if supported

systemctl enable UNIT Start automatically at boot

systemctl disable UNIT Disable autostart

systemctl enable --now UNIT Enable and start now

systemctl is-active UNIT Machine-friendly active check

systemctl is-enabled UNIT Check boot enablement

systemctl --failed Show failed units

systemctl daemon-reload Reload unit definitions

systemctl cat UNIT Show effective unit file

systemctl edit UNIT Create/edit override

systemctl Practical Guide Page 7

You might also like