This article is mainly the notes from “systemd” Tutorials
Why “systemd”?
What is systemd?
- A new kind of init system.
- Replaces SysV Init system
- Created at Redhat
- Chief Architect: Lennart Poettering
- Manages system services, targets, and resources.
Why systemd?
- Faster boot-up: Daemons start in parallel, reducing boot time.
- Simplified dependencies: No need to manually order daemon startup (no more numbered “S” and “K” links).
- Simpler config files: Easier to manage.
- Auto-restart: Automatically restarts crashed services.
- Better security: Services run in isolated cgroups; some services (e.g., auditd) cannot be stopped by root.
- Resource management: Tracks resource usage per process.
- Consistent administration: Unified commands across Linux distributions.
Shundown and Reboot Commands
systemd replaces traditional commands like shutdown, service, and chkconfig on Red Hat-type systems.
# Shutdown
sudo systemctl poweroff
# Reboot
sudo systemctl reboot
# Traditional commands still work but are deprecated
shutdown
reboot
For more details: man systemctl
Daemon Management
# Enable/disable services
systemctl enable name.service
systemctl disable name.service
systemctl is-enabled name.service
# Start/stop services
systemctl start name.service
systemctl stop name.service
# Check status
systemctl status name.service
systemctl status -l name.service # More detailed
# List all services
systemctl list-unit-files --type service
systemctl list-unit-files
Tragets vs. Runlevels
Targets are systemd’s equivalent of SysV runlevels.
# List all targets
sudo systemctl list-units --type target --all
# Set default target (runlevel)
sudo systemctl set-default multi-user.target # Text mode
sudo systemctl set-default graphical.target # Graphical mode
# Check current default
sudo systemctl get-default
# Switch to a target immediately
sudo systemctl isolate graphical.target # To graphical
sudo systemctl isolate multi-user.target # To text mode
Automatically Restart Crashed Services
To enable auto-restart, edit the service file:
sudo systemctl edit name.service
Add under [Service]:
Restart=always
Then reload and restart:
sudo systemctl daemon-reload
sudo systemctl restart name.service
Edit systemd Service Files Correctly
Use systemctl edit to override service files without modifying originals:
sudo systemctl edit nginx.service
This creates drop-in files in /etc/systemd/system/nginx.service.d/.
Controlling the auditd Service
auditd is a security auditing service.
sudo systemctl start auditd
sudo systemctl stop auditd
sudo systemctl status auditd
Note: auditd cannot be stopped or restarted by any user, including root, for security reasons.
Setting the hostname with systemd
# Check current hostname
hostname
# Set new hostname
sudo hostnamectl set-hostname newhostname
Setting the timezone with timedatectl
# Check current time and timezone
timedatectl
# Set timezone (e.g., to UTC)
sudo timedatectl set-timezone UTC
# Set time manually
sudo timedatectl set-time "2023-01-01 12:00:00"
Systemd Service Files
Service files are in /etc/systemd/system/ or /usr/lib/systemd/system/.
Example service file:
[Unit]
Description=ABC service
Documentation=https://example.com/abc.html
After=network.target
[Service]
Environment=VAR1=abc VAR2=abc VAR3=abc
ExecStart=/usr/local/bin/abc
Restart=on-failure
RestartSec=5
User=ubuntu
Group=ubuntu
Type=simple
WorkingDirectory=/data/abc
[Install]
WantedBy=multi-user.target
Another example:
[Unit]
Description=ABC service
After=network.target
[Service]
User=ubuntu
Group=ubuntu
Type=simple
Restart=always
RestartSec=5s
WorkingDirectory=/data/abc
ExecStart=/data/abc/abc
[Install]
WantedBy=multi-user.target
- [Unit]: Dependencies and description.
- [Service]: How to run the service.
- [Install]: When to start the service.
manual
man systemd
Additional Important Points
-
Units: systemd manages units (services, targets, sockets, timers, etc.). Services are just one type.
-
Logs: Use
journalctlto view system logs.journalctl -u name.service # Logs for a service journalctl -f # Follow logs in real-time -
Timers: systemd timers replace cron jobs.
systemctl list-timers -
Network Management: systemd-networkd for network configuration.
-
Resource Control: Use
systemctl showto see cgroup info. -
Masking Services: Prevent a service from starting:
sudo systemctl mask name.service -
Reloading Configuration: After editing files:
sudo systemctl daemon-reload
Comments