Linux Services (Part 1)
Ahh…Linux services, cannot talk about them without mentioning systemd! Let’s go over some theoretical things of Linux services in the first part, and we shall have a more practical example on the second part!
Linux services are vital components of the system, responsible for ensuring various functionalities run smoothly. They are managed through init systems like systemd
and can be tailored to meet the specific needs of the system or user.
Let’s have a look at some importances of linux services!
Linux Services
1. Background Processes
- Services typically run in the background, meaning they do not interact with the user directly through the terminal of GUI.
- They are often referred to as daemons and typically have names ending with a
d
, e.g.sshd
,systemd
,httpd
.
2. Managed by Init System
- Services are started, stopped and managed by an init system The most commmon init systems are:
- systemd: The most widely used on modern Linux distributions.
- SysVinit: Used in older Linux systems
- Upstart: An older init system, primarily used by Ubuntu in the past.
- The
systemctl
command is used to manage services insystemd
3. Configuration
- Services are configured through files stored in locations such as
/etc/systemd/system/
: highest priority./run/systemd/system/
/lib/systemd/system/
: lowest priority.
4. Starts at Boot
- Some services are enabled to start automatically when the system boots. For example, a web server like `nginx` can be configured to start every time the system is restarted.
5. Common Linux Services
- System Services:
- networkd: manages network configurations and connectivity.
- systemd-journald: handles logging and journal services, capturing system logs and events.
- cron: schedules and executes recurring tasks or scripts at specified times.
- dbus: facilitates communication between system processes and application through a messages bus.
- Network Services:
- sshd: SSH server.
- nginx: web server.
- dnsmasq: DNS.
- Application Services:
- mysql: database server.
- mariadb: database server.
- docker: virtual environment.
- Hardware Services:
- cups: for printing.
- bluetooth: for bluetooth devices.
Now that we know what services do in Linux, let’s address the elephant in the room. What is systemd
? And why it is relatd to Linux services.
Systemd
systemd
is an init system and service manager for Linux operating systems, designed to bootstrap the system and manage services and processes during runtime. It is an init system that is the first process to runs, hence, with PID1. It manages all the units on the system, and service is just one of the units.
Below are some common types of units:
Unit Type | File Suffix | Description |
---|---|---|
Service | .service | Manages system services (e.g., daemons like nginx.service or ssh.service ). |
Socket | .socket | Manages IPC or network sockets, often used to start services on demand (e.g., cups.socket ). |
Target | .target | Groups other units to represent a system state or boot stage (e.g., multi-user.target ). |
Device | .device | Tracks and manages kernel devices (e.g., USB devices). |
Mount | .mount | Controls filesystem mount points (e.g., home.mount for /home ). |
Automount | .automount | Sets up automount points that trigger mounting on access (e.g., home.automount ). |
Swap | .swap | Manages swap space activation and deactivation (e.g., swapfile.swap ). |
Path | .path | Monitors filesystem paths and triggers services based on changes (e.g., myapp.path ). |
Timer | .timer | Provides timer-based activation for services, replacing traditional cron jobs (e.g., backup.timer ). |
Slice | .slice | Organizes and limits resources for a group of processes (e.g., user.slice ). |
Scope | .scope | Tracks and manages externally created processes (e.g., started by non-systemd tools). |
Snapshot | .snapshot | Represents a saved state of the systemd manager for rollback purposes. |
Examples:
- Service:
nginx.service
runs - Timer:
backup.timer
triggers a backup service at specific intervals. - Target:
graphical.target
represents the system state for a graphical desktop environment.
systemd
is used by Ubuntu, and a few Linux distros, Fedora, CentOS, RedHat, and Arch, just to name a few. It starts a target which can start various other targets and services. These are what we called units. And for Mac user, it would be launchd
that is managing your daemon and services. You can read more about it here.
To see which target is the default for this system:
1
2
systemctl get-default
# graphical.target
An Ubuntu system, generally runs at the graphical target level. Which itself requires the multi-user.targets, which requires various targets, such as basic.target, sockets.target, network.target, etc.
To list the dependencies of a target, you can run:
1
systemctl list-dependencies graphical.target
To list the currectly active targets, we can run:
1
systemctl list-units --type target
Let’s take a look at a simple example of service file, rsyslog
. It resides in the /etc/systemd/system/syslog.service
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[Unit]
Description=System Logging Service
Requires=syslog.socket
Documentation=man:rsyslogd(8)
Documentation=man:rsyslog.conf(5)
Documentation=https://www.rsyslog.com/doc/
[Service]
Type=notify
ExecStartPre=/usr/lib/rsyslog/reload-apparmor-profile
ExecStart=/usr/sbin/rsyslogd -n -iNONE
StandardOutput=null
StandardError=journal
Restart=on-failure
# Increase the default a bit in order to allow many simultaneous
# files to be monitored, we might need a lot of fds.
LimitNOFILE=16384
CapabilityBoundingSet=CAP_BLOCK_SUSPEND CAP_CHOWN CAP_DAC_OVERRIDE CAP_LEASE CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_SYS_ADMIN CAP_SYS_RESOURCE CAP_SYSLOG CAP_MAC_ADMIN CAP_SETGID CAP_SETUID
SystemCallFilter=@system-service
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
NoNewPrivileges=yes
ProtectHome=no
ProtectClock=yes
ProtectHostname=yes
[Install]
WantedBy=multi-user.target
Alias=syslog.service
We will go over more in detail about a service file. For now, let’s look at the bottom, you can see that it is wanted by the multi-user.target
. This is automatically done, when you installed a software. Therefore, if it needs a service file, the installer will ensure creating it and making systemd
aware.
Let’s learn more about service files and writing your own in the next post.
Reference
- https://www.youtube.com/watch?v=Kzpm-rGAXos