In a Linux system, a “service” can be understood as a program that runs continuously in the background, responsible for providing specific functionalities such as web services (Nginx/Apache), database services (MySQL), email services (Postfix), and more. Managing services is a fundamental skill in Linux system operations and maintenance, including starting new services, stopping unnecessary ones, and checking if services are running properly.

I. Prerequisite for Service Management: Permission Preparation

Most service management commands (such as start, stop, restart) require administrative privileges. Therefore, these commands are typically executed with sudo (for regular users) or by switching to the root user (with caution). For example:

sudo systemctl start nginx  # Executed by a regular user, requiring password authentication

II. Checking Service Status: The Most Common Command systemctl status

systemctl status is the core command to check service status, displaying whether the service is running, its current state (e.g., active, inactive, failed), process ID, startup time, and other detailed information.

Syntax:

sudo systemctl status [service_name]

Example: Check the status of the Nginx service (assuming Nginx is installed):

sudo systemctl status nginx

Output Explanation:
- Active: active (running): The service is running (green indicates a normal state).
- Main PID: 1234: The main process ID of the service.
- CGroup: Information about the process group to which the service belongs.

If the service is not running, the status will show inactive (stopped) or failed (startup failed), along with an error reason.

III. Starting a Service: systemctl start

To start a service, use the systemctl start command. Note: The service name must match the actual configuration name in the system (confirmed via the “View Service List” command later).

Syntax:

sudo systemctl start [service_name]

Example: Start the Nginx service:

sudo systemctl start nginx

Verify Startup: After starting, confirm the status is active (running) using systemctl status nginx, or verify via port listening (e.g., Nginx defaults to listening on port 80):

sudo netstat -tuln | grep 80  # Check if port 80 is occupied by Nginx

IV. Stopping a Service: systemctl stop

To temporarily stop a service (e.g., during maintenance), use the systemctl stop command.

Syntax:

sudo systemctl stop [service_name]

Example: Stop the Nginx service:

sudo systemctl stop nginx

Verify Stopping: After executing systemctl status nginx, the status should show inactive (dead).

V. Restarting a Service: systemctl restart

When service configurations are modified (e.g., changing Nginx configuration files), restart the service to apply changes using systemctl restart.

Syntax:

sudo systemctl restart [service_name]

Example: Restart the Nginx service:

sudo systemctl restart nginx

Explanation: restart stops the service first and then restarts it, suitable for scenarios requiring configuration changes to take effect.

VI. Viewing Service List: systemctl list-units

If you forget the service name, use systemctl list-units to list all services in the system (or filter by specific types).

View All Services:

sudo systemctl list-units --type=service
  • --type=service: Only display services (excluding targets, mount points, etc.).
  • In the output, the LOAD column shows whether the service configuration is loaded, and the ACTIVE column shows the running status.

View Running Services:

sudo systemctl list-units --type=service --state=running

VII. Checking if a Service is Running: systemctl is-active

To quickly determine if a service is in an “active” state (returns only the result, no detailed information), suitable for scripts or automation scenarios.

Syntax:

sudo systemctl is-active [service_name]

Example: Check the status of Nginx:

sudo systemctl is-active nginx  # Output: active (service running) or inactive (service stopped)

VIII. Setting Up Automatic Startup on Boot (Optional)

To enable a service to start automatically on boot, use systemctl enable; to disable it, use systemctl disable.

Syntax:

sudo systemctl enable [service_name]   # Enable automatic startup on boot
sudo systemctl disable [service_name]  # Disable automatic startup on boot

Example: Set Nginx to start automatically on boot:

sudo systemctl enable nginx

Verify Automatic Startup:

sudo systemctl is-enabled nginx  # Output: enabled (set) or disabled (not set)

IX. Troubleshooting: Viewing Service Logs

If a service fails to start (e.g., systemctl status shows failed), use journalctl to view detailed error logs and identify issues.

Syntax:

sudo journalctl -u [service_name]  # -u specifies the service

Example: View logs for a failed Nginx startup:

sudo journalctl -u nginx

Common Errors:
- Port already occupied (e.g., port 80 used by another service).
- Incorrect syntax in configuration files (check files like /etc/nginx/nginx.conf).

Summary

The core of Linux service management is the systemctl command. Mastering the following basic operations covers most scenarios:
- Check status: systemctl status [service_name]
- Start service: sudo systemctl start [service_name]
- Stop service: sudo systemctl stop [service_name]
- Restart service: sudo systemctl restart [service_name]
- Automatic startup: sudo systemctl enable/disable [service_name]

It is recommended to practice managing different services (e.g., Nginx, MySQL, SSH) to become familiar with service names and common troubleshooting methods, gradually enhancing Linux system operation and maintenance capabilities.

Xiaoye