How to keep tmux running after SSH is disconnected?

When ssh connection is disconnected, loss of terminal occurs and SIGHUP is sent to the program.
Try to run tmux with the nohup prefix to tell it to ignore the SIGHUP signal and keep running upon ssh disconnection.

As @xabbu mention it may be an issue if you have a gui running on the server.

But that depends on the GUI used on the box in question. On one server I used vnc over ssh using an openbox session - and at the same time ran several tmux sessions started over ssh for simulatanous build of different iso-profiles - that gave me no issues.

The test I just conducted - mentioned above - was using an active webserver - that system is cli only.

Some keypresses may not work depending on the terminal emulator used on client - e.g. Konsole on Plasma interferes with some commands when using the default schema.

I installed and started XFCE desktop on that Ubuntu Server 22.04, and connected to it over SSH and tested it. The tmux sessions were still intact when I closed the SSH window and reconnected to it.

I think that will work, but on Ubuntu Server, the session is not closed when closing SSH, even when I did not start with nohup. How is that possible?

Well, if this is the problem of Gnome, and the choice is between not using Gnome and not being able to use tmux like Ubuntu, then I guess I have to choose the latter. It is not that I like to use Gnome over XFCE, but I use XRDP (remote desktop) and XFCE’s password dialogue (the thing that pops up when an app requires root privilege) does not show inside of XRDP at all (unlike Gnome for which it works at least for some apps), so I must use Gnome.

About the “using GUI on server is stupid” thing, well, a lot of things can be done within terminal, but often doing things inside of remote desktop is easier than that. For example, I think I could use terminal for disk/partition management, but it is a lot easier and less error-prone to do that with Gparted GUI inside of remote desktop.

You could dessect an Ubuntu installation to find why it behaves differently.

The systemd-run command is not that complicated and it is a onetime - until the system has been restarted.

vnc over ssh is easy to setup if a GUI is a requirement.

The partition tool argument is not very good - you can do the same with cli using any of these tools parted, cfdisk, gdisk, cgdisk.

I had tested the Ubuntu only with the XFCE. If your test results are real, then the Ubuntu will have the same problem if I install Gnome desktop on it. I could test it, but I kind of feel like it will be a waste of time.

Anyway, one of the biggest problems for me to use Linux as a server is that the password dialogue GUI does not show up inside of XRDP. My life would be much simpler if this gets somehow fixed in future versions of Linux…

This has nothing to do with tmux or ssh.

I can confirm the problem. Thus, I think the “solved” attribute attached to the thread may be premature. After all, it seems like it is anything but solved.

My situation: I used to ssh into a Manjaro XFCE installation where tmux behaved as expected (still alive after logging out). I re-installed Manjaro Gnome two weeks ago and I am getting the problem that has been described. tmux will just terminate when exiting the ssh session (which kind of defies the whole purpose of tmux).

I am not quite sure whether the problem is actually related to XFCE vs Gnome or rather a config difference between an “old” and a very recent installation of Manjaro.

By the way, loginctl user-status says Linger: yes, so there might be a different setting at the heart of this problem.

Well, I usually take a reply that can show “what you want (practically) cannot be done” as the solution/answer, because it saves me from wasting more time on it and move on to other workarounds, but it seems that if I mark it as solved, the thread will soon be closed and no one can reply, so for people like you who may want some more discussion, I removed the solution mark. I will mark the reply I had marked as a solution a while later when there will be no more discussion going on.

tmux is not really designed for use with gui systems as a display manager may interfere with running process.

If you have a running proces on a system where you are logged in using the display manager then your processes will be killed upon logout.

The same thing may happen with xrdp becauae Linux != Windows.

The only way I have ever gotten to keep running process active within a GUI on disconnect is to use VNC. Setting up VNC can be done in a manner where login over ssh and then a session is started in your userspace. When you close the VNC session without logging off the session keeps running and you can reattach to your session by relogin using ssh.

I don’t know how well this works with wayland - and as a reminder - Manjaro Gnome defaults to wayland - so perhaps your issue is related to or caused by how wayland works.

XRDP session works the same as that of Windows. Just closing the client does not terminate the session. That is, if I log into to the XRDP server as the same account, all opened applications/windows are still there. I need to explicitly click the logout button of the desktop (as you would locally do).

PS: In case there might be some misunderstanding, my use of XRDP is not directly related to tmux. The only relation is that I use two things on the same server. I do not use tmux in the server’s local terminal window inside of the XRDP, nor I clicked the GUI “log out” inside of the XRDP window when I tested tmux in terminal. I used tmux in the SSH session which was running on Windows Terminal app running on a remote (in the point of view of the server) Windows computer, separately from XRDP.

I had this issue with screen/tmux on Manjaro GNOME and finally managed to resolve it (it has been a long day that had me contemplating switching distro in my darker moments). It turns out the key is indeed the KillUserProcesses option of systemd-logind.

On my Manjaro GNOME machines there was a config file at at /etc/systemd/logind.conf.d/20-kill-user-processes.conf with KillUserProcesses=1 set which overrides the default Arch/Manjaro value of 0. Remove this file, reboot, and then screen, tmux sessions etc will persist when opened via SSH.

It’s worth noting that I don’t know why Manjaro GNOME overrides this behaviour by default, so there could be unintended side effects. I will try to remember to report back if I find any.

Thanks goes to the earlier commenters in this thread that helped steer me in the right direction!!

1 Like

Yeah, I checked it, and there was the file with KillUserProcesses=true. I commented that out.

But the thing is, I been testing EndeavourOS as my next distribution; I checked EOS, and there was no such file in the first place. I have encountered another weird problem like Super+Space not working for FCITX5+GTK4, so I gave EOS a try. EOS did not have that problem.

I think the developers at Manjaro should have added some sort of comment above that KillUserProcesses=true when they customised Gnome and added that configuration, so that users would understand why it is added and know what the “consequences” of removing that line are.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.