1

As per this answer, I enabled lingering for a user on a headless system. However in loginctl list-sessions, the user is not listed yet. If I login with this user by ssh it is shown, once I disconnect again it is missing.

How can I generate the lingering session without restarting the entire system

I have unsuccessfully tried to systemctl restart systemd-logind.

Edit: My goal is to run a timer controlled by this user. If i try this form a sudo -iu shell after enabling lingering, I get the following error:

$ systemctl --user status servicename
Failed to connect to bus: No such file or directory

If I login per ssh (and a session shows up in loginctl) it works as epxected. But now I noticed it only works within the ssh shell, still not in the sudo shell. So it is in fact probably not related to the session.

  • You might want to explain why you want to enable linger. Do you want to run user units at boot, such as services or timers? If so, that should work, regardless of sessions... See my answer below. Edit your question if you'd like to explain why you think you need lingering and what you're trying to accomplish. – filbranden Aug 16 '18 at 1:51
  • @FilipeBrandenburger thanks for your clarification. My apologies for not including enough information leading to an XY-Problem. I have edited the question to include more information what I try to do and why I though it was wrong. I guess my actual question sohuld have been How to run systemctl in a sudo shell. I will gladly accept your answer - which helped me out of the dead end - and possibly post another question if I cannot resolve the other issue. – Zulan Aug 16 '18 at 16:16
  • 1
    I edited my answer to cover your follow up. In short, you need to export XDG_RUNTIME_DIR=/run/user/$UID when you're using sudo to become the new user. I hope that helps! – filbranden Aug 16 '18 at 17:48
6

You're confusing "users" with "sessions" here.

Using loginctl enable-linger myuser will make the user manager for user myuser start at boot (and start immediately if it's not yet started), but it won't really start any sessions for the user (those only happen when the user logs in.)

If you use the loginctl list-users command you'll see the effects of it. You can also use ps -fu myuser to see the user manager (systemd --user) running for your user as soon as you enable linger:

[root@myhost ~]# loginctl list-users
UID USER
  0 root

1 users listed.
[root@myhost ~]# ps -fu myuser
UID        PID  PPID  C STIME TTY          TIME CMD
[root@myhost ~]# loginctl enable-linger myuser
[root@myhost ~]# ps -fu myuser
UID        PID  PPID  C STIME TTY          TIME CMD
myuser   12345     1 20 18:44 ?        00:00:00 /usr/lib/systemd/systemd --user
myuser   12349 12345  0 18:44 ?        00:00:00 (sd-pam)
[root@myhost ~]# loginctl list-users
 UID USER      
   0 root      
1001 myuser

2 users listed.

But no new sessions for myuser exist, since it's not logged in through the console, GUI or SSH:

[root@myhost ~]# loginctl list-sessions
SESSION UID USER SEAT TTY  
      1   0 root      tty0

1 sessions listed.

But regardless of sessions, the user manager is started, so if your user has user units (such as services or timers), those will get started at boot regardless of the user having logged in or created a session yet.

I hope that explains it to you.


UPDATE: From your edit to the question, looks like the trouble you're really having is running systemctl --user commands from a sudo -i shell and not an SSH session.

You can work around that by exporting the XDG_RUNTIME_DIR after you switch to the user, such as:

$ export XDG_RUNTIME_DIR=/run/user/$UID

Or:

$ XDG_RUNTIME_DIR=/run/user/$UID systemctl --user status servicename

The reason you don't need that while logging in through SSH is that SSH will authenticate you with PAM and one of the PAM modules will set those environment variables for you. If you use sudo, you don't go through PAM (at least not the full PAM stack) so you don't get these properly set.

  • 1
    Wow, thank you so much for two answers in one! Not only does your instruction help me with the task, you also clearly explain why it is necessary. Solved both my X and Y problem. – Zulan Aug 16 '18 at 20:22

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.