1. Overview

In this tutorial, we’ll explore the concept of daemons in Linux, to get a comprehensive understanding of their role and importance.

We’ll begin by defining and discussing the key characteristics that distinguish daemons from other processes. Then, we’ll look at some common examples of daemons that are widely used in various Linux distributions.

2. Definition and Key Characteristics

A daemon in Linux is a background process that performs certain operations or provides services without direct user intervention.

Daemons are essential for the functioning of the operating system and for providing various services such as web servers, database servers, printer management, networking, and more. Most require root privileges.

Linux systems typically start daemons at startup and stop them at shutdown. This process ensures the availability of necessary services as soon as the system is up and running, and properly shuts them down when the system turns off.

Most daemons run in user space, where application processes run, providing a safer environment by isolating these processes from the critical operations performed in kernel space. However, some daemons run in kernel space, where they handle low-level system tasks and interrupts. This distinction highlights the versatile roles that daemons can play in different parts of the operating system.

Let’s recap the main characteristics of demons:

  • Run in background → Daemons run in background, usually with root privileges in user space, often starting at system boot
  • Independence from console → Most daemons operate independently of a user session or terminal, so they keep running even if the user logs off
  • System services → They provide several services to the system and users
  • Naming convention → Often daemon names end with a d, such as httpd, sshd and crond, to indicate their nature as daemons

It’s worth noting that other operating systems have equivalents to Linux daemons, although they may have different names. In Windows, they’re called services, while in macOS we refer to them as daemons and agents. In Unix-like systems, including BSD variants, the term daemon is commonly used, similar to Linux.

3. Common Examples of Daemons

Daemons in Linux can be divided into several categories, each with specific permission requirements.

System daemons are essential to the operation of the operating system and manage critical services such as sshd for SSH connections, httpd or nginx for web servers, and crond for scheduled tasks. These daemons typically require root privileges to access protected system resources and privileged network ports. However, some of them can start with root privileges and then reduce their operating privileges to run as less privileged users for security reasons.

Service daemons manage functions that are important to users or other applications, but aren’t always critical to the system. Examples of these daemons include mysqld for MySQL database management, docker for container management, and postfix for mail management. These daemons often start with root privileges to configure the necessary environment, but then reduce their privileges to operate more safely.

User daemons provide specific services for user sessions or personal features. These include gvfsd for virtual file system mounting, pulseaudio for audio management, and dbus-daemon for interprocess messaging. These daemons don’t require root privileges and run with the privileges of the user who started them. They operate within user sessions to ensure that their operations don’t interfere with those of other users on the system.

Finally, kernel daemons operate at the kernel level and handle specific low-level system tasks. Examples of these daemons include ksoftirqd for managing software interrupts, kworker for handling kernel background tasks, and kswapd for virtual memory management. These daemons run in kernel space, which means they have full access to system resources and can perform critical operations without being subject to regular user privileges.

4. Managing Daemons

systemctl is the command-line utility for managing services on systems that use the systemd init system. systemd has become the standard for most modern Linux distributions due to its efficiency and powerful features. With systemctl, we can start, stop, restart, enable, disable, and check the status of daemons.

Let’s list all the active daemons:

$ systemctl list-units --type=service --state=running
  UNIT                           LOAD   ACTIVE SUB     DESCRIPTION                                                     
  accounts-daemon.service        loaded active running Accounts Service
  acpid.service                  loaded active running ACPI event daemon
  apache2.service                loaded active running The Apache HTTP Server
  atd.service                    loaded active running Deferred execution scheduler
  avahi-daemon.service           loaded active running Avahi mDNS/DNS-SD Stack
[...]

We can check the status of one of these daemons:

$ systemctl status apache2.service
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2024-07-03 11:40:04 CEST; 9h ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 1713 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 1882 (apache2)
      Tasks: 6 (limit: 18830)
     Memory: 18.6M
        CPU: 1.929s
     CGroup: /system.slice/apache2.service
             ├─1882 /usr/sbin/apache2 -k start
[...]

To restart a daemon, we need sudo:

$ sudo systemctl restart apache2.service

On the other hand, service is a legacy command to manage services on older Linux systems that use the SysVinit system. The commands are similar. Let’s look at an example:

$ service apache2 status
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2024-07-03 21:02:44 CEST; 59s ago
[...]

While service is obsolete on most modern systems, knowing it remains useful for managing legacy systems. Additionally, on systems using systemd, we can still use the service command to manage daemons since service often works with systemd. In these cases, many service commands internally redirect to the equivalent systemctl commands.

5. Conclusion

In this article, we explored the concept of daemons in Linux, defining their main characteristics and importance.

We examined different types of daemons, including system, service, user, and kernel daemons, and discussed their specific roles and privileges. We also discussed how to manage daemons using systemctl, the modern command-line service management utility, and briefly touched on the legacy service command.

Understanding and effectively managing daemons is critical to maintaining a robust and efficient Linux system, ensuring that essential services are always available and properly managed.