Understanding systemd in Linux

utsab sapkotautsab sapkota
4 min read

If you've ever managed a Linux system, you've likely encountered systemd. It's the default init system for most modern Linux distributions, responsible for starting and managing system processes. Let's break it down into simple terms and explore how to work with it effectively.

What is systemd?

systemd is an init system, which means it's the first process that runs when your system boots up. It holds the PID 1 (use htop command to view it), making it the parent of all other processes on your system. Its main job is to manage all the background services, known as daemons.

With systemd, you can start, stop, restart, and check the status of these services easily.

Working with Units

n systemd, the things it manages are called units. These can be services, timers, mounts, sockets, and more. Units are stored in special configuration files, known as unit files, which define their behavior.

Types of Units

  1. Service Units (.service) - These define system services that run in the background, such as Apache (apache2.service).

  2. Timer Units (.timer) - These act like cron jobs, scheduling tasks at specific times.

  3. Socket Units (.socket) - These manage network sockets and can start services on demand.

  4. Mount Units (.mount) - These handle filesystem mounting and ensure certain filesystems are mounted at boot.

By understanding different unit types, you can automate tasks and efficiently manage system resources.

Note: There are multiple types of units present.

Why Do We Create Units?

We create unit files to define how services should start, stop, and restart. Instead of manually running scripts each time a service needs to be controlled, systemd unit files provide a standardized way to manage them.

For example, if you develop a custom application that runs in the background, you can create a service unit so systemd can manage it like any other system service.

systemd Unit Directories

Unit files are stored in different directories based on their purpose and priority:

  • /etc/systemd/system/ → Highest priority, used for system administrator overrides.

  • /lib/systemd/system/ → Contains system-wide default unit files provided by installed packages.

  • /run/systemd/system/ → Temporary units created at runtime, usually for transient services.

Installing a Web Server

If you're using a Debian/Ubuntu-based system, install Apache using:

sudo apt install apache2

For Red Hat/Fedora-based systems, install HTTPD (Apache) using:

sudo dnf install httpd

Once installed, you can manage it with systemd.

Managing Services with systemd

Here are some essential commands for working with services: (We are currently on Ubuntu system)

  • Start a service:

      sudo systemctl start apache2  # (Debian/Ubuntu)
      sudo systemctl start httpd    # (RHEL/Fedora)
    
  • Stop a service:

      sudo systemctl stop apache2
    
  • Restart a service:

      sudo systemctl restart apache2
    
  • Check the status of a service:

      sudo systemctl status apache2
    
  • Enable a service to start at boot:

      sudo systemctl enable apache2
    
  • Disable a service from starting at boot:

      sudo systemctl disable apache2
    

How Overrides Work

If a unit file exists in multiple locations, systemd follows this priority order:

  1. /etc/systemd/system/ (Admin-created overrides, highest priority)

  2. /run/systemd/system/ (Runtime changes, removed on reboot)

  3. /lib/systemd/system/ (Default unit files from system packages, lowest priority)

To override a unit file, create a new configuration in /etc/systemd/system/. If the same unit exists in /lib/systemd/system/, the version in /etc takes precedence.

Note: The units are placed in /lib/systemd/system and overridden through the help of /etc/systemd/system location following the priority sequence. You can see the Loaded: loaded (filepath) to know where the unit is present.

Understanding systemd Unit Files

Unit files are configuration files that define how a service should run.

If you want to modify a service, you can override its unit file instead of editing the original.

Editing Unit Files

To override a service unit file, use:

sudo systemctl edit <service-name> 
sudo systemctl edit apache2 #for example apache2

Note: The changes should be placed about the comment ### Edits below this comment will be discarded.

This creates a drop-in override in /etc/systemd/system/<service-name>.d/override.conf.

If you want to edit the full configuration file, use:

sudo systemctl edit --full apache2

This will copy the existing unit file into /etc/systemd/system/, allowing complete modifications.

After making changes, reload systemd and restart the service:

sudo systemctl daemon-reload
sudo systemctl restart apache2

Conclusion

systemd makes managing services in Linux easier and more efficient. Whether you're starting, stopping, or modifying services, these commands will help you work effectively with your Linux system.

By understanding how systemd units work, including different types like services, timers, and sockets, you can take full advantage of systemd's capabilities.

0
Subscribe to my newsletter

Read articles from utsab sapkota directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

utsab sapkota
utsab sapkota