ubuntu下如何列出另一个用户的用户服务

Kat*_*aty 5 services systemd

有一个列出 systemd 服务的常用命令:

systemctl list-units --type=service
Run Code Online (Sandbox Code Playgroud)

然后是 --user 参数,用于启用和启动用户服务,从用户会话执行命令:

systemctl --user enable/start <service name>
Run Code Online (Sandbox Code Playgroud)

我需要以 root 身份列出在某个用户下运行的服务,例如:

systemctl list-units --type=service --user <username>
Run Code Online (Sandbox Code Playgroud)

一直无法找到任何有关此的信息。有可能吗?

wal*_*tor 1

是的,确实如此,但是通过使用sudo. 读man sudo sudoers

sudo -u theuser systemctl ...
Run Code Online (Sandbox Code Playgroud)

会成功的。


Eri*_*und 1

使用machinectl这样的命令:

machinectl shell <username>@ /usr/bin/systemctl --user list-units --type=service --quiet
Run Code Online (Sandbox Code Playgroud)

以下是我的计算机上的本地帐户erikdev的示例。

[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 ~]# 
Run Code Online (Sandbox Code Playgroud)

我在Fedora 34 Linux 计算机上运行Systemd 248

[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 ~]# 
Run Code Online (Sandbox Code Playgroud)

也许可以使用systemd-run代替machinectl,但我没有让它发挥作用。