Linux – Inspect unit status for user-units with systemctl as root

linuxsystemd

I've created a user unit (slice) and started it. With:

cytrinox@pollux$ systemctl status --user --full firefox-limits.slice
● firefox-limits.slice - Firefox Slice
   Loaded: loaded (/home/cytrinox/.config/systemd/user/firefox-limits.slice; static; vendor preset: enabled)
   Active: active since Sun 2018-11-25 00:09:14 CET; 37min ago
   CGroup: /user.slice/user-1000.slice/user@1000.service/firefox.slice/firefox-limits.slice
           └─run-r791a1fc1147748059accf82ecded4c56.scope
             ├─5291 /home/cytrinox/bin/Firefox/firefox
             ├─5451 /home/cytrinox/bin/Firefox/firefox -contentproc -childID 1 -isForBrowser -prefsLen 1 -prefMapSize 452779 -schedulerPre
             ├─5500 /home/cytrinox/bin/Firefox/firefox -contentproc -childID 2 -isForBrowser -prefsLen 1 -prefMapSize 452779 -schedulerPre
             ├─5517 /home/cytrinox/bin/Firefox/firefox -contentproc -childID 3 -isForBrowser -prefsLen 1 -prefMapSize 452779 -schedulerPre
             ├─5539 /home/cytrinox/bin/Firefox/firefox -contentproc -childID 4 -isForBrowser -prefsLen 1 -prefMapSize 452779 -schedulerPre
             └─5562 /home/cytrinox/bin/Firefox/firefox -contentproc -childID 5 -isForBrowser -prefsLen 1 -prefMapSize 452779 -schedulerPre

Nov 25 00:09:14 pollux systemd[858]: Created slice Firefox Slice.

I can inspect the current status.

But as, root, how can I get the status for this user unit?

root@pollux:/etc/systemd/system# systemctl status --full firefox-limits.slice
● firefox-limits.slice
   Loaded: loaded
   Active: inactive (dead)

Best Answer

Unfortunately, no, it's not possible to access the units running under the systemd unit manager while running as root...

It is almost possible to do so, by running systemctl --user status as root while setting the XDG_RUNTIME_DIR environment variable to point to /run/user/<uid> (see the relevant code in bus_connect_user_systemd()), but unfortunately this is not enough:

# XDG_RUNTIME_DIR=/run/user/1000 systemctl --user status
Failed to connect to bus: Operation not permitted

The problem is that, after connecting to the user manager, systemd checks that the uid running systemctl matches the one that owns the socket to the manager (see the relevant code in bus_check_peercred()).

So your best choice is to just use su to become the user in order to check the status of the unit. Furthermore, when using su, you still need to set XDG_RUNTIME_DIR, since otherwise systemctl might be unable to find the socket to the manager:

# su cytrinox -c 'XDG_RUNTIME_DIR=/run/user/$UID systemctl --user status'

(Or the appropriate systemctl status command you want for your Firefox slice unit...)