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.
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:
Open a terminal in the LabEx VM environment. You should already be in the default directory, which is
/home/labex/project.Run the following command to check the status of the SSH service:
systemctl status sshdYou 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 startupsThis 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
If you need to check the status of a different service, you can replace
sshdwith 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:
To list all active services:
systemctl list-units --type=service --state=activeThis command shows all services that are currently active on your system.
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:
Create a file named
services_list.txtin the current directory by running:systemctl list-units --type=service --state=running > ~/project/services_list.txtThis command executes
systemctl list-unitsand redirects its output to a file namedservices_list.txtin your project directory.Verify that the file was created and contains data:
cat ~/project/services_list.txtYou 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:
First, check the current status of the SSH service:
systemctl status sshdNote whether the service is currently active or inactive.
Now, stop the SSH service:
sudo systemctl stop sshdThis command stops the service immediately. If successful, there is no output from this command.
Verify that the service has been stopped:
systemctl status sshdThe 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 agoStart the service again:
sudo systemctl start sshdVerify that the service has started:
systemctl status sshdThe output should show that the service is "active (running)" again.
Restarting Services
Restarting a service is a common operation, especially after changing its configuration:
To restart the SSH service:
sudo systemctl restart sshdThis command stops and then starts the service.
To verify the restart, check the service status:
systemctl status sshdLook 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:
Create a file named
service_control.txtin 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.txtVerify 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
To check if the SSH service is configured to start at boot:
systemctl is-enabled sshdThe 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:
Disable the SSH service:
sudo systemctl disable sshdYou should see a message indicating that the symlink was removed:
Removed /etc/systemd/system/multi-user.target.wants/ssh.service.Verify that the service is now disabled:
systemctl is-enabled sshdThe output should be "disabled".
Note that disabling a service doesn't stop it if it's currently running. Check the current status:
systemctl status sshdEven 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:
Re-enable the SSH service:
sudo systemctl enable sshdYou 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.Verify that the service is now enabled:
systemctl is-enabled sshdThe output should be "enabled".
Combined Commands
You can also combine enabling/disabling with starting/stopping in a single command:
To disable and stop a service in one command:
sudo systemctl disable --now sshdTo 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:
Create a file named
service_boot.txtin 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.txtVerify 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:
Checking service status: You learned how to view the current state of services, which is crucial for monitoring and troubleshooting.
Controlling services: You practiced starting, stopping, and restarting services, which are basic operations needed for maintenance and configuration changes.
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.



