Back to Blog

Operate system services on Ubuntu with systemctl: start, enable and analyze

Complete tutorial on systemctl in Ubuntu. Learn start/stop/restart, enable/disable, status, unit files, logs.

Operate system services on Ubuntu with systemctl: start, enable and analyze

Operate System Services on Ubuntu with systemctl: Start, Enable, and Analyze

Introduction

In modern Ubuntu systems, systemd serves as the system and service manager, replacing the traditional SysVinit system. At the heart of systemd's functionality is the command-line tool systemctl, which allows users to manage and control system services, also known as units. Understanding how to use systemctl is crucial for system administrators and users alike, as it enables the configuration, monitoring, and management of background services that are essential to the operating system's operation.

From web servers to database services, managing these units effectively is vital for system reliability and performance. Whether you are troubleshooting an unresponsive service or ensuring that essential services start on boot, mastering systemctl will empower you to maintain your Ubuntu systems efficiently. This tutorial will guide you through the essential commands to start, enable, and analyze system services using systemctl, ensuring that you become proficient in managing your system's services.

Prerequisites

  • Ubuntu Version: This tutorial is applicable for Ubuntu 16.04 and later.
  • Required Packages: Ensure you have systemd installed, which is the default in modern Ubuntu distributions.
  • Permissions: You will need sudo privileges to manage services.
  • Risks: Improperly managing services can lead to system instability or security vulnerabilities. Always ensure that you understand the implications of starting or stopping critical services.

Core Concepts

Terminology

  • systemd: A system and service manager for Linux, responsible for initializing the system and managing services.
  • Unit: A service, socket, device, mount point, or other resource managed by systemd.
  • Service Unit: A specific type of unit that represents a service.

Architecture

systemd operates using unit files that define how services should behave. These files are typically located in /etc/systemd/system/ or /lib/systemd/system/.

When to Use

You should use systemctl to manage services when you need to start, stop, or check the status of services running on your system.

Limits

While systemctl is powerful, it may not work optimally with all services, especially those relying on older init systems. It's also crucial to know the correct unit file name to manage the desired service.

Syntax/Commands

Command Description Flags/Parameters
systemctl start <unit> Starts a specified service.
systemctl stop <unit> Stops a specified service.
systemctl restart <unit> Restarts a specified service.
systemctl enable <unit> Enables a service to start at boot.
systemctl disable <unit> Disables a service from starting at boot.
systemctl status <unit> Shows the status of a specified service.
systemctl is-active <unit> Checks if a service is currently running.
journalctl -u <unit> Displays logs for a specified service. -f (follow logs)

Practical Examples

1. Start a Service

To start a service, use:

sudo systemctl start apache2

This command will start the Apache web server.

2. Stop a Service

To stop a running service:

sudo systemctl stop apache2

This command will halt the Apache service.

3. Restart a Service

To restart a service, which is useful for applying configuration changes:

sudo systemctl restart apache2

This command will stop and then start the Apache service.

4. Enable a Service

To enable a service to start on boot:

sudo systemctl enable apache2

This command ensures that the Apache service starts automatically when the system boots.

5. Disable a Service

To prevent a service from starting at boot:

sudo systemctl disable apache2

This command disables the automatic start of the Apache service on boot.

6. Check Service Status

To check the status of a service:

sudo systemctl status apache2

This command provides detailed information about the Apache service, including its current state and any error messages.

7. Check If a Service is Active

To verify if a service is currently active:

sudo systemctl is-active apache2

This command returns "active" if the service is running.

8. View Service Logs

To view logs for a specific service:

journalctl -u apache2

Use the -f flag to follow logs in real-time:

journalctl -u apache2 -f

This command will show the latest log entries for the Apache service.

Real-World Scenarios

Scenario 1: Web Server Management

Suppose you are managing a web server running Apache. You need to ensure the service starts automatically after a reboot and troubleshoot after it crashes unexpectedly. You would use:

sudo systemctl enable apache2
sudo systemctl restart apache2
sudo systemctl status apache2
journalctl -u apache2 -f

Scenario 2: Troubleshooting a Failing Service

If a service fails, you first check its status and logs:

sudo systemctl status myservice
journalctl -u myservice

Based on the logs, you may need to restart the service or modify its configuration before starting it again.

Scenario 3: Disabling Unused Services

If you have a service that is no longer needed and could pose a security risk, disable it:

sudo systemctl stop unused-service
sudo systemctl disable unused-service

This ensures that the service does not run unnecessarily.

Best Practices

  1. Regularly Monitor Services: Use systemctl status and journalctl to keep an eye on the health of your services.
  2. Disable Unused Services: Reducing the number of running services can minimize potential attack vectors.
  3. Use systemctl Commands Safely: Be cautious when restarting or stopping critical services.
  4. Document Changes: Maintain a log of any changes made to service configurations for future reference.
  5. Automate Startup Processes: Use systemctl enable for services that you know are required at boot.

Common Errors

1. Error: Failed to start service

Message: "Job for apache2.service failed because the control process exited with error code."

  • Cause: The service may have a configuration error.
  • Fix: Check the service logs with journalctl -u apache2 for details about the failure.

2. Error: Unit file not found

Message: "Unit apache2.service not found."

  • Cause: The specified service does not exist.
  • Fix: Verify the service name and ensure it is installed.

3. Error: Permission denied

Message: "Failed to start apache2.service: Access denied."

  • Cause: Insufficient permissions.
  • Fix: Use sudo to execute the command.

4. Error: Cannot disable unit

Message: "Failed to disable unit: Unit apache2.service is masked."

  • Cause: The service is masked, meaning it has been disabled and cannot be started.
  • Fix: Unmask the service with sudo systemctl unmask apache2.

Related Commands

Command Description
service Older command to manage services (deprecated).
init.d Script-based service management (SysVinit).
systemd-analyze Analyzes system boot-up performance.
systemctl list-units Lists all active units.

Automation Script

Here’s a complete bash script to automate the management of a service (e.g., Apache):

#!/bin/bash

# This script manages the Apache service

SERVICE="apache2"

# Function to start the service
start_service() {
    echo "Starting $SERVICE..."
    sudo systemctl start $SERVICE
}

# Function to enable the service
enable_service() {
    echo "Enabling $SERVICE to start at boot..."
    sudo systemctl enable $SERVICE
}

# Function to check the status of the service
check_status() {
    echo "Checking status of $SERVICE..."
    sudo systemctl status $SERVICE
}

# Function to view logs for the service
view_logs() {
    echo "Viewing logs for $SERVICE..."
    journalctl -u $SERVICE -f
}

# Main script execution
case "$1" in
    start)
        start_service
        ;;
    enable)
        enable_service
        ;;
    status)
        check_status
        ;;
    logs)
        view_logs
        ;;
    *)
        echo "Usage: $0 {start|enable|status|logs}"
        exit 1
        ;;
esac

Conclusion

In this tutorial, you learned how to manage system services on Ubuntu using the powerful systemctl command. By understanding how to start, enable, and analyze services, you can ensure that your system runs smoothly and efficiently. As a next step, consider exploring more advanced functionalities of systemd, such as creating custom unit files and managing service dependencies.

References