Service management is a day-to-day task for many Linux system administrators. Now that we have left behind tools such as service and chkconfig, it's time to get comfortable with the systemctl command and all that it offers for service management.
I will use a basic Apache web server deployment as my example. Apache isn't the focus, so be sure to look at all of the necessary security and performance configurations before deploying the web server. My focus is on managing the service with systemctl rather than the service itself.
If you want to follow along, use a package manager and grab the Apache package.
On Red Hat, Fedora, CentOS and similar, type:
$ sudo dnf install -y httpd
On Debian, Ubuntu and similar, type:
$ sudo apt update
$ sudo apt install apache2
Note that you may need to use sudo for some or all of the commands in this article, depending on how your distribution is configured.
Don't forget to edit the firewall configuration. Your distribution probably uses firewalld, so type the following two commands:
$ sudo firewall-cmd --zone=public --add-service=http --permanent
$ sudo firewall-cmd --reload
The systemctl Syntax
At this point, Apache is installed, and the firewall accepts remote connections. However, Apache itself is not yet running or servicing requests. That's where systemctl comes in.
The systemctl command has many useful subcommands that allows you to manage and query system services.
One of my favorite tricks to teach new Linux users is Bash's tab completion feature and how to use it to display subcommands for tools like systemctl. You should already know that if you type in part of a command or filename, you can hit tab and Bash will autocomplete the name for you. If you didn't type in enough to make the filename unique, you can hit tab twice and it displays the possible options.
Tab completion works similarly with utilities that rely on subcommands, such as ip and systemctl.
Type in systemctl and add a space, then hit the tab key twice. You should see a complete list of all available subcommands. Try the same trick with the ip command.
Don't forget that space after the primary command. Use this trick instead of trying to remember every possible subcommand.
The syntax for systemctl is command subcommand service.
Use systemctl To Start and Stop the Service
Notice two options as you examine the subcommands list: start and stop. Apache is installed, but you haven't yet started the service. Use the following command to do so:
$ sudo systemctl start httpd
This command should silently succeed. However, use the systemctl status command to confirm the functionality of httpd.
$ systemctl status httpd
You could test the functionality by connecting via a web browser at http://localhost. You can also use wget or curl to pull the test index page.
$ wget http://localhost/index.html
The command to stop Apache is similar.
$ sudo systemctl stop httpd
Check its status again.
However, another user or an automated process could start the service without your intervention. Mask the service to prevent this. Masking sets its startup configuration location as /dev/null, which, of course, contains nothing.
$ sudo systemctl mask httpd
If you're following the steps in this article on your own system, start Apache back up.
$ sudo systemctl start httpd
Now that Apache is running, it's time to configure it.
Edit the Configuration File and Restart
Use your favorite text editor to edit the /etc/httpd/conf/httpd.conf file and configure the web server the way you want it. After modifying a configuration file, you must restart the service so that it reads the file and applies the changes.
$ sudo systemctl restart httpd
Your web server is now available! Unfortunately, if you reboot the system, Apache won't automatically start. Starting (and stopping) a service only applies to the current runtime. You must enable a service for it to be persistent.
Enable Apache on System Startup
Use the enable and disable subcommands to control whether Apache starts when the system boots.
Remember, enabling and starting are two different things. If you enable a service, it still isn't running (unless you reboot). Therefore, you need to both start and enable the web server.
$ sudo systemctl start httpd
$ sudo systemctl enable httpd
There is a trick. Try the following (don't forget the two dashes):
$ sudo systemctl --now enable httpd
This command enables the service immediately.
For security or troubleshooting reasons, you might want to disable a service. Use the systemctl disable subcommand for this task.
Remember, this does not stop the service. Like the relationship between start and enable, you must both stop and disable a service to halt it and remove its persistency.
To test this, disable the service and then reboot. Check the service's status after the reboot.
$ sudo systemctl disable httpd
$ sudo reboot
Use systemctl To Troubleshoot Issues
The systemctl command also helps with troubleshooting. I already covered the status subcommand, but there are several other useful examples.
If your service doesn't seem to be working correctly, you can verify whether it is active and enabled.
$ sudo systemctl is-active httpd
$ sudo systemctl is-enabled httpd
Try starting, stopping, enabling and disabling httpd along with the above commands to see how the status changes.
You can display all active services by using the list-units subcommand. Add the --failed flag to see any that have not started correctly.
$ sudo systemctl list-units | grep -i httpd
$ sudo systemctl list-units --failed
Modern Linux distributions use systemd to manage the system and services. It can also help with troubleshooting, especially for performance issues. The first command below shows the startup times for all services. The second displays the services with the slowest startup times and uses grep to display httpd specifically.
$ sudo systemd-analyze
$ sudo systemd-analyze blame | grep -i httpd
Learn the Skills You Need With CompTIA Linux+
I find that investigating and managing services with systemctl is easier than with earlier tools such as service and chkconfig. This is the kind of command that Linux administrators find themselves using many times a day. Knowledge of systemctl will be helpful with the new CompTIA Linux+ (XK0-005) certification exam, too.
CompTIA Linux+ is an intermediate-level IT certification and is the only job-focused Linux certification covering the latest foundational skills demanded by hiring managers. CompTIA Linux+ validates the competencies required of an early career supporting Linux systems.
Ready to get started? Download the CompTIA Linux+ exam objectives for free to see what's covered.