System Services and Service Management
In this lesson, we'll explore how Linux manages system services - the background processes that keep your system running smoothly. You've already learned about process management and package management; now we'll see how to control the services those packages provide.
Learning Goals:
- Understand what system services are and how they work
- Master the
systemctlcommand for service management - Learn to start, stop, restart, and monitor services
- Configure services to start automatically at boot
- Troubleshoot common service issues
What Are System Services?
System services (also called daemons) are background processes that provide essential functionality to your system. Examples include:
- ssh: Secure Shell for remote access
- apache2 or nginx: Web servers
- mysql: Database server
- cron: Task scheduler
- network: Network connectivity
These services typically start automatically when your system boots and run continuously in the background.
The Systemd Init System
Ubuntu uses systemd as its init system - the first process that starts when your system boots (PID 1). Systemd manages all other services and processes.
Systemd replaced the older SysV init system. While you might encounter legacy service commands, systemctl is the modern standard.
Checking Systemd Status
Let's first verify that systemd is running:
systemctl status
You should see output showing systemd is active and running, along with system load and memory information.
Managing Services with systemctl
The systemctl command is your primary tool for service management. Let's explore its most common uses.
Viewing Service Status
Check if a service is running:
systemctl status ssh
The output shows:
- Whether the service is active (running) or inactive (stopped)
- The process ID (PID)
- Recent log entries
- Any error messages
Listing All Services
See all available services on your system:
systemctl list-units --type=service
For a more detailed view with descriptions:
systemctl list-units --type=service --all
Service Control Operations
Starting and Stopping Services
sudo systemctl start ssh
sudo systemctl stop ssh
Restarting and Reloading Services
sudo systemctl restart ssh
sudo systemctl reload ssh
Use restart when you want to completely stop and start a service. Use reload when you only need to apply configuration changes without interrupting active connections.
Enabling and Disabling Services
Control whether services start automatically at boot:
sudo systemctl enable ssh
sudo systemctl disable ssh
Practical Service Management Examples
Let's work with some common services you might encounter.
Managing the Nginx Web Server
First, let's install and experiment with nginx:
sudo apt update
sudo apt install nginx -y
Now manage the nginx service:
sudo systemctl start nginx
sudo systemctl enable nginx
systemctl status nginx
curl http://localhost
Managing the Cron Service
Cron handles scheduled tasks. Let's ensure it's running:
systemctl status cron
sudo systemctl restart cron
Service Configuration and Troubleshooting
Viewing Service Details
Get detailed information about a service:
systemctl show ssh
Checking Service Logs
View recent log entries for a service:
sudo journalctl -u ssh -f
The -f flag follows the log (like tail -f).
Masking Services
Prevent a service from being started, even manually:
sudo systemctl mask apache2
To unmask:
sudo systemctl unmask apache2
Common Pitfalls
- Forgetting sudo: Most
systemctlcommands require root privileges - Confusing restart vs reload: Use
reloadfor config changes,restartfor complete service restart - Not checking dependencies: Some services depend on others; check with
systemctl list-dependencies - Ignoring failed services: Always investigate services marked as "failed" in status
- Overlooking journal logs:
journalctl -u service-nameprovides crucial troubleshooting information
Summary
In this lesson, you've learned:
- System services are background processes managed by systemd
- The
systemctlcommand controls service lifecycle (start, stop, restart, reload) - Services can be enabled/disabled for automatic startup at boot
- Service status and logs provide crucial troubleshooting information
- Proper service management ensures system stability and security
You're now equipped to manage the essential background services that keep your Ubuntu system running efficiently.
Quiz
Show quiz
-
What command would you use to check if the SSH service is running?
- A)
systemctl check ssh - B)
systemctl status ssh - C)
service status ssh - D)
ps aux | grep ssh
- A)
-
Which command ensures a service starts automatically when the system boots?
- A)
systemctl start service-name - B)
systemctl enable service-name - C)
systemctl boot service-name - D)
systemctl auto service-name
- A)
-
What's the difference between
systemctl restartandsystemctl reload?- A) They do exactly the same thing
- B) Restart stops then starts; reload applies config changes
- C) Restart is for system services; reload is for user services
- D) Restart requires sudo; reload doesn't
-
How can you view real-time logs for a specific service?
- A)
systemctl logs service-name - B)
tail -f /var/log/service-name - C)
journalctl -u service-name -f - D)
cat /var/log/syslog | grep service-name
- A)
-
What does
systemctl mask service-namedo?- A) Hides the service from listing commands
- B) Prevents the service from being started
- C) Encrypts the service configuration
- D) Backs up the service files
Answers:
- B)
systemctl status ssh - B)
systemctl enable service-name - B) Restart stops then starts; reload applies config changes
- C)
journalctl -u service-name -f - B) Prevents the service from being started