Linux Service Managing

LinuxLinuxBeginner
Practice Now

Introduction

Welcome to this hands-on lab on Linux service management. Services in Linux are background processes that handle critical system functions such as network connectivity, logging, printing, and more. Managing these services efficiently is an essential skill for Linux administrators.

In this lab, you will learn how to use the systemctl command, which is part of the systemd suite - the standard init system and service manager for most modern Linux distributions. You will practice checking service status, controlling services by starting and stopping them, and configuring service behavior at system boot time.

By the end of this lab, you will understand the basics of Linux service management, enabling you to effectively control system services in a Linux environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/InputandOutputRedirectionGroup(["Input and Output Redirection"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/InputandOutputRedirectionGroup -.-> linux/redirect("I/O Redirecting") linux/SystemInformationandMonitoringGroup -.-> linux/service("Service Managing") subgraph Lab Skills linux/echo -.-> lab-271377{{"Linux Service Managing"}} linux/cat -.-> lab-271377{{"Linux Service Managing"}} linux/redirect -.-> lab-271377{{"Linux Service Managing"}} linux/service -.-> lab-271377{{"Linux Service Managing"}} end

Understanding and Checking System Service Status

In this step, you will learn how to check the status of system services using the systemctl command. Understanding service status is the first step to effective service management.

What are System Services?

System services (or daemons) are programs that run in the background to perform various system tasks. They typically start at boot time and continue running until the system shuts down. Examples include web servers, database servers, SSH servers, and printing services.

Checking Service Status

Let's start by checking the status of the SSH service (sshd), which is commonly used for remote access to Linux systems:

  1. Open a terminal in the LabEx VM environment. You should already be in the default directory, which is /home/labex/project.

  2. Run the following command to check the status of the SSH service:

    systemctl status sshd

    You should see output similar to this:

    ● ssh.service - OpenBSD Secure Shell server
      Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
      Active: active (running) since Wed 2023-10-25 08:15:00 UTC; 1h 30min ago
        Docs: man:sshd(8)
              man:sshd_config(5)
     Process: 1234 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
    Main PID: 1235 (sshd)
       Tasks: 1 (limit: 4915)
      Memory: 5.6M
         CPU: 500ms
      CGroup: /system.slice/ssh.service
              └─1235 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups

    This output provides important information about the service:

    • Loaded: Shows if the service is loaded and whether it's enabled to start at boot
    • Active: Shows if the service is currently running
    • Process/Main PID: Displays the process ID of the service
    • Memory/CPU: Shows resource usage
    • Logs: Recent log entries related to the service
  3. If you need to check the status of a different service, you can replace sshd with the name of that service. For example, to check the status of the Apache web server (if installed):

    systemctl status apache2

Listing All Services

You can list all services on your system with the following command:

  1. To list all active services:

    systemctl list-units --type=service --state=active

    This command shows all services that are currently active on your system.

  2. To list all services regardless of their state:

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

Creating a Service List File

Now, let's create a file to store a list of all running services on the system:

  1. Create a file named services_list.txt in the current directory by running:

    systemctl list-units --type=service --state=running > ~/project/services_list.txt

    This command executes systemctl list-units and redirects its output to a file named services_list.txt in your project directory.

  2. Verify that the file was created and contains data:

    cat ~/project/services_list.txt

    You should see a list of all running services on your system.

In this step, you've learned how to check the status of services and how to create a list of all running services on your system. This information is valuable for understanding which services are active and how they're configured.

Controlling System Services: Start, Stop, and Restart

In this step, you will learn how to start, stop, and restart system services. These operations are essential for service maintenance, troubleshooting, and applying configuration changes.

Starting and Stopping Services

Let's practice controlling the SSH service:

  1. First, check the current status of the SSH service:

    systemctl status sshd

    Note whether the service is currently active or inactive.

  2. Now, stop the SSH service:

    sudo systemctl stop sshd

    This command stops the service immediately. If successful, there is no output from this command.

  3. Verify that the service has been stopped:

    systemctl status sshd

    The output should now show that the service is "inactive (dead)":

    ● ssh.service - OpenBSD Secure Shell server
      Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
      Active: inactive (dead) since Wed 2023-10-25 10:15:00 UTC; 5s ago
  4. Start the service again:

    sudo systemctl start sshd
  5. Verify that the service has started:

    systemctl status sshd

    The output should show that the service is "active (running)" again.

Restarting Services

Restarting a service is a common operation, especially after changing its configuration:

  1. To restart the SSH service:

    sudo systemctl restart sshd

    This command stops and then starts the service.

  2. To verify the restart, check the service status:

    systemctl status sshd

    Look at the "Active" line to see when the service was last started.

Creating a Reference File for Service Control Commands

Let's create a reference file with the commands you've learned:

  1. Create a file named service_control.txt in your project directory:

    echo "Service Control Commands:" > ~/project/service_control.txt
    echo "Start a service: sudo systemctl start <service_name>" >> ~/project/service_control.txt
    echo "Stop a service: sudo systemctl stop <service_name>" >> ~/project/service_control.txt
    echo "Restart a service: sudo systemctl restart <service_name>" >> ~/project/service_control.txt
  2. Verify the contents of the file:

    cat ~/project/service_control.txt

In this step, you've learned how to control services by starting, stopping, and restarting them. These operations are fundamental for service management and maintenance in Linux systems.

Managing Service Boot Behavior: Enable and Disable

In this step, you'll learn how to configure whether services start automatically when the system boots. This is important for ensuring that required services are available without manual intervention after a system restart.

Understanding Service Boot Configuration

Services can be configured to start automatically at boot time (enabled) or to require manual starting (disabled). This configuration is separate from the current running state of the service.

Checking If a Service Is Enabled

  1. To check if the SSH service is configured to start at boot:

    systemctl is-enabled sshd

    The output will be either "enabled" (starts at boot) or "disabled" (doesn't start at boot).

Disabling a Service

When you disable a service, you're configuring it not to start automatically at boot time:

  1. Disable the SSH service:

    sudo systemctl disable sshd

    You should see a message indicating that the symlink was removed:

    Removed /etc/systemd/system/multi-user.target.wants/ssh.service.
  2. Verify that the service is now disabled:

    systemctl is-enabled sshd

    The output should be "disabled".

  3. Note that disabling a service doesn't stop it if it's currently running. Check the current status:

    systemctl status sshd

    Even though the service is now disabled for the next boot, it may still be active.

Enabling a Service

When you enable a service, you're configuring it to start automatically at boot time:

  1. Re-enable the SSH service:

    sudo systemctl enable sshd

    You should see a message indicating that the symlink was created:

    Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/systemd/system/ssh.service.
  2. Verify that the service is now enabled:

    systemctl is-enabled sshd

    The output should be "enabled".

Combined Commands

You can also combine enabling/disabling with starting/stopping in a single command:

  1. To disable and stop a service in one command:

    sudo systemctl disable --now sshd
  2. To enable and start a service in one command:

    sudo systemctl enable --now sshd

Creating a Reference File for Service Boot Configuration

Let's create a reference file with the commands you've learned:

  1. Create a file named service_boot.txt in your project directory:

    echo "Service Boot Configuration Commands:" > ~/project/service_boot.txt
    echo "Check if a service is enabled: systemctl is-enabled <service_name>" >> ~/project/service_boot.txt
    echo "Enable a service to start at boot: sudo systemctl enable <service_name>" >> ~/project/service_boot.txt
    echo "Disable a service from starting at boot: sudo systemctl disable <service_name>" >> ~/project/service_boot.txt
    echo "Enable and immediately start a service: sudo systemctl enable --now <service_name>" >> ~/project/service_boot.txt
    echo "Disable and immediately stop a service: sudo systemctl disable --now <service_name>" >> ~/project/service_boot.txt
  2. Verify the contents of the file:

    cat ~/project/service_boot.txt

In this step, you've learned how to manage service boot behavior by enabling and disabling services. This is crucial for configuring which services start automatically at system boot, ensuring that necessary services are available while unnecessary ones don't consume resources.

Summary

In this lab, you have learned the fundamentals of Linux service management using the systemctl command. You have practiced essential skills that every Linux administrator should know:

  1. Checking service status: You learned how to view the current state of services, which is crucial for monitoring and troubleshooting.

  2. Controlling services: You practiced starting, stopping, and restarting services, which are basic operations needed for maintenance and configuration changes.

  3. Managing boot behavior: You learned how to enable and disable services at system startup, allowing you to control which services run automatically after a reboot.

These skills provide the foundation for effective service management in Linux environments. As you continue to work with Linux systems, you will find these commands essential for maintaining system stability and performance.

Remember that proper service management is critical for system security, resource optimization, and ensuring that necessary functionality is available when needed. The knowledge you gained in this lab will serve as a building block for more advanced Linux administration tasks.