Linux 118: Systemd Deep Dive – Managing Services and Boot Processes

Linux 118: Systemd Deep Dive – Managing Services and Boot Processes

Systemd is the modern init system used by most major Linux distributions. It manages everything from service startup to logging, device management, and system states. Understanding systemd is essential for effective system administration.


1. What is systemd?

Systemd is a suite of system components designed to manage:

  • System boot and shutdown
  • Service supervision
  • Device and mount point handling
  • Logging via journal

It replaces older init systems like SysVinit and Upstart.


2. systemctl – The Core Command

The systemctl command is used to interact with systemd.

Start, Stop, Enable Services


sudo systemctl start nginx sudo systemctl stop nginx sudo systemctl restart nginx

Enable/Disable on Boot


sudo systemctl enable nginx sudo systemctl disable nginx

Check Status


systemctl status nginx

3. Listing Services and Targets

List All Services


systemctl list-units --type=service

List Enabled Services


systemctl list-unit-files --state=enabled

Check Target (Runlevel Equivalent)


systemctl get-default

Change target:


sudo systemctl set-default multi-user.target

4. Understanding Unit Files

Unit files define how systemd manages resources like services, mounts, devices, and sockets.

Types include:

  • .service – services
  • .target – groups of units
  • .socket – network sockets
  • .timer – scheduled tasks

Example unit file: /etc/systemd/system/myapp.service


[Unit] Description=My Custom App [Service] ExecStart=/usr/bin/myapp Restart=on-failure [Install] WantedBy=multi-user.target

Enable and start it:


sudo systemctl enable myapp sudo systemctl start myapp

5. Journald – Viewing Logs

Systemd uses journald for centralized logging.

View Recent Logs


journalctl

Filter by Service


journalctl -u nginx

Follow Logs in Real Time


journalctl -f

Limit to Boot Session


journalctl -b

6. Debugging Boot Issues

Analyze Boot Time


systemd-analyze

View Boot Processes by Time


systemd-analyze blame

Graph Boot Process


systemd-analyze plot > boot.svg

Open the file to see a visual representation of boot performance.


7. Scheduling Tasks with systemd Timers

Timers can replace cron for more advanced scheduling.

Create a Timer:

myjob.timer


[Unit] Description=Run My Job [Timer] OnCalendar=*-*-* 12:00:00 Unit=myjob.service [Install] WantedBy=timers.target

Create myjob.service as a companion unit.

Enable and start:


sudo systemctl enable --now myjob.timer

8. Managing System States

Reboot and Shutdown


sudo systemctl reboot sudo systemctl poweroff

Suspend and Hibernate


sudo systemctl suspend sudo systemctl hibernate

9. Creating Rescue and Emergency Modes

  • Rescue mode: Minimal services and root shell
  • Emergency mode: Only root filesystem and shell

Enter:


sudo systemctl rescue sudo systemctl emergency

10. Conclusion

Systemd is more than just a service manager — it's the backbone of modern Linux systems. Mastering its components helps you control services, debug boot problems, manage logs, and automate tasks effectively.


Next Steps:

  • Learn how to write custom .service and .timer units
  • Explore systemd-resolved and systemd-networkd for networking
  • Practice troubleshooting with systemd-analyze and journalctl


Coming next: Linux 119: Using Containers with Podman and Docker

Post a Comment (0)
Previous Post Next Post

ads