3

There is the usual command for listing the systemd services:

systemctl list-units --type=service

Then there is the --user parameter for enabling and starting a user service, executing the command from a user's session:

systemctl --user enable/start <service name>

I need, as root, to list services running under a certain user, something like:

systemctl list-units --type=service --user <username>

Haven't been able to find anything about this. Is it even possible?

2 Answers 2

1

Yes, it is, but by using sudo. Read man sudo sudoers, and

sudo -u theuser systemctl ...

will do the trick.

0

Use the machinectl command like this:

machinectl shell <username>@ /usr/bin/systemctl --user list-units --type=service --quiet

Here is an example with the local account erikdev on my computer.

[root@laptop ~]# machinectl shell erikdev@ /usr/bin/systemctl --user list-units --type=service --quiet
Connected to the local host. Press ^] three times within 1s to exit session.
  systemd-tmpfiles-setup.service loaded active exited Create User's Volatile Files and Directories

Connection to the local host terminated.
[root@laptop ~]# 

I'm running Systemd 248 on a Fedora 34 Linux computer.

[root@laptop ~]# cat /etc/fedora-release 
Fedora release 34 (Thirty Four)
[root@laptop ~]# machinectl --version
systemd 248 (v248.3-1.fc34)
+PAM +AUDIT +SELINUX -APPARMOR +IMA +SMACK +SECCOMP +GCRYPT +GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 +PWQUALITY +P11KIT +QRENCODE +BZIP2 +LZ4 +XZ +ZLIB +ZSTD +XKBCOMMON +UTMP +SYSVINIT default-hierarchy=unified
[root@laptop ~]# 

It might be possible to use systemd-run instead of machinectl, but I didn't get that to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.