For advanced Linux users, managing services is a key skill. If you want to use a web server, like Apache, you need to start that service. To access a database, you’ll need to start MySQL. Effectively managing these services not only makes them accessible but also keeps your system stable and performing well.
Many users think that handling these tasks is complicated, but it’s pretty straightforward. Regardless of whether you’re on CentOS, Ubuntu, Red Hat, Fedora, or Debian, the basic commands are essentially the same.
Now, let’s clear up something. There are two main tools for managing these services: systemctl
and service
.
Systemctl
is the more powerful option. It lets you manage dependencies, enable or disable services, and even integrates with logs using journalctl
. On the other hand, service
is much simpler, mainly used for basic commands like start, stop, and status. It’s mostly found on older systems that use SysVinit.
Your choice between them depends on your Linux distribution. Most modern systems use systemd
, so you’ll likely rely on systemctl
. However, some administrators still prefer the old service
command out of habit. The good news is that even when using `system
d, the
service` command still works for basic functions.
Sometimes, you might encounter services that need manual intervention, located in /etc/rc.d
or /etc/init.d
. But for best practices in managing services, stick with systemctl
.
Starting a Linux Service
Let’s start with how to begin a service, like Apache.
- Open a terminal.
- Type:
sudo systemctl start httpd
.
Breaking that down:
sudo
lets you run the command with root privileges.systemctl
controls the service manager.start
is the action you’re taking – starting the service.httpd
is the name of the Apache service.
If successful, you’ll see: “The service httpd has started successfully.” If it’s already running, you’ll see: “The service httpd is already running.”
Common Error Messages
-
Failed to start httpd.service. Unit httpd.service not found.
This means the Apache package is missing. You can install it using:sudo apt install apache2
for Debian-based systems.sudo yum install httpd
for Red Hat-based systems.
- Failed to start httpd.service. Address already in use.
This means another process is using port 80. Usesudo lsof -i:80
to find the conflicting process and either stop it or change Apache’s port.
Stopping a Linux Service
To stop Apache, the steps are similar:
- Open a terminal.
- Type:
sudo systemctl stop httpd
.
You’ll see: “The service httpd has been stopped successfully.” If it wasn’t running, you’ll get: “Failed to stop service httpd. Unit httpd.service is not loaded.” You’ll need to install Apache in that case.
Other messages you could see include:
- “Failed to stop service httpd. Unit httpd.service is not running.” This just means it’s already stopped.
- “Failed to stop service httpd. Unit httpd.service is in a failed state.” Check logs with
sudo journalctl -xe
for troubleshooting. - “Failed to stop service httpd. Unit httpd.service is locked.” This means another process is controlling it. Wait a moment and try again or check for running processes with
ps aux | grep httpd
.
Restarting a Linux Service
To restart Apache:
- Open a terminal.
- Type:
sudo systemctl restart httpd
.
You should see: “The service httpd has been restarted successfully.”
If the service isn’t running, the message will be: “The service httpd is not running.” You can start it with sudo systemctl start httpd
or check its status with systemctl status httpd
.
If you see “Job for httpd.service failed,” then something’s wrong with the configuration. Check the error logs with sudo journalctl -xe
for more details.
Using the Service Command
If you prefer to stick to the service
command, it still works in most modern distributions:
- Starting:
sudo service httpd start
- Stopping:
sudo service httpd stop
- Restarting:
sudo service httpd restart
When you run these commands, they redirect to systemctl
, so you’ll get the same results without any errors. Want to dig deeper? Use the command man systemctl
for a detailed read on its capabilities.