System Services
systemctl & Daemons
What is a Service?
A service (or daemon) is a program that runs in the background:
- nginx: Web server
- ssh: Remote access
- cron: Scheduled tasks
- docker: Container runtime
- NetworkManager: Network connections
They start automatically at boot and run without user interaction.
systemd & systemctl
systemd is the init system on modern Linux. It manages:
- Services (daemons)
- Boot process
- Logging
- Mounts
- Timers
systemctl is the command to control systemd.
Service Management
Check Status
systemctl status nginx # Service status
systemctl status ssh # Is SSH running?
systemctl is-active nginx # Just "active" or "inactive"
systemctl is-enabled nginx # Starts at boot?
Start / Stop / Restart
sudo systemctl start nginx # Start service
sudo systemctl stop nginx # Stop service
sudo systemctl restart nginx # Stop + Start
sudo systemctl reload nginx # Reload config (no downtime)
Enable / Disable (Boot Behavior)
sudo systemctl enable nginx # Start at boot
sudo systemctl disable nginx # Don't start at boot
sudo systemctl enable --now nginx # Enable AND start now
Understanding Status Output
systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled)
Active: active (running) since Mon 2024-12-30 10:00:00 UTC
Main PID: 1234 (sshd)
Tasks: 1 (limit: 4915)
Memory: 2.1M
CPU: 32ms
CGroup: /system.slice/ssh.service
└─1234 sshd: /usr/sbin/sshd -D
Dec 30 10:00:00 ubuntu sshd[1234]: Server listening on 0.0.0.0 port 22
Key information:
- Loaded: Where the service file is, enabled/disabled
- Active: Running/stopped and since when
- Main PID: Process ID
- CGroup: Process hierarchy
Listing Services
systemctl list-units --type=service # Running services
systemctl list-units --type=service --all # All services
systemctl list-unit-files --type=service # All service files
systemctl list-unit-files --state=enabled # Services enabled at boot
Viewing Logs
journalctl shows systemd logs:
journalctl # All logs
journalctl -u ssh # Logs for SSH service
journalctl -u nginx -f # Follow nginx logs (live)
journalctl -u ssh --since "1 hour ago" # Recent logs
journalctl -u ssh -n 50 # Last 50 lines
journalctl -b # Logs since boot
journalctl -p err # Only errors
Service Files
Services are defined in .service files:
cat /lib/systemd/system/ssh.service
[Unit]
Description=OpenBSD Secure Shell server
After=network.target
[Service]
ExecStart=/usr/sbin/sshd -D
Restart=on-failure
[Install]
WantedBy=multi-user.target
Creating custom services is beyond this lesson, but now you understand the structure.
Common Services
| Service | Purpose | |---------|---------| | sshd | SSH server (remote login) | | nginx / apache2 | Web server | | docker | Container runtime | | cron | Scheduled tasks | | cups | Printing | | NetworkManager | Network management | | ufw | Firewall |
Try It!
Practice service management in the terminal:
Exercises:
- Check SSH status:
systemctl status ssh - List running services:
systemctl list-units --type=service - Check if a service is enabled:
systemctl is-enabled ssh - View logs:
journalctl -u ssh -n 10
Try it in the terminal
A sandboxed shell with this lesson’s commands. Type help to see what’s available.