5

I am using tmux on a remote machine that I access over ssh. For some reason, the tmux sessions do not persist between consecutive ssh login sessions. I do not have this issue while logging into this other remote machine that I have access to. This is essentially the same issue as described in this question.

However, the machine that I use already uses ssh.service rather than ssh.socket, so the accepted answer does not work for me.

Exact steps taken:

  1. Login to the machine via ssh
  2. Start a tmux session, do some work.
  3. Detach session, and possibly attach again (this works).
  4. Log out with Ctrl+D.
  5. Login again and try tmux a or tmux ls (doesn't work).
Abhilash
  • 339
  • Also, did you try the KillMode=process option, listed in the answer you linked to? If that works, then this is actually a duplicate. – Artur Meinild Jul 20 '21 at 10:30
  • @ArturMeinild KillMode=process is what I have by default in the Service section of sshd.service. – Abhilash Jul 21 '21 at 04:42

2 Answers2

8

After some research, I found the solution to my problem.

The reason for the killing of the tmux sessions was the default setting of KillUserProcesses=yes in /etc/systemd/logind.conf.

From the man page of logind.conf,

KillUserProcesses= Takes a boolean argument. Configures whether the processes of a user should be killed when the user logs out. If true, the scope unit corresponding to the session and all processes inside that scope will be terminated.

The solution is to explicitly start tmux in the user scope rather than in the session scope using the following.

systemd-run --scope --user tmux

Additionally, you need to enable "lingering" so that the tmux process can persist even if there is no active user session.

loginctl enable-linger

For more details, check the examples in the man page for systemd-run.

Abhilash
  • 339
4

While I don't remember if I've ever experienced the problem, I've been using a user service to have an "always-on" tmux server running anyway:

$ cat /home/tom/.config/systemd/user/tmux-server.service 
[Unit]
Description=tmux server

[Service] Type=forking ExecStart=/usr/bin/tmux -f %h/.config/tmux/server.conf start-server ExecStop=/usr/bin/tmux kill-server

[Install] WantedBy=default.target

$ cat /home/tom/.config/tmux/server.conf 
set -s exit-empty off
set -s exit-unattached off

and optionally:

set -g detach-on-destroy off

Certainly it requires lingering as well.

Tom Yan
  • 731