Skip to content

Commit

Permalink
docs: 📝 systemd about user unit lack support
Browse files Browse the repository at this point in the history
  • Loading branch information
heldrida committed Sep 7, 2023
1 parent e55fc38 commit 1522314
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/node/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -642,13 +642,13 @@ sudo systemctl enable lightning.service
```
:::caution
You shouldn't prefix the systemctl command with **sudo** when start/stop/status the service.
You shouldn't have prefix the systemctl command with **sudo** when start/stop/status the service. Due to some VPS providers modifying the operating system, we had to present the examples prefixed with sudo for the wider audience. If you'd like to learn more about controlling Systemd services as a `user` check the reference [Systemd user-service](/references/Systemd/user-service).
:::
Start the service by:
```sh
sudo systemctl start lightning
sudo systemctl start lightning.service
```
:::tip
Expand Down
142 changes: 142 additions & 0 deletions references/Systemd/user-service.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
---
title: User service
slug: user service
hide_title: true
tags:
- references
- help
- user service
- unit
- systemctl
- systemd
---

<!--
The following import is intentional (see partial <CheckoutCommitWarning />)
-->
import Author from '@site/src/components/Author';

A user should have the ability to run a Systemd user service unit without having to use `sudo` to control it.

## Check `--user` support

Use the `--user` flag when getting the list of unit files.

```sh
systemctl --user list-unit-files
```

It should return a list of unit files.

## Put the service unit in the user service

Create the Systemd user units directory:

```sh
sudo mkdir -p /etc/systemd/system/user
```

Move the `lightning.service` to the system user unit service directory:

```sh
mv /etc/systemd/system/lightning.service /etc/systemd/system/user/lightning.service
```

Check the **Load path when running in user mode (--user)** in [Systemd unit](https://www.freedesktop.org/software/systemd/man/systemd.unit.html) for other alternative user paths, or to understand how it works to customize your server accordingly.

## Reload daemon

```sh
sudo systemctl daemon-reload
```

## Systemd service control as `--user`

Reload the Systemctl daemon by executing the command:

```sh
systemctl --user daemon-reload
```

Enable the service for starting up on system boot:

```sh
systemctl --user enable lightning.service
```

Start the service by:

```sh
systemctl --user start lightning
```

Stop the service by:

```sh
systemctl --user stop lightning
```

Restart the service by:

```sh
systemctl --user restart lightning
```

Check the service status by:

```sh
systemctl --user status lightning.service
```

## Problem statement


On tests done in a DigitalOcean Ubuntu 22.x, we had setup user-level services which were operated with `--user`. When the commands were executed as `--user` it failed with:

```sh
Failed to connect to bus: Operation not permitted (consider using --machine=<user>@.host --user to connect to bus of other user)
```

The user should be able to operate as user, it shouldn't be required to connect on behalf of other users. This means that even for a simple command, such as to retrieve the list of unit files:

```sh
systemctl --user list-unit-files
```

We'd get the error:

```sh
Failed to connect to bus: Operation not permitted (consider using --machine=<user>@.host --user to connect to bus of other user)
```

For any of the supported user unit locations e.g. $HOME/.config/systemd/user as documented in [Systemd unit documentation](https://www.freedesktop.org/software/systemd/man/systemd.unit.html), the result is the error above.

This is related to the load paths when running in user mode (--user), as described in the discussion [here](https://unix.stackexchange.com/questions/224992/where-do-i-put-my-systemd-unit-file/367237#367237).


**User-dependent**

**$XDG_CONFIG_HOME/systemd/user** User configuration (only used when $XDG_CONFIG_HOME is set)

**$HOME/.config/systemd/user** User configuration (only used when $XDG_CONFIG_HOME is not set)

**$XDG_RUNTIME_DIR/systemd/user** Runtime units (only used when $XDG_RUNTIME_DIR is set)

**$XDG_DATA_HOME/systemd/user** Units of packages that have been installed in the home directory (only used when $XDG_DATA_HOME is set)

**$HOME/.local/share/systemd/user** Units of packages that have been installed in the home directory (only used when $XDG_DATA_HOME is not set)

For example, if we check the `$XDG_RUNTIME_DIR` in a DigitalOcean box, we get the following output:

```sh
/run/user/0
```

For this reason and to provide support for a wider audience of users and systems, we've stuck to sudo to execute the service, but this should not be a requirement and is not recommended.

<Author
name="Helder Oliveira"
image="https://github.com/heldrida.png"
title="Software Developer + DX"
url="https://github.com/heldrida"
/>

0 comments on commit 1522314

Please sign in to comment.