Skip to content

Linux | Managing Services

System services are one of the most important parts of a Linux system. They start applications in the background, open sockets, mount file systems, run timers and keep your system operational.

On modern Linux distributions this is usually handled by systemd. The main CLI tool to interact with systemd is systemctl.

1. What is systemd?

systemd is the init system and service manager used by most modern Linux distributions. It is the first userspace process started by the Linux kernel and usually runs as PID 1.

You can verify this with:

Bash
ps -p 1 -o comm=

Typical output:

Text Only
systemd

systemd is responsible for:

  • starting and stopping services
  • handling dependencies between services
  • managing sockets
  • mounting file systems
  • starting timers
  • logging service output through journald
  • supervising processes

In older Linux distributions this was often handled by SysVinit or other init systems.

2. What is systemctl?

systemctl is the command line tool used to control systemd. With systemctl you can start, stop, restart, enable or inspect services.

Basic syntax:

Bash
systemctl <command> <unit>

For example:

Bash
sudo systemctl restart ssh.service

The .service suffix is optional in many cases, so this also works:

Bash
sudo systemctl restart ssh

3. How to find services

3.1 List running services

Bash
systemctl list-units --type=service --state=running

post28_1

This lists all currently running services.

3.2 List all loaded services

Bash
systemctl list-units --type=service --all

This shows active, inactive and failed services.

3.3 List installed service files

Bash
systemctl list-unit-files --type=service

This command shows service files and their enablement state.

Typical states:

  • enabled → starts automatically during boot
  • disabled → does not start automatically
  • static → cannot be enabled directly, usually used as dependency
  • masked → blocked from being started

3.4 Search for a service

Bash
systemctl list-unit-files | grep ssh

or:

Bash
systemctl status ssh

You can also search directly in the unit directories:

Bash
find /etc/systemd/system /usr/lib/systemd/system -name '*ssh*'

Depending on the distribution, the vendor directory can also be:

Text Only
/lib/systemd/system

4. Where are services located?

systemd unit files are usually stored in different locations. The location is important because it tells you who owns the unit file and how it should be managed.

4.1 Vendor provided unit files

Text Only
/usr/lib/systemd/system/

post28_2

or on some distributions:

Text Only
/lib/systemd/system/

These files are installed by packages from your distribution. You should usually not edit them directly, because package updates can overwrite your changes.

4.2 Local administrator unit files

Text Only
/etc/systemd/system/

This directory is used for local services and overrides. If you create your own service, put it here.

4.3 Runtime unit files

Text Only
/run/systemd/system/

This directory is used for temporary runtime units. Content under /run is not persistent across reboots.

5. What are init files?

Init files are files used by an init system to start and manage services.

With older SysVinit, services were controlled by shell scripts located under:

Text Only
/etc/init.d/

Example:

Bash
ls -la /etc/init.d/

Those scripts usually supported commands like:

Bash
sudo /etc/init.d/ssh start
sudo /etc/init.d/ssh stop
sudo /etc/init.d/ssh restart

On modern systems with systemd, these old init scripts can still exist for compatibility. systemd can generate compatibility units for them, but the preferred way is to use native systemd unit files.

6. What are unit files?

A systemd unit file describes something that systemd can manage. A service is only one type of unit.

Common unit types:

  • .service → background service/process
  • .socket → socket activation endpoint
  • .timer → scheduled task, similar to cron
  • .mount → mount point
  • .target → group of units, similar to runlevels
  • .path → path based activation

List all unit types:

Bash
systemctl list-units --all

7. What is a .service file?

A .service file describes how a service should be started, stopped and supervised.

A very basic service file looks like this:

INI
[Unit]
Description=Example Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/example.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

Explanation

  • [Unit] → metadata and dependencies
  • Description → human readable name
  • After → start this unit after another unit
  • [Service] → service specific settings
  • Type=simple → process started by ExecStart is the main process
  • ExecStart → command to start the service
  • Restart=on-failure → restart if the process exits with an error
  • [Install] → information used by systemctl enable
  • WantedBy=multi-user.target → start during normal multi-user boot

8. What is a .socket file?

A .socket file describes a socket that systemd should listen on. This can be used for socket activation.

The idea:

  1. systemd opens the network or filesystem socket
  2. systemd waits for incoming connections
  3. the related service is started only when needed

A simple socket file:

INI
[Unit]
Description=Example Socket

[Socket]
ListenStream=8080

[Install]
WantedBy=sockets.target

The matching service would usually have the same base name:

Text Only
example.socket
example.service

When a connection comes in on port 8080, systemd can start example.service.

9. Difference between .service and .socket

.service

A .service file manages a process. It defines how an application starts and stops.

Example:

Bash
sudo systemctl start nginx.service

This starts the nginx process directly.

.socket

A .socket file manages a listening socket. It can start the related service on demand.

Example:

Bash
sudo systemctl start docker.socket

This does not necessarily start the full Docker daemon immediately. It tells systemd to listen on the Docker socket and activate the service when required.

Quick comparison

File type Purpose Example
.service manages a process ssh.service, nginx.service, docker.service
.socket manages a socket and can activate a service docker.socket, systemd-journald.socket

10. Managing services

Start a service

Bash
sudo systemctl start <service>

Example:

Bash
sudo systemctl start ssh

Stop a service

Bash
sudo systemctl stop <service>

Restart a service

Bash
sudo systemctl restart <service>

Reload a service

Some services can reload their configuration without a full restart.

Bash
sudo systemctl reload <service>

If you are not sure whether reload is supported, you can use:

Bash
sudo systemctl reload-or-restart <service>

Enable autostart

Bash
sudo systemctl enable <service>

This configures the service to start automatically during boot.

Disable autostart

Bash
sudo systemctl disable <service>

Enable and start at the same time

Bash
sudo systemctl enable --now <service>

Disable and stop at the same time

Bash
sudo systemctl disable --now <service>

11. Inspecting services

Status

Bash
systemctl status <service>

This is usually the first command when troubleshooting a service. It shows:

  • current state
  • main PID
  • last log lines
  • loaded unit file
  • whether the service is enabled or disabled

Show detailed properties

Bash
systemctl show <service>

For example only show some specific properties:

Bash
systemctl show ssh --property=LoadState,ActiveState,SubState,FragmentPath

Show the unit file

Bash
systemctl cat <service>

This is very useful because it also shows override files.

Check if a service is enabled

Bash
systemctl is-enabled <service>

Check if a service is active

Bash
systemctl is-active <service>

12. Logs with journalctl

systemd services usually write logs to journald. You can read them with journalctl.

Show logs for one service:

Bash
journalctl -u <service>

Follow logs live:

Bash
journalctl -u <service> -f

Show logs since last boot:

Bash
journalctl -u <service> -b

Show the last 100 lines:

Bash
journalctl -u <service> -n 100

Example:

Bash
journalctl -u ssh -n 100

13. Editing services the right way

Do not directly edit unit files under /usr/lib/systemd/system/ or /lib/systemd/system/. Those files belong to packages.

Use overrides instead:

Bash
sudo systemctl edit <service>

This creates an override file under:

Text Only
/etc/systemd/system/<service>.d/override.conf

Example override:

INI
[Service]
Restart=always
RestartSec=5

After changing unit files or overrides, reload systemd:

Bash
sudo systemctl daemon-reload

Then restart the service:

Bash
sudo systemctl restart <service>

14. Creating a custom service

Create a script:

Bash
sudo nano /usr/local/bin/hello-service.sh

Example content:

Bash
#!/usr/bin/env bash
while true; do
  echo "hello from systemd"
  sleep 30
done

Make it executable:

Bash
sudo chmod +x /usr/local/bin/hello-service.sh

Create the service file:

Bash
sudo nano /etc/systemd/system/hello.service

Content:

INI
[Unit]
Description=Hello Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/hello-service.sh
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Reload systemd:

Bash
sudo systemctl daemon-reload

Enable and start the service:

Bash
sudo systemctl enable --now hello.service

Check status:

Bash
systemctl status hello.service

Check logs:

Bash
journalctl -u hello.service -f

15. Masking services

Sometimes you want to prevent a service from being started at all. This is called masking.

Bash
sudo systemctl mask <service>

A masked service cannot be started manually or as a dependency. To undo this:

Bash
sudo systemctl unmask <service>

Use this carefully, because masking can break dependencies.

16. Useful troubleshooting commands

Bash
# Show failed units
systemctl --failed

# Show dependency tree
systemctl list-dependencies <service>

# Verify unit file syntax
systemd-analyze verify /etc/systemd/system/example.service

# Show boot time by unit
systemd-analyze blame

# Show critical boot chain
systemd-analyze critical-chain

17. Basic workflow

A common workflow when managing Linux services:

  1. Find the service
Bash
systemctl list-unit-files | grep <name>
  1. Check the current state
Bash
systemctl status <service>
  1. Inspect the unit file
Bash
systemctl cat <service>
  1. Check the logs
Bash
journalctl -u <service> -n 100
  1. Restart or reload
Bash
sudo systemctl restart <service>
  1. Enable autostart if needed
Bash
sudo systemctl enable <service>

systemd looks complicated at first, but most daily service management can be done with a small set of commands: status, start, stop, restart, enable, disable, cat and journalctl.

Cheers!