Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Learn Modern Service Management System (Systemd) on Linux
systemd is a modern system and service manager for Linux operating systems. Running as the first process on boot (PID 1), it acts as an init system that initializes and maintains userspace services, replacing traditional SysV init scripts with a more efficient service management framework.
systemd provides comprehensive system initialization, service management, and system state control through its suite of tools, primarily systemctl for service control and systemd-analyze for performance analysis.
Basic systemd Information
To get help information about systemd, use the following command −
$ systemd -h
Starts up and maintains the system or user services. -h --help Show this help --test Determine startup sequence, dump it and exit --no-pager Do not pipe output into a pager --dump-configuration-items Dump understood unit configuration items --unit=UNIT Set default unit --system Run a system instance, even if PID != 1 --user Run a user instance --dump-core[=BOOL] Dump core on crash --crash-vt=NR Change to specified VT on crash --crash-reboot[=BOOL] Reboot on crash --crash-shell[=BOOL] Run shell on crash
Version Information
To check the systemd version and compile-time features −
$ systemd --version
systemd 229 +PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN
Boot Process Analysis
Analyze Boot Performance
To analyze systemd boot process timing −
$ systemd-analyze
Startup finished in 8.740s (firmware) + 4.483s (loader) + 3.616s (kernel) + 4min 21.244s (userspace) = 4min 38.084s
Detailed Service Boot Times
To see which services took the longest to start during boot −
$ systemd-analyze blame
3min 59.399s apt-daily.service
16.585s NetworkManager-wait-online.service
11.182s grub-common.service
9.532s apport.service
9.224s irqbalance.service
8.986s networking.service
8.916s snapd.refresh.service
8.859s speech-dispatcher.service
7.709s dev-sda2.device
7.641s gpu-manager.service
Service Management with systemctl
systemctl is the primary command for controlling systemd services. It allows you to start, stop, restart, enable, disable, and monitor services.
Basic Service Operations
| Operation | Command | Description |
|---|---|---|
| Start | sudo systemctl start service.service |
Starts a service immediately |
| Stop | sudo systemctl stop service.service |
Stops a running service |
| Restart | sudo systemctl restart service.service |
Restarts a service |
| Status | systemctl status service.service |
Shows service status and logs |
| Enable | sudo systemctl enable service.service |
Enables service to start at boot |
| Disable | sudo systemctl disable service.service |
Disables service from starting at boot |
Example: Managing Bluetooth Service
Start the bluetooth service −
$ sudo systemctl start bluetooth.service
Check service status −
$ systemctl status bluetooth
bluetooth.service - Bluetooth service Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2017-01-27 09:32:45 IST; 3 days ago Docs: man:bluetoothd(8) Main PID: 954 (bluetoothd) Status: "Running" CGroup: /system.slice/bluetooth.service ??954 /usr/lib/bluetooth/bluetoothd
Advanced Service Control
Masking and Unmasking Services
Masking prevents a service from being started manually or automatically −
$ sudo systemctl mask bluetooth.service $ sudo systemctl unmask bluetooth.service
Service Dependencies
View service dependencies −
$ systemctl list-dependencies bluetooth.service
Critical Chain Analysis
Analyze the critical chain for a specific service −
$ systemd-analyze critical-chain bluetooth.service
bluetooth.service +886ms
System Control Groups (cgroups)
Control Group Hierarchy
View the control group hierarchy −
$ systemd-cgls
Control group /: -.slice ??init.scope ? ??1 /lib/systemd/systemd --system --deserialize 19 ??system.slice ? ??bluetooth.service ? ? ??954 /usr/lib/bluetooth/bluetoothd ? ??dbus.service ? ? ??759 /usr/bin/dbus-daemon --system ? ??cron.service ? ??850 /usr/sbin/cron -f
Resource Usage Monitoring
Monitor resource usage by control groups −
$ sudo systemd-cgtop
Control Group Tasks %CPU Memory Input/s Output/s / - 20.0 6.3G - - /system.slice 125 - - - - /user.slice 695 - - - -
Unit File Management
Listing Units and Unit Files
List all active units −
$ systemctl list-units
List all unit files and their states −
$ systemctl list-unit-files
List specific unit types −
$ systemctl list-unit-files --type=service $ systemctl list-unit-files --type=socket
System State Control
systemd provides commands for system power management −
| Command | Action |
|---|---|
sudo systemctl reboot |
Restart the system |
sudo systemctl poweroff |
Shutdown and power off |
sudo systemctl halt |
Halt the system |
sudo systemctl suspend |
Suspend to RAM |
sudo systemctl hibernate |
Hibernate to disk |
Conclusion
systemd revolutionizes Linux system management with its unified approach to service control, boot process optimization, and resource management. Its powerful tools like systemctl and systemd-analyze provide administrators with comprehensive control over system services and performance monitoring, making it an essential skill for modern Linux administration.
