What Is systemd? (Need Revision from linkedIn series PTC)
systemd is a system and service manager for Linux operating systems. It initializes the system at boot, manages background services (daemons), and handles logging via journald.
A service in systemd is a unit of work that typically runs in the background. These are defined by .service files located in:
/etc/systemd/system/— Custom or user-defined services/lib/systemd/system/— Installed by packages/run/systemd/system/— Runtime-generated services
Here’s a minimal example of a systemd service:
[Unit]
Description=My Sample Web Server
After=network.target
[Service]
ExecStart=/usr/bin/python3 -m http.server 8080
Restart=always
User=www-data
[Install]
WantedBy=multi-user.targetKey Sections
| Section | Purpose |
|---|---|
[Unit] | Metadata and dependencies |
[Service] | How the service starts/stops |
[Install] | When/where to start the service |
| Task | Command |
|---|---|
| Start a service | sudo systemctl start nginx |
| Stop a service | sudo systemctl stop nginx |
| Restart a service | sudo systemctl restart nginx |
| Reload without restarting | sudo systemctl reload nginx |
| Enable (auto-start at boot) | sudo systemctl enable nginx |
| Disable | sudo systemctl disable nginx |
| Check if running | systemctl status nginx |
| View all active services | systemctl list-units --type=service |
| View all installed service files | systemctl list-unit-files |
| Show details | systemctl show nginx |
| Analyze boot time of services | systemd-analyze blame |
$ systemctl status sshSample output:
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled)
Active: active (running) since Mon 2025-07-21 10:00:00 IST; 3h 22min ago
Main PID: 1234 (sshd)
Tasks: 1
Memory: 2.5M| Field | Meaning |
|---|---|
Loaded | Service file is recognized and configured |
Active | Current state (active, inactive, failed, etc.) |
Main PID | The main process ID of the running service |
# Create a custom service file
sudo nano /etc/systemd/system/hello.serviceContent:
[Unit]
Description=Hello Service
[Service]
ExecStart=/bin/echo "Hello, world!"
[Install]
WantedBy=multi-user.targetThen:
sudo systemctl daemon-reexec # Reload systemd itself (rarely needed)
sudo systemctl daemon-reload # Reload unit files
sudo systemctl start hello
sudo systemctl enable hello
systemctl status hello-
Reload after editing services:
sudo systemctl daemon-reload -
Check logs:
journalctl -u servicename -
Debug a failing service: Add
StandardOutput=journal+consoleto your[Service]block.
| Flag | Description |
|---|---|
--type=service | Filter for services only |
--state=failed | Show failed services |
--now | Used with enable/disable to act immediately |
--user | For user-level services |
Last updated on