Services & systemd
Learn how Linux starts applications in the background, manages services, handles system startup, and keeps servers running automatically.
Difficulty: Beginner
Estimated reading time: 55 min
Introduction
So far, you have learned about:
- filesystems
- permissions
- processes
Now it is time to connect everything together.
One of the most important concepts in Linux is:
Services.
Services are background applications that keep the operating system functional.
Examples:
| Service | Purpose |
|---|---|
| Nginx | Web server |
| Docker | Container engine |
| SSH | Remote access |
| MySQL | Database server |
| Cron | Scheduled jobs |
Without services:
- servers would not host websites
- Docker would not run containers
- databases would not work
- remote SSH access would fail
Modern Linux systems manage services using:
systemd
Understanding systemd is critical for:
- Linux administration
- DevOps
- servers
- Docker
- cloud infrastructure
- troubleshooting
What Is a Service?
A service is:
A long-running background process.
Unlike temporary commands:
ls
pwd
cat
services are designed to:
- run continuously
- start automatically
- wait for requests
- operate silently in the background
Example:
Nginx waits for HTTP requests
Docker waits for container commands
SSH waits for remote connections
Foreground vs Background
Normal commands often run in the foreground.
Example:
python app.py
The terminal becomes occupied.
But services usually run in the background:
Terminal remains free
Service continues running
This is essential for servers.
What Is a Daemon?
In Linux, background services are traditionally called:
Daemons.
Pronounced:
DEE-mons
not:
day-mons
Historically, daemon names often end with:
d
Examples:
| Daemon | Purpose |
|---|---|
sshd |
SSH server |
httpd |
Apache web server |
systemd |
System manager |
dockerd |
Docker daemon |
Daemons usually:
- start during boot
- run continuously
- wait for events or requests
What Happens During Boot?
When Linux starts:
- the kernel loads
- the kernel starts PID 1
- PID 1 initializes the system
- services are started
- users can log in
On modern Linux systems:
PID 1 = systemd
This makes systemd one of the most important processes on the machine.
What Is systemd?
systemd is:
The system and service manager used by most modern Linux distributions.
Its responsibilities include:
- starting services
- stopping services
- restarting services
- managing boot processes
- handling logs
- tracking dependencies
- supervising processes
Before systemd existed, Linux used older init systems like:
- SysVinit
- Upstart
systemd unified and modernized service management.
Why systemd Exists
Imagine booting a server manually.
You would need to:
- start networking
- start logging
- start SSH
- start Docker
- start databases
- start web servers
And if something crashed:
- restart it manually
- monitor failures
- manage dependencies yourself
systemd automates all of this.
Understanding systemctl
The primary command for interacting with systemd is:
systemctl
This is one of the most important Linux administration commands.
Checking Service Status
Example:
systemctl status nginx
Example output:
● nginx.service - A high performance web server
Loaded: loaded
Active: active (running)
This tells you:
- whether the service exists
- whether it is enabled
- whether it is currently running
Understanding Service States
Common states:
| State | Meaning |
|---|---|
| active (running) | Service is running |
| inactive | Service is stopped |
| failed | Service crashed or failed |
| activating | Service is starting |
Starting Services
Start a service manually:
sudo systemctl start nginx
This launches the service immediately.
Stopping Services
sudo systemctl stop nginx
This terminates the service.
Restarting Services
Very common command:
sudo systemctl restart nginx
This stops and starts the service again.
Useful after:
- configuration changes
- updates
- crashes
Reloading Services
Some services support reloads:
sudo systemctl reload nginx
Reloading usually:
- re-reads configuration
- avoids full restart
- minimizes downtime
This is extremely useful on production servers.
Enabling Services on Boot
Start automatically during system startup:
sudo systemctl enable nginx
Without enabling:
- service may work now
- but disappear after reboot
Disabling Services
sudo systemctl disable nginx
The service will no longer auto-start during boot.
Viewing All Services
List services:
systemctl list-units --type=service
This displays active services.
Viewing Failed Services
Useful for troubleshooting:
systemctl --failed
This quickly identifies broken services.
What Is a Unit?
systemd manages objects called:
Units.
Service units are only one type.
Examples:
| Unit Type | Purpose |
|---|---|
.service |
Services |
.mount |
Mounted filesystems |
.timer |
Scheduled tasks |
.target |
Boot targets |
Example:
nginx.service
docker.service
Understanding Service Files
systemd services are defined using unit files.
Usually located in:
/etc/systemd/system
or:
/lib/systemd/system
Example:
/etc/systemd/system/myapp.service
Example Service File
[Unit]
Description=My Node.js App
[Service]
ExecStart=/usr/bin/node /app/server.js
Restart=always
[Install]
WantedBy=multi-user.target
This tells systemd:
- what to execute
- how to restart it
- when to start it
Understanding Restart Policies
Example:
Restart=always
This means:
If the application crashes, restart it automatically.
This is one reason Linux servers are so reliable.
systemd continuously supervises services.
Viewing Logs with journalctl
systemd also manages logs through:
journalctl
Example:
journalctl -u nginx
This displays logs for the Nginx service.
Real-Time Logs
Live log monitoring:
journalctl -u nginx -f
Similar to:
tail -f
Very useful for debugging applications.
Viewing Boot Logs
Display boot logs:
journalctl -b
Useful when diagnosing startup problems.
Why Logs Matter
When services fail:
Logs explain why.
Common problems:
- missing permissions
- invalid configs
- missing ports
- dependency failures
- crashed applications
Reading logs is one of the most important Linux skills.
Example: Broken Nginx Configuration
Imagine you edit:
/etc/nginx/nginx.conf
Then restart:
sudo systemctl restart nginx
But the service fails.
Check status:
systemctl status nginx
Then inspect logs:
journalctl -u nginx
You may discover:
syntax error in configuration
This is real Linux troubleshooting.
Understanding Targets
Older Linux systems used:
runlevels
Modern systemd systems use:
targets
Examples:
| Target | Purpose |
|---|---|
multi-user.target |
Normal server mode |
graphical.target |
Desktop environment |
reboot.target |
System reboot |
Targets define system states.
Checking Boot Time
Useful command:
systemd-analyze
Example:
Startup finished in 3.2s
This helps analyze boot performance.
Why systemd Is Controversial
Some Linux users dislike systemd because:
- it is large
- it handles many responsibilities
- it replaced older Unix-style tools
Others love it because:
- it standardizes service management
- it improves reliability
- it simplifies administration
Regardless of opinions:
systemd dominates modern Linux infrastructure.
If you work with Linux professionally, you will almost certainly use it.
Real-World Example: Hosting a Website
Imagine running a production server.
Services involved:
| Service | Purpose |
|---|---|
sshd |
Remote access |
docker |
Containers |
nginx |
Reverse proxy |
mysql |
Database |
Typical workflow:
Check Running Services
systemctl status docker
Restart Web Server
sudo systemctl restart nginx
View Application Logs
journalctl -u docker -f
Enable Services During Boot
sudo systemctl enable docker
This is everyday Linux administration.
Why systemd Matters for DevOps
Modern infrastructure depends heavily on service management.
Examples:
| Technology | Managed by systemd? |
|---|---|
| Docker | Yes |
| Kubernetes components | Often |
| Databases | Yes |
| Monitoring agents | Yes |
| CI/CD runners | Yes |
Understanding systemd makes Linux servers much easier to manage.
Linux as an Automated System
One of the biggest mindset shifts:
Linux is designed to:
- boot automatically
- recover automatically
- manage services automatically
- supervise processes continuously
systemd acts as the orchestration layer that keeps everything running.
The Bigger Picture
Once you understand services and systemd, Linux starts feeling much more alive.
You begin understanding:
- how servers boot
- how applications stay online
- how infrastructure recovers from failures
- how modern Linux systems manage complexity
Services are the foundation of nearly every Linux server.
What Comes Next
In the next chapter, we will explore:
- networking basics
- IP addresses
- ports
- localhost
- DNS
- network troubleshooting
- tools like
curl,ping, andss
This is where Linux starts connecting to the outside world.