How To Manage Services In Linux

Updated 4/8/2018

In this post, I will be showing you how to manage services in Linux. This includes listing all available services and their respective status, starting, stopping, checking status, and restarting services. This is something that everyone who runs a Linux server or desktop needs to know, and while the title may sound intimidating it’s really quite simple.

List All Available Services And Respective Status

The following commands can be used to check the status of every service running on your Linux server or desktop. This will work for both Debian or Ubuntu based distros as-well as Red Hat, CentOS, and Fedora based distros.

sudo service --status-all # This will list all services
sudo service --status-all | grep <service name> # This will look for a specific service

Check Status Of Specific Service

If you need more information on the status of a specific service, you can use the following command. Again this will work for both Debian or Ubuntu based distributions as-well as Red Hat, CentOS, and Fedora based distributions. The information that you receive will vary from service to service, however, you will usually get PID/s (process IDs) and at-least some other useful information related to that specific service. I have included an example below as-well so you can see what kind of information the service status command can provide.

sudo systemctl status <service name>

# The below command will work on older distributions
sudo /etc/init.d/<service name> status
~# systemctl status apache2
● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
   Active: active (running) since Fri 2017-09-08 04:35:27 UTC; 1 day 18h ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1585 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCE
    Tasks: 11
   Memory: 183.5M
      CPU: 8min 36.160s
   CGroup: /system.slice/apache2.service
           ├─ 1724 /usr/sbin/apache2 -k start
           ├─15654 /usr/sbin/apache2 -k start
           ├─15734 /usr/sbin/apache2 -k start
           ├─15746 /usr/sbin/apache2 -k start
           ├─15771 /usr/sbin/apache2 -k start
           ├─15841 /usr/sbin/apache2 -k start
           ├─15882 /usr/sbin/apache2 -k start
           ├─15883 /usr/sbin/apache2 -k start
           ├─15918 /usr/sbin/apache2 -k start
           ├─16330 /usr/sbin/apache2 -k start
           └─16904 /usr/sbin/apache2 -k start

Start , Stop, Restart, Or Reload A Specific Service

You can use the same command as the last few examples to start, stop, restart, and reload services. Please note that when applying configuration changes that require a service restart or reload, a good idea is to make a backup of whatever configuration file you change. This way you can revert back to the original configuration should the changes make the service unable to start (it happens!). I will include an example of how you can make backups of your important configuration files below.

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak # Backup configuration file
sudo systemctl start <service name> # Start service
sudo systemctl stop <service name> # Stop service
sudo systemctl restart <service name> # Restart service
sudo systemctl reload <service name> # Reload service

# The below commands will work on older distributions
sudo /etc/init.d/<service name> start # Start service
sudo /etc/init.d/<service name> stop # Stop service
sudo /etc/init.d/<service name> restart # Restart service
sudo /etc/init.d/<service name> reload # Reload service

Enable Or Disable Services During Boot/Startup

Last but not least, we need to be able to control whether or not we want a specific service to start when your desktop or server does. This will vary based on which distribution you are using as well as which version, I have included both the chkconfig command method and update-rc.d command methods.

sudo update-rc.d <service name> enable # Enable service on Ubuntu/Debian and Red Hat 6/CentOS 6 (and below)
sudo update-rc.d <service name> disable # Disable service on Ubuntu/Debian and Red Hat 6/CentOS 6 (and below)
sudo chkconfig <service name> on # Enable service on RHEL 7/CentOS 7
sudo chkconfig <service name> off # Disable service on RHEL 7/CentOS 7

I hope this post on how to manage Linux services was helpful. Please don’t forget to like/comment/share, thanks!

Leave a Reply

Protected with IP Blacklist CloudIP Blacklist Cloud