What are Daemons?

Linux Mar 20, 2024

A daemon is essentially just a background process performing various tasks, and are not in direct control of an interactive user.

Did you ever notice that your laptop or computer is already connected to your WiFi every time you turn it on? It's because of a daemon that's running in the background, you don't have to do it manually because it does it for you, with out the daemon you'd have to start network manager, and various other things and then open up your wifi settings to check for the available networks and then connect to it.

Daemons perform a certain task or event at a predefined interval or after a certain event, there are many daemons that run on a linux system, such as systemd, httpd, logind, sshd, as you might have noticed most daemons end with a letter d it is a general rule that in linux daemons have to end with a d.

If you are a windows user daemons are basically services that run in the background.

To check all the processes that are running in the background you can us, ps, top, or htop. You can also use pstree to see all the processes in a tree diagram, and all the processes that end with a d are daemons.

   ~ pstree
systemd─┬─ModemManager───3*[{ModemManager}]
        ├─NetworkManager───3*[{NetworkManager}]
        ├─avahi-daemon───avahi-daemon
        ├─bash─┬─bash
        │      └─bspc
        ├─bash───alacritty─┬─zsh───pstree
        │                  └─18*[{alacritty}]
        ├─bluetoothd
        ├─colord───3*[{colord}]
        ├─cupsd
        ├─dbus-broker-lau───dbus-broker
        ├─dunst───4*[{dunst}]
        ├─firefox─┬─Isolated Web Co───20*[{Isolated Web Co}]
        │         ├─5*[Isolated Web Co───19*[{Isolated Web Co}]]
        │         ├─2*[Isolated Web Co───18*[{Isolated Web Co}]]
        │         ├─Privileged Cont───18*[{Privileged Cont}]
        │         ├─RDD Process───3*[{RDD Process}]
        │         ├─Socket Process───4*[{Socket Process}]
        │         ├─Utility Process───3*[{Utility Process}]
        │         ├─2*[Web Content───15*[{Web Content}]]
        │         ├─Web Content───11*[{Web Content}]
        │         ├─WebExtensions───22*[{WebExtensions}]
        │         └─160*[{firefox}]
        ├─httpd───5*[httpd]
        ├─2*[ksuperkey───{ksuperkey}]
        ├─mariadbd───7*[{mariadbd}]
        ├─mpd───2*[{mpd}]
        ├─picom───11*[{picom}]
        ├─polkitd───3*[{polkitd}]
        ├─polybar───14*[{polybar}]
        ├─rtkit-daemon───2*[{rtkit-daemon}]
        ├─sddm─┬─Xorg───12*[{Xorg}]
        │      ├─sddm-helper───bspwm
        │      └─{sddm}
        ├─sshd
        ├─sxhkd
        ├─systemd─┬─(sd-pam)
        │         ├─appimagelaunche
        │         ├─at-spi-bus-laun─┬─dbus-broker-lau───dbus-broker
        │         │                 └─4*[{at-spi-bus-laun}]
        │         ├─at-spi2-registr───3*[{at-spi2-registr}]
        │         ├─dbus-broker-lau───dbus-broker
        │         ├─dconf-service───3*[{dconf-service}]
        │         ├─dirmngr
        │         ├─gnome-keyring-d───4*[{gnome-keyring-d}]
        │         ├─gpg-agent─┬─scdaemon───{scdaemon}
        │         │           └─{gpg-agent}
        │         ├─gvfs-afc-volume───4*[{gvfs-afc-volume}]
        │         ├─gvfs-goa-volume───3*[{gvfs-goa-volume}]
        │         ├─gvfs-gphoto2-vo───3*[{gvfs-gphoto2-vo}]
        │         ├─gvfs-mtp-volume───3*[{gvfs-mtp-volume}]
        │         ├─gvfs-udisks2-vo───4*[{gvfs-udisks2-vo}]
        │         ├─gvfsd─┬─gvfsd-dnssd───3*[{gvfsd-dnssd}]
        │         │       ├─gvfsd-network───4*[{gvfsd-network}]
        │         │       ├─gvfsd-trash───4*[{gvfsd-trash}]
        │         │       └─3*[{gvfsd}]
        │         ├─gvfsd-fuse───6*[{gvfsd-fuse}]
        │         ├─gvfsd-metadata───3*[{gvfsd-metadata}]
        │         ├─pipewire───3*[{pipewire}]
        │         ├─pipewire-pulse───2*[{pipewire-pulse}]
        │         ├─wireplumber───6*[{wireplumber}]
        │         ├─xdg-desktop-por───4*[{xdg-desktop-por}]
        │         ├─xdg-document-po─┬─fusermount3
        │         │                 └─6*[{xdg-document-po}]
        │         └─xdg-permission-───3*[{xdg-permission-}]
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-resolve
        ├─systemd-timesyn───{systemd-timesyn}
        ├─systemd-udevd
        ├─tailscaled───11*[{tailscaled}]
        ├─udisksd───5*[{udisksd}]
        ├─upowerd───3*[{upowerd}]
        ├─wpa_supplicant
        ├─xfce-polkit───9*[{xfce-polkit}]
        ├─xfce4-power-man───9*[{xfce4-power-man}]
        └─xsettingsd

When a user launches a program or an application it creates a process, let's say a user wants to edit a file using nano(a terminal text editor) a process is created when a user starts nano.

   ~ ps -aux | grep nano
deepak    315173  0.0  0.1   9312  6704 pts/0    S+   17:40   0:00 nano filetoedit
deepak    315684  0.0  0.1   6612  4240 pts/1    S+   17:41   0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox nano

As you can see nano and the file that is being edited(filetoedit) shows up when we search the list of all the current processes, the process is terminated when the user closes the program.

   ~ ps -aux | grep nano
deepak    325312  0.0  0.1   6612  4272 pts/1    S+   17:45   0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox nano

Here a process is being created when a user opens or closes an application or a program these type of processes are called interactive processes, a daemon on the other had is not interactive, it starts during the boot sequence and just runs in the background.

Daemons are spawned(created) by the init(short for initialization) process, the init process is the first process that starts up during the boot sequence, and then spawn a bunch of new processes by forking itself, making it the direct ancestor to the processes, another way of creating a daemons is when a process creates a child process and terminates itself, the child process becomes an orphan, when a child process is orphaned the init process adopts the orphaned child, making it an indirect ancestor or parent to the process, most linux distros come with systemd as the init system process. If you've noticed systemd ends with a d, as it is also a daemon, its task is to create and monitor other daemons and processes, systemd also acts as a service manager which let's the user to manage daemon using the systemctl command.

Daemon management using systemctl

We use systemctl with various options like, start, stop, enable, disable, and status to manage and control various daemons using systemctl.

To check if a service(say ssh) is running or not we use the status command.

   ~ sudo systemctl status sshd
[sudo] password for deepak:
● sshd.service - OpenSSH Daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; preset: disabled)
     Active: active (running) since Wed 2024-03-20 15:25:45 IST; 2h 41min ago
   Main PID: 609 (sshd)
      Tasks: 1 (limit: 3892)
     Memory: 244.0K (peak: 2.3M swap: 1.0M swap peak: 1.0M zswap: 97.7K)
        CPU: 25ms
     CGroup: /system.slice/sshd.service
             └─609 "sshd: /usr/bin/sshd -D [listener] 0 of 10-100 startups"

Mar 20 15:25:45 deepak-69ut systemd[1]: Started OpenSSH Daemon.
Mar 20 15:25:46 deepak-69ut sshd[609]: Server listening on 0.0.0.0 port 22.
Mar 20 15:25:46 deepak-69ut sshd[609]: Server listening on :: port 22.

To start a service we use the start option:

sudo systemctl start sshd

To stop a service we use the stop option:

sudo systemctl stop sshd

To enable a process, that is to stat a process on boot we use the enable option:

sudo systemctl enable sshd

And to disable a process, that is to stop a process from starting during the boot sequence we use the disable option:

sudo systemctl disable sshd

And that's it you now know what a daemon is and how to manage them using the master daemon systemd, and also how to control and manage other daemons using the master daemon with the help of systemctl command, and in a future blog ima tell you how to create your own daemon, in the mean time i'll tell you a DNS joke but be advised, it could take up to 24 hours for everyone to get it.

Tags

Deepak

Linux Enthusiast | Aspiring Cybersecurity Professional | Coding Enthusiast