On Linux servers, various services (such as web servers, databases, email services, etc.) need to be manually started to provide functionality and stopped to release resources. As a Linux newbie, mastering commands for starting, stopping, and checking the status of services is fundamental to server management. This article will explain these core commands in the simplest way to help you get started quickly.
一、What are Linux Services?¶
In simple terms, a service is a program that runs continuously in the background, such as:
- Nginx/Apache: Provides website access services
- MySQL/MariaDB: Provides database storage services
- SSH: Provides remote login services
- FTP: Provides file transfer services
These services need to be “started” to work properly and “stopped” to shut down or update.
二、Common Service Management Tool: systemctl (Must-Learn for Newbies)¶
Most modern Linux distributions (e.g., CentOS 7+/RHEL 7+/Ubuntu 16+) use systemd as the service management system, controlled by the systemctl command.
Core Format:
sudo systemctl [command] [service-name]
(sudo requires administrator privileges; regular users must add sudo to execute management commands.)
三、Detailed Explanation of systemctl Core Commands¶
| Command | Function | Example (Start Nginx) |
|---|---|---|
start |
Start a service | sudo systemctl start nginx |
stop |
Stop a service | sudo systemctl stop nginx |
restart |
Restart a service (stop then start) | sudo systemctl restart nginx |
reload |
Reload configuration (no stop needed) | sudo systemctl reload nginx |
status |
Check service status | sudo systemctl status nginx |
enable |
Set auto-start on boot | sudo systemctl enable nginx |
disable |
Disable auto-start on boot | sudo systemctl disable nginx |
四、Example: Managing Common Services¶
1. Start/Stop Web Server¶
- CentOS/RHEL: Web service name is
httpd(Apache)
sudo systemctl start httpd # Start Apache
sudo systemctl stop httpd # Stop Apache
- Ubuntu/Debian: Web service name is
apache2
sudo systemctl start apache2 # Start Apache
sudo systemctl stop apache2 # Stop Apache
2. Start/Stop Database Service¶
- CentOS/RHEL: Database service name is
mariadb(MySQL alternative)
sudo systemctl start mariadb # Start MariaDB
sudo systemctl stop mariadb # Stop MariaDB
- Ubuntu/Debian: Database service name is
mysql
sudo systemctl start mysql # Start MySQL
sudo systemctl stop mysql # Stop MySQL
3. Check Service Status¶
Use the status command to confirm if a service is running:
sudo systemctl status sshd # Check SSH service status (universal for all systems)
active (running)in the output indicates the service is running normally.inactive (dead)indicates the service is stopped.
4. Set Auto-Start on Boot¶
Make services start automatically on boot to avoid manual intervention:
sudo systemctl enable nginx # Enable auto-start for Nginx
sudo systemctl disable nginx # Disable auto-start for Nginx
五、Common Issues and Solutions¶
- Insufficient Permissions:
Regular users will get “Permission denied” when runningsystemctlcommands. Usesudo:
sudo systemctl start nginx
- Service Start Failure:
Usestatusor logs to troubleshoot:
sudo systemctl status [service-name] # Check error messages
sudo journalctl -u [service-name] # View detailed logs (e.g., Nginx config errors)
- Forgot Service Name:
List all services withlist-unit-files:
systemctl list-unit-files | grep [service-keyword] # e.g., grep nginx
六、Summary¶
- Core Commands:
start/stop/restart/status/enable/disable - Permissions: Require
sudoor root privileges - Service Name Differences: Service names may vary across distributions (e.g., CentOS
httpdvs Ubuntuapache2) - Troubleshooting Tips: Use
statusto check status andjournalctlto view logs
Practice these commands repeatedly to master service management on your server!