Categories: TutorialsUbuntu

How to use systemd Units on Ubuntu 20.04: A Sysadmin’s Deep Dive

 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.

1. Understanding Ubuntu systemd Targets: Defining System States

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?

  • Control: You can choose which target your system boots into, controlling the services that are running.
  • Resource Management: Different targets use different amounts of resources. A server might boot into a minimal target to conserve resources, while a desktop system would boot into a graphical target.
  • Troubleshooting: You can boot into a rescue target to fix problems without starting up all the usual services.

Read: How to Troubleshoot and Optimize Ubuntu Startup: Manage Systemd Services for Faster Boot Time 

2. Understanding Ubuntu systemd Services: The Building Blocks

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.

3. Ubuntu systemd Target Descriptions: A Breakdown of Common Targets

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

4. Identifying and Configuring the Default Target: Setting Your System’s Startup Mode

How do you know which target your system is using, and how do you change it?

  • Check the current default target:
    systemctl get-default
    

    This command will output the name of the default target (e.g., graphical.target or multi-user.target).

  • Change the default 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.

5. Understanding systemd Units and Unit Types: The Details

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

6. Dynamically Changing the Current Target: Switching Modes on the Fly

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!

7. Enabling, Disabling, and Masking systemd Units: Controlling Startup Behavior

  • Enabling a unit: Means that the unit will be started automatically when the system boots into a target that depends on 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.

  • Disabling a unit: Means that the unit will not be started automatically.
    systemctl disable sshd.service
    

    This command removes the symbolic links created by enable.

  • Starting a unit: Starts the unit immediately.
    systemctl start sshd.service
    
  • Stopping a unit: Stops the unit immediately.
    systemctl stop sshd.service
    
  • Checking the status of a unit:
    systemctl status sshd.service
    

    This command shows you whether the unit is active, enabled, and provides other useful information, including recent log messages.

  • Masking a unit: This is a stronger form of disabling. It prevents the unit from being started at all, even if another unit depends on it. This is useful for preventing a service from being accidentally started.
    systemctl mask sshd.service
    

    This command creates a symbolic link from the unit file to /dev/null, effectively making the unit invisible to systemd.

  • Unmasking a unit: Reverses the effect of mask.
    systemctl unmask sshd.service
    
  • Reloading a Unit: apply changes made without restarting.
      systemctl reload sshd.service
    

8. Working with systemd Units in Cockpit: A Graphical Approach

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.

9. Summary: Your systemd Toolkit

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.

FAQ

  1. Q: What’s the difference between a target and a service?A: A service is a background process that provides specific functionality (e.g., SSH, web server). A target is a collection of units (including services) that define a particular system state (e.g., graphical desktop, multi-user text mode).
  2. Q: I changed the default target, but my system still boots into the graphical desktop.A: Make sure you used systemctl set-default, not systemctl isolate. set-default changes the default target for future boots. isolate changes the current target.
  3. Q: How can I see which services are enabled to start at boot?A: Use systemctl list-unit-files --type=service --state=enabled. This will list all enabled service units.
  4. Q: I masked a service, and now I can’t start it. What do I do?A: You need to unmask the service using systemctl unmask .
  5. Q: Where are the systemd unit files located?A: There are two main locations:
    *   `/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/`.
    
  6. Q: How can I see the dependencies of a specific service?A: Use systemctl list-dependencies .
  7. Q: My System is not booting correctly after making changes, what to do?A: Boot into rescue.target.
  8. Q:How can I check all the available targets on my machine?A: use the following:
      systemctl list-unit-files --type=target
    
  9. Q: How can I check what are the units that my target depends on?A: use the following , by replacing the :
     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.

Ubuntu Server Admin

Recent Posts

How to Fix “No rule to make target ‘debian/canonical-certs.pem” When Compiling Linux Kernel 5.11.11 and Later

If you’ve ever tried compiling a Linux kernel from source—whether to add custom system calls,…

44 minutes ago

GUFW and UFW Ubuntu 20.10 Firewall Configuration Guide

Security is paramount, and one of the first lines of defense for any system, whether…

44 minutes ago

How to fix “Cannot Connect to the Docker Daemon” Error

If you’ve ever encountered the frustrating Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is…

45 minutes ago

Ubuntu 22.10 Boot Warnings: How to fix the “blacklist: Problem blacklisting hash (-13)” Boot Message in Ubuntu 22.10

If you’ve recently upgraded to Ubuntu 22.10 from version 22.04, you might have encountered an…

1 day ago

The Ultimate Guide to Viewing and Analyzing Core Dump Files on Ubuntu

When developing software, particularly in languages like C and C++, crashes are inevitable. The dreaded…

1 day ago

How to Turn off error sound on Ubuntu 18.04

Have you ever been working in your Ubuntu terminal when suddenly that jarring error sound…

1 day ago