Linux Skills: Manage System Services
System administrators are responsible for service management on Linux devices. Here is what they need to know about configuration, startup options, security and more.
Jun 9th, 2024 6:00am by
Feature image by Derek Oyen on Unsplash.
What Are Some Common Services?
There are several services you will probably encounter regularly on most Linux systems. You’ll likely manipulate or check their status as part of configuration and troubleshooting tasks. The following list offers some sample services to be familiar with and a summary of their function.- sshd : The Secure Shell (ssh) is an essential Linux remote administration tool.
- httpd : The Apache web server is a standard web server service on Linux systems.
- firewalld : The firewall filters network traffic in and out of the system using rules to determine what to allow or block.
- cupsd : The Common Unix Printing System (CUPS) offers excellent management of print servers.
- rsyslogd : The rsyslog service manages system and application log files.
Check Service Status
The service management command on modern Linux devices is systemctl . The syntax is command, subcommand, service name. It looks like this:
systemctl <subcommand> <servicename>
$ systemctl status sshd
Figure 1: The systemctl status subcommand displays the current status of the service. In this case, the service is loaded and active.
- Unit Status
- active (running) – Service is running (this is usually the desired result).
- inactive – Service is not running (you may have stopped it).
- failed – Service failed and is not running.
- Loaded Status
- loaded – Unit configuration file has loaded.
- error – Unit configuration file failed to load.
- Enabled Status
- enabled – Service starts automatically with the system.
- disabled – Service does not start automatically with the system.
Display Subcommands With the Tab Key
Commands like systemctl or ip have many subcommands, and it may be challenging to remember them all. One trick for displaying the available subcommands is to use the Linux tab-completion feature. Type the command, enter a space, and then press the Tab key twice. The available subcommands appear.
$ systemctl <tab><tab>
What Management Do Services Need?
What kinds of management do services need? Services require security settings, configuration options, resource access, network access, etc. Linux stores these configurations in text files. Each service has one or more text files. When the service starts up (usually when the computer boots), it reads the text file and applies the settings. That’s an important detail. Services use the settings they find in the text file when they start. If an administrator changes these settings, the service must be restarted to cause it to reread the configuration file and apply the new settings. Therefore, one of the first sysadmin tasks for managing services is restarting them.Start, Stop or Restart Services
Use the systemctl command to manage services. The command recognizes many subcommands, including a restart option. The syntax is:
systemctl restart <servicename>
$ sudo systemctl restart sshd
Figure 2: Restarting a service, perhaps because of a configuration change.
$ systemctl reload sshd
$ sudo systemctl stop sshd
$ sudo systemctl start sshd
Figure 3: Manually stopping and starting a service, perhaps during troubleshooting.
Configure Services to Start Automatically
Starting and stopping services only manages their status during the current system runtime (the current instance of the system). Administrators typically need to instruct services to start automatically when the system boots or not to start when the system boots. The applicable subcommands for these settings are enable and disable . To cause the SSH service to start automatically when the system boots, type:
$ sudo systemctl enable sshd
$ sudo systemctl disable sshd
Service Management Example
Suppose you’ve just installed the Apache web service on your Linux system and want to manage it. After editing the configuration file, your next step is the start the service so you can test whether it works as expected. Once satisfied, enable it so it launches when the system starts. Finally, confirm the service is running by using the status subcommand. Here’s an example of the commands:
$ sudo systemctl start httpd
$ sudo systemctl enable httpd
$ systemctl status httpd
$ sudo systemctl enable --now sshd
Check the Startup Configuration of Services
The is-enabled subcommand is useful for checking the startup status of a service without making any changes.
$ systemctl is-enabled ssh
- enabled – Service runs automatically.
- disabled – Service does not run automatically.
- static – Service runs when called by another service.
$ systemctl is-active ssh
Figure 4: Checking whether a service is enabled and active. Note the service name is 'ssh' in this case.
Prevent Services From Starting
Use the mask and unmask subcommands to prevent or allow services to start. The mask subcommand stops the service from being manually started with the systemctl start command or by being called by another service. The unmask subcommand reverses the setting, allowing the service to run if started. Use the same systemctl syntax you learned above with these two subcommands.Wrap Up
Service management is a daily function for Linux sysadmins. Luckily, the systemctl syntax is fairly straightforward. You’ll mainly use the status and restart subcommands. Don’t forget how services discover their configuration settings: They read a configuration file when they start, and they only read it again (to find your changes) if you restart them. That means restarting services is an essential step in system and service configuration. While this article does not focus directly on security, it’s worth noting that service management is a critical part of hardening. Hardening a system includes removing everything it does not need for its specified role. That means using systemctl to disable any unnecessary or unused services. Use your lab computer to practice the systemctl command and explore the various subcommands. Be sure to practice the tab-completion trick to display available subcommands I introduced at the start of the article. Learning this tool today will make your Linux administration journey much easier.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.