Linux Systemd and Service Management

systemd is a widely used initialization and service management system on modern Linux distributions. It starts system services, manages dependencies, handles system states, and provides tools for monitoring and troubleshooting services.

What is systemd?

systemd is responsible for starting important services during boot and managing them while the system is running. It uses units to represent services, sockets, timers, mounts, and other system resources.

TEXT
Boot → systemd → Dependencies → Services → Running System

What is systemctl?

systemctl is the primary command used to communicate with systemd. It can start, stop, restart, enable, disable, and inspect system services.

BASH
systemctl status nginx
sudo systemctl start nginx
sudo systemctl stop nginx

Starting a Service

The systemctl start command starts a service immediately. Starting a service does not necessarily mean it will automatically start after the next reboot.

BASH
sudo systemctl start nginx

Stopping a Service

The stop command terminates a running service when the service supports normal shutdown.

BASH
sudo systemctl stop nginx

Restarting and Reloading Services

restart stops and starts a service again, while reload asks a service to reload its configuration without a full restart when supported.

BASH
sudo systemctl restart nginx
sudo systemctl reload nginx

Checking Service Status

The status command provides information about whether a service is running, recent logs, its process ID, and recent systemd events.

BASH
systemctl status nginx

Enabling Services at Boot

The enable command configures a service to start automatically during the appropriate boot target. The disable command removes that configuration.

BASH
sudo systemctl enable nginx
sudo systemctl disable nginx

Enable and Start Together

The --now option can be used with enable or disable to apply the runtime state immediately as well as changing the boot configuration.

BASH
sudo systemctl enable --now nginx
sudo systemctl disable --now nginx

Listing Services

systemctl can list active services and other systemd units.

BASH
systemctl list-units --type=service
systemctl list-unit-files --type=service

Viewing Logs with journalctl

journalctl is used to query logs collected by the systemd journal. It is especially useful when a service fails to start or unexpectedly stops.

BASH
journalctl -u nginx
journalctl -u nginx --since today
journalctl -u nginx -n 50

Systemd Command Table

CommandPurposeExample
systemctl startStart a servicesudo systemctl start nginx
systemctl stopStop a servicesudo systemctl stop nginx
systemctl restartRestart a servicesudo systemctl restart nginx
systemctl statusCheck service statussystemctl status nginx
systemctl enableStart service at bootsudo systemctl enable nginx
systemctl disableDisable automatic startupsudo systemctl disable nginx
journalctlView systemd logsjournalctl -u nginx

Checking Failed Services

systemd can identify units that have entered a failed state. This is useful when troubleshooting boot or application problems.

BASH
systemctl --failed

Example Service Troubleshooting Scenario

TEXT
Service Fails → systemctl status → journalctl -u service → Check Configuration → Fix Problem → Restart Service

Systemd Units

systemd manages more than traditional services. Different unit types represent different system resources and tasks.

  • Service units manage background services
  • Socket units manage sockets
  • Timer units schedule tasks
  • Mount units manage mount points
  • Target units group related units

Common Service Management Problems

  • Service fails during startup
  • Incorrect configuration
  • Missing dependencies
  • Port already in use
  • Permission problems
  • Service disabled at boot

Common Mistakes to Avoid

  • Restarting services without checking logs
  • Enabling unnecessary services at boot
  • Editing service files incorrectly
  • Ignoring dependency failures
  • Running privileged commands without understanding their effect

Advanced systemd Concepts

  • Unit files
  • Service dependencies
  • Targets
  • Timers
  • Socket activation
  • Resource limits
  • Custom systemd services

Practice Exercises

  • Check the status of a service
  • Start and stop a test service
  • Enable a service at boot
  • View service logs with journalctl
  • Find failed systemd units
  • Restart a service after configuration changes

Conclusion

systemd provides a central way to manage services and many other system resources on modern Linux systems. Learning systemctl and journalctl is essential for Linux administration, service deployment, and troubleshooting.

Note: Note: When a service fails, check its status and journal logs before repeatedly restarting it. The logs often reveal the underlying problem.