Jump to content

Systemd/User Services: Difference between revisions

From NixOS Wiki
Ardenet (talk | contribs)
repositon translate tag
Tags: Mobile edit Mobile web edit Visual edit
Ardenet (talk | contribs)
Marked this version for translation
Tags: Mobile edit Mobile web edit
 
Line 40: Line 40:
You'll also likely want to change to {{ic|<nowiki>wantedBy = [ "multi-user.target" ];</nowiki>}} so the service starts at boot time.
You'll also likely want to change to {{ic|<nowiki>wantedBy = [ "multi-user.target" ];</nowiki>}} so the service starts at boot time.


== Enabling a service for specific users ==
== Enabling a service for specific users == <!--T:10-->


<!--T:11-->
By default, enabling a user service enables it for every user for which systemd spawns a service manager. If you wish for the service to be run only for specific users (say, {{ic|<nowiki>UserA</nowiki>}} and {{ic|<nowiki>UserB</nowiki>}}), use {{ic|<nowiki>ConditionUser</nowiki>}} ({{ic|<nowiki>man 5 systemd.unit</nowiki>}}):
By default, enabling a user service enables it for every user for which systemd spawns a service manager. If you wish for the service to be run only for specific users (say, {{ic|<nowiki>UserA</nowiki>}} and {{ic|<nowiki>UserB</nowiki>}}), use {{ic|<nowiki>ConditionUser</nowiki>}} ({{ic|<nowiki>man 5 systemd.unit</nowiki>}}):
</translate>
</translate>
Line 52: Line 53:


<translate>
<translate>
<!--T:12-->
Likewise, you can also disable a service for a specific user:
Likewise, you can also disable a service for a specific user:
</translate>
</translate>
Line 62: Line 64:


<translate>
<translate>
== Usage ==
== Usage == <!--T:13-->


<!--T:14-->
To interact with user-specific systemd services, use the <code>--user</code> flag with the <code>systemctl</code> command. For example, to check the status of a user service:
To interact with user-specific systemd services, use the <code>--user</code> flag with the <code>systemctl</code> command. For example, to check the status of a user service:
</translate>
</translate>
Line 70: Line 73:


<translate>
<translate>
<!--T:15-->
To view logs for a specific user service, use <code>journalctl</code> with the <code>--user-unit</code> option:
To view logs for a specific user service, use <code>journalctl</code> with the <code>--user-unit</code> option:
</translate>
</translate>
Line 76: Line 80:


<translate>
<translate>
<!--T:16-->
To list all active user units:
To list all active user units:
</translate>
</translate>
Line 82: Line 87:


<translate>
<translate>
<!--T:17-->
[[Category:systemd]]
[[Category:systemd]]
</translate>
</translate>

Latest revision as of 22:41, 7 October 2025

Systemd supports running a separate instance of systemd for a given user, allowing the user to control their own services. See here for more information: https://wiki.archlinux.org/title/Systemd/User

In NixOS, a user service can be expressed with systemd.user.services.<name>, as documented here: https://search.nixos.org/options?query=systemd.user.services

This may be useful if you want a user to be able to start, stop, and restart their own instance of a service without needing to make the user a sudoer.

Here is an example:

systemd.user.services.my-cool-user-service = {
  enable = true;
  after = [ "network.target" ];
  wantedBy = [ "default.target" ];
  description = "My Cool User Service";
  serviceConfig = {
      Type = "simple";
      ExecStart = ''/my/cool/user/service'';
  };
};

By default, user services will be stopped when the user logs out and will start again when the user logs back in due to us setting wantedBy = [ "default.target" ] in the example.

Keeping user services running after logout

If you need a user service to stay running after a user logs out, you need to enable "lingering" by setting users.users.<username>.linger = true;

You'll also likely want to change to wantedBy = [ "multi-user.target" ]; so the service starts at boot time.

Enabling a service for specific users

By default, enabling a user service enables it for every user for which systemd spawns a service manager. If you wish for the service to be run only for specific users (say, UserA and UserB), use ConditionUser (man 5 systemd.unit):

systemd.user.services.my-cool-user-service = {
  unitConfig.ConditionUser = "UserA|UserB";
};

Likewise, you can also disable a service for a specific user:

systemd.user.services.my-cool-user-service = {
  unitConfig.ConditionUser = "!root";
};

Usage

To interact with user-specific systemd services, use the --user flag with the systemctl command. For example, to check the status of a user service:

 $ systemctl --user status my-cool-user-service

To view logs for a specific user service, use journalctl with the --user-unit option:

 $ journalctl --user-unit my-cool-user-service

To list all active user units:

 $ systemctl --user list-units