Skip to main content

Chapter 8.1 - Introduction to systemd

Learning Objectives​

By the end of this chapter, you will be able to:

  • Understand what systemd is
  • Use systemctl to manage services
  • Understand systemd units
  • View the status of services
  • Start, stop, restart services

What is systemd?​

systemd is the initialization and service management system on most modern Linux distributions.

Functions:

  • Service management
  • System startup
  • Log management (journald)
  • Scheduling (timers)
  • File system mounting

systemd Units​

Unit Types​

# View types
systemctl list-units --type=service
systemctl list-units --type=timer
systemctl list-units --type=mount
systemctl list-units --type=socket

Common types:

  • .service: Services
  • .timer: Scheduled tasks
  • .mount: Mount points
  • .socket: Network sockets
  • .target: Unit groups

systemctl - Basic Commands​

View Status​

# All services
systemctl list-units --type=service

# Active services
systemctl list-units --type=service --state=running

# Inactive services
systemctl list-units --type=service --state=inactive

# Failed services
systemctl list-units --type=service --state=failed

# Service status
systemctl status sshd
systemctl status apache2

Start/Stop​

# Start
sudo systemctl start service_name

# Stop
sudo systemctl stop service_name

# Restart
sudo systemctl restart service_name

# Reload configuration
sudo systemctl reload service_name

# Reload or restart
sudo systemctl reload-or-restart service_name

Enable/Disable at Startup​

# Enable (automatic startup)
sudo systemctl enable service_name

# Disable
sudo systemctl disable service_name

# Enable and start
sudo systemctl enable --now service_name

# Check if enabled
systemctl is-enabled service_name

Service States​

Possible States​

States:

  • active (running): Running
  • active (exited): Successfully completed
  • inactive: Stopped
  • failed: Failed
  • activating: Starting

View Details​

# Detailed status
systemctl status service_name

# Complete information
systemctl show service_name

# Specific property
systemctl show -p ActiveState service_name
systemctl show -p LoadState service_name

Useful Commands​

List Services​

# All services
systemctl list-units --type=service --all

# Enabled services
systemctl list-unit-files --type=service --state=enabled

# Disabled services
systemctl list-unit-files --type=service --state=disabled

# By state
systemctl list-units --type=service --state=running
systemctl list-units --type=service --state=failed

Mask/Unmask​

# Mask service
sudo systemctl mask service_name

# Unmask
sudo systemctl unmask service_name

Reload systemd​

# Reload configuration (after file modifications)
sudo systemctl daemon-reload

Practical Examples​

SSH Management​

# View status
systemctl status sshd

# Start
sudo systemctl start sshd

# Enable at startup
sudo systemctl enable sshd

# Restart
sudo systemctl restart sshd

Apache/Nginx Management​

# Apache
sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl status apache2

# Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx

Failed Services​

# View failed services
systemctl list-units --state=failed

# Restart failed service
sudo systemctl restart failed_service

# View logs
sudo journalctl -u failed_service

Summary​

In this chapter, you learned:

systemd: Initialization and service management system
Units: .service, .timer, .mount, .socket, .target
systemctl: Commands to manage services
States: active, inactive, failed, activating
Commands: start, stop, restart, enable, disable, status
Practice: Managing common services (SSH, Apache, etc.)


Next Steps​

Chapter 8.2: Creating Custom Services
Chapter 8.3: Advanced Cron and systemd Timers


Chapter created on: December 2024