Linux systems rely on a multitude of services to perform a wide range of tasks, from networking and security to system monitoring and application support.
In this comprehensive guide, we will delve into the heart of Linux service management, demystifying the process of checking whether a service is enabled on your Linux system.
Table of Contents
Understanding systemd in Linux
Systemd is a system and service manager for Linux operating systems. It is a replacement for the traditional SysVinit system used in many Unix-like systems, including Linux distributions.
To check if your Linux system is systemd-controlled, you can use various methods, depending on the specific command or approach you prefer.
Boost Your Website Speed!
If you want your website to run as fast as ours, consider trying Cloudways. Their powerful cloud infrastructure and optimized stack deliver exceptional performance. Free migration!Here are a few ways to determine whether systemd is the init system:
Check the Process with PID 1:
You can check the process with PID 1, which is always the init process. Use the ps command to see the name of the init process:
ps -p 1 -o comm=
If the output is “systemd,” your system is using systemd as the init system.
Check for systemd Executables:
You can check if systemd-related executables are present in your system’s PATH. Try running the following command:
which systemd
If it returns a path like /bin/systemd or /usr/lib/systemd/systemd, it means systemd is installed and likely controlling your system.
Check the init Process Symbolic Link:
Another method is to check the symbolic link of the /sbin/init process, which typically points to the init system in use. Use the following command:
ls -l /sbin/init
If it points to systemd (e.g., /lib/systemd/systemd), then your system is using systemd as the init system.
check if a service is enabled with systemctl command
In the context of systemd, when we say a service is “enabled,” it means that the service is configured to start automatically during the system’s boot process or when specific conditions are met.
Systemctl is a powerful command-line utility used to manage services, units, and other aspects of the systemd init system in Linux.
It provides a unified and consistent way to interact with system services, targets, sockets, timers, and more.
For example, to check the status of a specific service, use:
systemctl status servicename
For example:
$ systemctl status postgresql-14
* postgresql-14.service - PostgreSQL 14 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-14.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2023-08-10 06:11:02 GMT; 1 months 0 days ago
Docs: https://www.postgresql.org/docs/14/static/
Process: 9042 ExecStartPre=/usr/pgsql-14/bin/postgresql-14-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 9048 (postmaster)
Tasks: 13 (limit: 100420)
Memory: 1.3G
From the output, we can see that postgresl-14 is enabled on this system.
We can also use the systemctl command with the is-enabled option. Replace servicename with the name of the service you want to check:
systemctl is-enabled servicename
This command will return one of the following:
- enabled: The service is enabled and set to start at boot.
- disabled: The service is disabled and won’t start at boot.
- static: The service is enabled but is not meant to start on its own.
For example:
$ systemctl is-enabled postgresql-14
enabled
If you are not exactly sure about the service name, you can use the following way to get this.
systemctl list-unit-files --state=enabled --type=service
When you run this command, you’ll see a list of services with their names and statuses. The “enabled” state means that these services are configured to start at boot time.
Example output looks like:
UNIT FILE STATE
auditd.service enabled
cronie.service enabled
dbus-org.freedesktop.network1.service enabled
...
This output displays the unit file names and their current states (in this case, “enabled”). It’s a convenient way to check which services are set to start automatically on your system.
You can combine the above command with grep command to filter and search for specific services more effectively like this.
systemctl list-unit-files --state=enabled --type=service |grep postgresl
It filters the results to display only the lines that contain the word “postgresl.”
You can adapt this approach to search for other specific services or keywords.