Lab 8.1 - Managing systemd Services
Objectives
By the end of this lab, you will be able to:
- Use systemctl to manage services
- Understand service states
- Start, stop, restart services
- Enable/disable services at startup
- View service logs
Prerequisites
- Ubuntu 24.04 installed
- Sudo access
- Terminal open
Exercise 1: Basic Commands
Step 1: View the Status of a Service
# Service status
systemctl status sshd
# or
systemctl status ssh
# Short status
systemctl is-active sshd
systemctl is-enabled sshd
systemctl is-failed sshd
Step 2: List Services
# All services
systemctl list-units --type=service
# Active services
systemctl list-units --type=service --state=running
# Inactive services
systemctl list-units --type=service --state=inactive
# Failed services
systemctl list-units --type=service --state=failed
Exercise 2: Service Control
Step 1: Start/Stop
# Start a service
sudo systemctl start sshd
# Stop a service
sudo systemctl stop sshd
# Restart
sudo systemctl restart sshd
# Reload configuration (if supported)
sudo systemctl reload sshd
Step 2: Enable at Startup
# Enable (automatic startup)
sudo systemctl enable sshd
# Disable
sudo systemctl disable sshd
# Enable and start
sudo systemctl enable --now sshd
Step 3: Mask a Service
# Mask (prevents manual start too)
sudo systemctl mask sshd
# Unmask
sudo systemctl unmask sshd
Exercise 3: Service Logs
Step 1: journalctl
# Service logs
sudo journalctl -u sshd
# Last lines
sudo journalctl -u sshd -n 50
# Follow in real-time
sudo journalctl -u sshd -f
# Logs from today
sudo journalctl -u sshd --since today
# Logs from a period
sudo journalctl -u sshd --since "2024-12-01" --until "2024-12-31"
Step 2: Log Levels
# By priority
sudo journalctl -u sshd -p err
sudo journalctl -u sshd -p warning
# Combinations
sudo journalctl -u sshd -p err --since "1 hour ago"
Exercise 4: System Services
Step 1: Essential Services
# View system services
systemctl list-units --type=service | grep systemd
# systemd status
systemctl status
# Critical services
systemctl list-units --type=service --state=failed
Step 2: Analyze Dependencies
# Dependencies of a service
systemctl list-dependencies sshd
# Services depending on sshd
systemctl list-dependencies --reverse sshd
Exercise 5: Practice with nginx
Step 1: Installation and Configuration
# Install nginx
sudo apt update
sudo apt install -y nginx
# Check status
systemctl status nginx
# Start
sudo systemctl start nginx
# Enable at startup
sudo systemctl enable nginx
Step 2: Management
# Restart
sudo systemctl restart nginx
# Reload configuration
sudo systemctl reload nginx
# View logs
sudo journalctl -u nginx -f
Step 3: Test
# Test the service
curl http://localhost
# View processes
ps aux | grep nginx
Exercise 6: Management Script
Step 1: Basic Script
# Script to manage a service
cat > manage-service.sh << 'EOF'
#!/bin/bash
SERVICE=$1
ACTION=$2
if [ -z "$SERVICE" ] || [ -z "$ACTION" ]; then
echo "Usage: $0 service {start|stop|restart|status|enable|disable}"
exit 1
fi
case $ACTION in
start)
sudo systemctl start "$SERVICE"
;;
stop)
sudo systemctl stop "$SERVICE"
;;
restart)
sudo systemctl restart "$SERVICE"
;;
status)
systemctl status "$SERVICE"
;;
enable)
sudo systemctl enable "$SERVICE"
;;
disable)
sudo systemctl disable "$SERVICE"
;;
*)
echo "Unknown action: $ACTION"
exit 1
;;
esac
EOF
chmod +x manage-service.sh
# Use
./manage-service.sh nginx status
./manage-service.sh nginx restart
Verification
Checklist
Check that you can:
- View service status (systemctl status)
- List services (list-units)
- Start/stop services
- Enable/disable at startup
- View logs (journalctl)
- Analyze dependencies
- Manage common services (nginx, sshd)
Reflection Questions
-
What is the difference between start and enable?
- start: Starts now
- enable: Activates at system startup
-
When to use reload vs restart?
- reload: Reloads config without interruption
- restart: Completely restarts (interruption)
-
Why use journalctl instead of /var/log?
- journalctl: Centralized systemd logs, more modern
- /var/log: Traditional logs, always available
Next Steps
- Lab 8.2: Custom Services
- Lab 8.3: Advanced Cron and systemd Timers
- Chapter 8.1: Managing systemd Services
Lab created on: December 2024 - Tested on Ubuntu 24.04