If you’re managing Ubuntu servers or desktops, understanding systemd
is absolutely essential. systemd
is the init system and service manager that’s become the standard for most Linux distributions, including Ubuntu.
It’s responsible for starting, stopping, and managing all the services and processes that make your system run.
This article will give you a solid foundation in working with systemd
units. We’ll focus on the core concepts of targets and services, and show you how to manage them effectively.
Think of systemd
targets as different “modes” or “states” your Ubuntu system can be in. Each target represents a specific level of functionality. When your system boots, it starts up into a particular target, and that target determines which services and processes are launched.
Why are targets important?
Read: How to Troubleshoot and Optimize Ubuntu Startup: Manage Systemd Services for Faster Boot Time
A systemd
service is essentially a background process that provides some specific functionality. For example, sshd
is the service that handles SSH connections. apache2
is the service for the Apache web server. Targets define which services should be started.
Key Concept: Services are managed by systemd
and are defined by unit files. We’ll talk more about unit files later.
Ubuntu comes with a set of predefined targets. Here are some of the most important ones:
poweroff.target
: Shuts down the system. (You wouldn’t want this as your default!)rescue.target
: Boots into a single-user mode, with minimal services running. This is your “emergency mode” for troubleshooting. Only the root user can log in, and networking is typically not started. Use systemctl isolate rescue.target
multi-user.target
: This is the standard target for servers. It provides a multi-user environment with networking, but without a graphical desktop. You’ll get a text-mode login prompt. Use systemctl isolate multi-user.target
graphical.target
: This is the standard target for desktop systems. It includes everything in multi-user.target
, plus the graphical desktop environment (GNOME, in the case of Ubuntu). Use systemctl isolate graphical.target
reboot.target
: Reboots the system.Behind the Scenes: These main targets often depend on other, more specialized targets. For example, multi-user.target
depends on basic.target
, which in turn depends on other targets like sockets.target
. This creates a hierarchy of dependencies, ensuring that everything starts up in the correct order.
Read: How to Manage Ubuntu Boot Services: List, Start, and Stop Systemd Services at Startup
How do you know which target your system is using, and how do you change it?
systemctl get-default
This command will output the name of the default target (e.g., graphical.target
or multi-user.target
).
systemctl set-default multi-user.target
This command changes the default target to multi-user.target
. The next time you reboot, the system will boot into this target. The command creates a symbolic link, usually in /etc/systemd/system/
, that points to the target file.
Important Note: Changing the default target only affects future boots. It doesn’t change the current state of the system.
We’ve mentioned “targets” and “services.” These are both examples of systemd
units. A unit is a configuration file that describes a resource managed by systemd
. Unit files are usually located in:
/usr/lib/systemd/system/
: Unit files provided by installed packages./etc/systemd/system/
: Unit files created or modified by the system administrator. These override the files in /usr/lib/systemd/system/
.There are several types of units, each identified by a file extension:
Unit Type | File Extension | Description |
---|---|---|
Service | .service | Defines a service (a background process). |
Target | .target | Groups together other units. Think of it as a “state” the system can be in. |
Socket | .socket | Defines an inter-process communication (IPC) socket. |
Mount | .mount | Defines a file system mount point. |
Automount | .automount | Defines a file system mount point that is mounted on demand. |
Swap | .swap | Defines a swap device or file. |
Timer | .timer | Defines a timer that triggers actions at specific times (similar to cron jobs). |
Path | .path | Defines a file or directory to be monitored for changes. |
Slice | .slice | Used for resource management (grouping units into hierarchical cgroups). |
Scope | .scope | Manages a set of system processes that are not started by systemd itself. |
Device | .device | Represents a device as seen by the Linux kernel. |
Snapshot | .snapshot | Represents a saved state of systemd. |
Key Point: Targets are special because they group together other units. A target doesn’t do anything itself; it just defines which other units should be active when that target is active.
Read: How to analyze Linux systemd logs using journalctl advanced filtering options
You can change the current target without rebooting using the isolate
command:
sudo systemctl isolate multi-user.target
This command immediately switches the system to the multi-user.target
. If you were in the graphical desktop, it would shut down, and you’d be presented with a text-mode login.
Important Note: isolate
is a powerful command. It will stop any services that are not part of the target you’re switching to. Make sure you understand the consequences before using it!
systemctl enable sshd.service
This command enables the sshd
service. It creates symbolic links in the appropriate .wants
directories (usually in /etc/systemd/system/
) to link the service to the targets that depend on it.
systemctl disable sshd.service
This command removes the symbolic links created by enable
.
systemctl start sshd.service
systemctl stop sshd.service
systemctl status sshd.service
This command shows you whether the unit is active, enabled, and provides other useful information, including recent log messages.
systemctl mask sshd.service
This command creates a symbolic link from the unit file to /dev/null
, effectively making the unit invisible to systemd
.
mask
. systemctl unmask sshd.service
systemctl reload sshd.service
While command-line tools are essential for managing systemd
, Cockpit provides a convenient web-based interface for many tasks. If you have Cockpit installed, you can access the “Services” section to view and manage systemd units. You can see the status of units, start and stop them, enable and disable them, and even view their logs. This is particularly useful for remote administration.
systemd
is a powerful and complex system, but understanding the basics of units, targets, and services is crucial for managing your Ubuntu system. Here’s a recap of the key commands:
systemctl get-default
: Show the default boot target.systemctl set-default
: Change the default boot target.systemctl isolate
: Switch to a different target immediately.systemctl list-dependencies
: Show the units a target depends on.systemctl status
: Show the status of a unit.systemctl start
: Start a unit.systemctl stop
: Stop a unit.systemctl enable
: Enable a unit to start at boot.systemctl disable
: Disable a unit from starting at boot.systemctl mask
: Prevent a unit from being started at all.systemctl unmask
: Reverse the effect of mask
.systemctl reload
: Reload service.systemctl set-default
, not systemctl isolate
. set-default
changes the default target for future boots. isolate
changes the current target.systemctl list-unit-files --type=service --state=enabled
. This will list all enabled service units.systemctl unmask
.* `/usr/lib/systemd/system/`: Unit files provided by installed packages.
* `/etc/systemd/system/`: Unit files created or modified by the system administrator. These override the files in `/usr/lib/systemd/system/`.
systemctl list-dependencies
.rescue.target
. systemctl list-unit-files --type=target
: systemctl list-dependencies target_name>
The post How to use systemd Units on Ubuntu 20.04: A Sysadmin’s Deep Dive appeared first on net2.
If you’ve ever tried compiling a Linux kernel from source—whether to add custom system calls,…
Security is paramount, and one of the first lines of defense for any system, whether…
If you’ve ever encountered the frustrating Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is…
If you’ve recently upgraded to Ubuntu 22.10 from version 22.04, you might have encountered an…
When developing software, particularly in languages like C and C++, crashes are inevitable. The dreaded…
Have you ever been working in your Ubuntu terminal when suddenly that jarring error sound…