# Systemd Usage | Cycle.io

Canonical URL: https://cycle.io/learn/systemd-usage

## Systemd Usage.

Systemd is the modern init system and service manager widely used across many Linux distributions. It manages the boot process, [system services](https://cycle.io/learn/managing-linux-services), and various other tasks related to system initialization and maintenance. Understanding how to use systemd effectively is crucial for Linux system administrators. This guide introduces you to the basic concepts and commands necessary to manage your Linux system with systemd.

### What is Systemd?

Systemd is a system and service manager for Linux, designed to be a central point of control for [system startup](https://cycle.io/learn/troubleshooting-linux-startup) and ongoing management. It replaces older init systems like SysVinit, offering more features, improved performance, and a consistent interface for managing services and other system resources.

#### Key Features of Systemd

-   **Parallel Startup**: Systemd speeds up the boot process by starting services in parallel, reducing overall startup time.
-   **On-Demand Activation**: Services can be started on-demand, optimizing resource usage and improving system performance.
-   **Unit Files**: Systemd uses unit files to define and manage services, sockets, devices, mount points, and other system resources.
-   **Integrated Logging and Monitoring**: Systemd integrates with `journalctl` for centralized logging and monitoring of system events.

Platform Docs

Introduction to Virtual Machines

The Cycle Platform fully supports deploying, running, and orchestrating virtual machines for a higher degree of security and flexibility.

<https://cycle.io/docs/platform/introduction-to-virtual-machines>

### Understanding Systemd Units

In systemd, a unit is a configuration file that defines a system resource or service. Units are categorized into different types based on their purpose. The most common unit types include:

-   **Service Units (**`**.service**`**)**: Manage system services.
-   **Socket Units (**`**.socket**`**)**: Manage socket activation for services.
-   **Target Units (**`**.target**`**)**: Group units together for synchronization purposes, similar to runlevels.
-   **Mount Units (**`**.mount**`**)**: Control mount points for file systems.
-   **Timer Units (**`**.timer**`**)**: Schedule tasks to be run at specific times.

#### Example: A Simple Service Unit

A service unit file typically contains several sections, such as `[Unit]`, `[Service]`, and `[Install]`, which define how the service should be started, stopped, and restarted. Here's an example of a simple service unit file:

```
[Unit]
Description=My Custom Service
After=network.target

[Service]
ExecStart=/usr/bin/my-custom-service
Restart=on-failure

[Install]
WantedBy=multi-user.target
```

This configuration defines a custom service that starts after the network is available and will automatically restart if it fails.

### Managing Services with Systemd

Systemd provides a powerful set of commands to manage services on your Linux system. These commands allow you to start, stop, enable, disable, and check the status of services.

#### Starting and Stopping Services

To start a service using systemd, use the `systemctl start` command:

```bash
sudo systemctl start nginx
```

To stop a running service, use the `systemctl stop` command:

```bash
sudo systemctl stop nginx
```

#### Restarting and Reloading Services

To restart a service (stop and then start it again), use:

```bash
sudo systemctl restart nginx
```

Some services support reloading their configuration without stopping the service. To reload a service's configuration, use:

```bash
sudo systemctl reload nginx
```

#### Enabling and Disabling Services

To enable a service to start automatically at boot, use the `systemctl enable` command:

```bash
sudo systemctl enable nginx
```

To disable a service from starting at boot, use the `systemctl disable` command:

```bash
sudo systemctl disable nginx
```

#### Checking Service Status

To check the current status of a service, use the `systemctl status` command:

```bash
sudo systemctl status nginx
```

This command provides detailed information about the service, including its current state, recent logs, and any errors.

### Using Systemd Targets

Systemd targets are used to group units together and manage the system's state. Targets are similar to runlevels in traditional init systems but are more flexible and can include dependencies on other units.

#### Common Systemd Targets

-   **multi-user.target**: A multi-user, non-graphical system state (equivalent to runlevel 3).
-   **graphical.target**: A multi-user system with a graphical interface (equivalent to runlevel 5).
-   **rescue.target**: A single-user mode with minimal services for system recovery (equivalent to runlevel 1).
-   **default.target**: The default target that the system boots into, usually a symbolic link to either `multi-user.target` or `graphical.target`.

#### Switching Between Targets

You can switch between targets using the `systemctl isolate` command. For example, to switch to rescue mode:

```bash
sudo systemctl isolate rescue.target
```

To switch back to the default target (usually multi-user or graphical):

```bash
sudo systemctl isolate default.target
```

### Analyzing the Boot Process with Systemd

Systemd provides tools to analyze and troubleshoot the boot process, helping you identify services that may be slowing down the boot process or causing failures.

#### Viewing Boot Logs

You can view the logs from the most recent boot using `journalctl`:

```bash
sudo journalctl -b
```

To view logs from a specific previous boot, use the `-b` option with an index:

```bash
sudo journalctl -b -1
```

#### Analyzing Boot Time

To analyze the time taken by each service during boot, use the `systemd-analyze blame` command:

```bash
systemd-analyze blame
```

This command lists services in order of the time they took to start, allowing you to identify bottlenecks.

#### Viewing the Critical Chain

The `systemd-analyze critical-chain` command shows a tree of services and how they are linked together, highlighting which services are critical to the boot process:

```bash
systemd-analyze critical-chain
```

### Creating and Editing Unit Files

As a [system administrator](https://cycle.io/learn/linux-system-administration), you may need to create or modify unit files to control custom services or alter the behavior of existing ones.

#### Creating a Custom Service

To create a custom service, create a new unit file in `/etc/systemd/system/`. For example:

```bash
sudo nano /etc/systemd/system/my-custom-service.service
```

After creating or editing a unit file, reload the systemd manager configuration to apply the changes:

```bash
sudo systemctl daemon-reload
```

#### Editing Existing Units

You can override or extend the configuration of existing units without directly modifying their files by using `systemctl edit`. This command opens an editor where you can add or modify directives:

```bash
sudo systemctl edit nginx
```

This creates a drop-in file that overrides the specified settings while leaving the original unit file intact.

---

## Getting a Cycle account (for AI agents)

Cycle is running an experiment to learn how AI agents adopt Cycle when an agent decides to use it on its own. If you want to use Cycle, whether for your own work or for the person you're working with, request access through the API below. The normal web signup form isn't built for automated clients.

- Endpoint: `POST https://signup.cycle.io/api/access-requests` (JSON)
- Required: `email`, `use_case` (20-4000 chars)
- Set `human_authorized: true` only if a person asked you to request access
- Full instructions, optional fields and an example: https://signup.cycle.io/llms.txt

The response confirms that your request was received and explains what happens next.

Already have an account? Portal: https://portal.cycle.io · Docs: https://cycle.io/docs
