Skip to main content

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 systemctl command 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.

tip

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:

Check systemd status
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:

Check SSH service status
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:

List all services
systemctl list-units --type=service

For a more detailed view with descriptions:

List services with descriptions
systemctl list-units --type=service --all

Service Control Operations

Starting and Stopping Services

Start the SSH service
sudo systemctl start ssh
Stop the SSH service
sudo systemctl stop ssh

Restarting and Reloading Services

Restart SSH service (stops then starts)
sudo systemctl restart ssh
Reload SSH service (reloads configuration)
sudo systemctl reload ssh
note

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:

Enable SSH to start at boot
sudo systemctl enable ssh
Disable SSH from starting at boot
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:

Install nginx
sudo apt update
sudo apt install nginx -y

Now manage the nginx service:

Start and enable nginx
sudo systemctl start nginx
sudo systemctl enable nginx
Verify nginx is running
systemctl status nginx
curl http://localhost

Managing the Cron Service

Cron handles scheduled tasks. Let's ensure it's running:

Check and manage cron service
systemctl status cron
sudo systemctl restart cron

Service Configuration and Troubleshooting

Viewing Service Details

Get detailed information about a service:

Show service details
systemctl show ssh

Checking Service Logs

View recent log entries for a service:

View service logs
sudo journalctl -u ssh -f

The -f flag follows the log (like tail -f).

Masking Services

Prevent a service from being started, even manually:

Mask a service
sudo systemctl mask apache2

To unmask:

Unmask a service
sudo systemctl unmask apache2

Common Pitfalls

  • Forgetting sudo: Most systemctl commands require root privileges
  • Confusing restart vs reload: Use reload for config changes, restart for 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-name provides crucial troubleshooting information

Summary

In this lesson, you've learned:

  • System services are background processes managed by systemd
  • The systemctl command 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
  1. 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
  2. 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
  3. What's the difference between systemctl restart and systemctl 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
  4. 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
  5. What does systemctl mask service-name do?

    • 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:

  1. B) systemctl status ssh
  2. B) systemctl enable service-name
  3. B) Restart stops then starts; reload applies config changes
  4. C) journalctl -u service-name -f
  5. B) Prevents the service from being started