Systemd brings the concept of systemd units in Linux. For e.g., service unit, target unit, mount unit etc. are unit types with file extension as .service, .target, .mount respectively. The configurational file representing these units are stored inside the directories: /usr/lib/systemd/system/, /run/systemd/system/, /etc/systemd/system/
Earlier versions of Red Hat Enterprise Linux (RHEL) used init scripts. These scripts were written in BASH and were located in the directory “/etc/rc.d/init.d/”. These are scripts used to control the services and daemons. Later in RHEL 7, service units were introduced to replace the init scripts. Fedora, which is an upstream OS of Red Hat Enterprise Linux, has started using the systemd from the Fedora version 15.
Service units have .service file extensions and have similar roles as init scripts. “Systemd” uses the “systemctl” utility to manage system services. It can be used to view, start, stop, restart, enable or disable these services.
Advantages of Systemd Over Init System
- With systemd, we can prioritize necessary services over less significant services.
- Cgroups are used by systemd to keep track of processes and control the execution.environment.
- Systemd still supports the old init process and has more control.
- Systemd is capable of dealing with dynamic system configuration modifications.
What Will We Cover?
In this guide, we will learn about managing systemd processes. We will see how to enable and disable startup services at boot and how to do service operations like start, stop, restart, etc. We have performed the below exercises on Fedora 30 workstations, which will be most applicable to other Linux OSes.
List Startup Services at Boot in Fedora Linux
The old SysV method uses the service and chkconfig commands to manage the services. These commands are now replaced with the systemd commands like systemctl. Let us see some of the operations of “systemctl” on various services in Linux.
1. To list all the services running on your system, along with their states (enabled or disabled), use the command below:
A service can have three states: 1) enabled 2) disabled 3) static
An enabled service has a symlink in a .wants directory, whereas a disabled service does not have one. A static service does not have an install section in the corresponding init script. So, it cannot be enabled or disabled.
To get more details of the services, the below command should be used.
Summary of the above column names:
UNIT — systemd unit name (here a service name).
LOAD — Specify if the systemd unit was loaded correctly or not.
ACTIVE — State of the unit (here service).
SUB — A sub-state of a unit activation.
DESCRIPTION — A short info of the unit.
We can also use the following command:
or
The “/etc/inittab” is now replaced by “/etc/systemd/system/” in systemd. This directory now contains the symlinks to the files in the directory “/usr/lib/systemd/system”. The init scripts are placed in the “/usr/lib/systemd/system”. A service must be mapped to “/etc/systemd/system/” for starting it at system boot. For this purpose, the systemctl command is used in Fedora and other latest Linux systems.
2. Let us see the below example of enabling the httpd service:
Also, we can use the command below to filter all the enabled services:
or use the command:
3. To list all the active (running) services, use the command:
4. To see which services are enabled to start automatically on system boot, we can also use the following command:
5. Similarly, we can check the services disabled to start at boot with the command:
6. We can also see what time each service is taking at startup:
7. To check if a service is enabled for autostart at boot, use the command:
Put the name of service in place of xxx. E.g., in the case of httpd service, the command will be:
or
8. To check the status of a service, use the command:
For example, to check the status of the sshd service:
9. To check if a service is running or not, just run the below command:
For example, to check the telnet status:
10. To start a dead or inactive service, use the command:
For example, to start an sshd service:
11. To disable a service at system boot
For example, to disable the httpd service:
or
12. To restart a running service
To restart the sshd service, use the command:
If the service is not already running, it will be started.
13. To reload a running service
For example, reload the httpd service with:
This command reloads the configuration of a specific service. To reload the unit configuration file of systemd, we need the command:
14. To list all the dependencies of a service:
In the case of httpd service, the command will be:
Conclusion
In this guide, we have seen various ways of managing services with systemd utility like enabling services at boot time, starting and stopping them, etc. If you were used to the service command of old Sysvinit, you should switch to systemd as it has more features and it is the default init system in newer versions of Fedora, RHEL, and most of the other major Linux distributions.