I’ve been looking into why I’m losing the running processes, like tmux, when I disconnect from ssh. And I found section 5.9 of the manual:
5.9 tmux/screen session killed when disconnecting from SSH
If your processes get killed at the end of the session, it is possible that you are using socket activation and it gets killed by systemd when it notices that the SSH session process exited. In that case there are two solutions. One is to avoid using socket activation by using ssh.service instead of ssh.socket. The other is to set KillMode=process in the Service section of ssh@.service.
The KillMode=process setting may also be useful with the classic ssh.service, as it avoids killing the SSH session process or the screen or tmux processes when the server gets stopped or restarted.
When searching and trying to find my sshd.service, I find it in the following locations:
I’m not sure which of the sshd.service files is in effect so I checked all of them. Interesting enough, I can see KillMode=process in all of the files. Still, when I disconnect from the ssh, the tmux session is lost.
Does anyone know how I can keep my tmux process when disconnecting from ssh?
For good measure, here’s the content of /usr/lib/systemd/system/sshd.service (which I have a hunch is the correct file):
Thanks for the reply. I didn’t know about that. Even though it didn’t help, it might be my fault.
First of all, I don’t have the /etc/systemd/logind.conf.d/20-kill-user-processes.conf file. But what I do have is /etc/systemd/logind.conf. And in there, I could find the KillUserProcesses=no commented out. Which I uncommented and then reboot. But still, when I disconnect the ssh, I lose tmux process. Any other idea?
I use something similar in my .bash_profile or on systems with ZSH .zprofile . The if part looks like
if [ "$TMUX" = "" ] && [ "$SSH_CONNECTION" != "" ]; then
which does the same as the example form @linux-aarhus . The tmux commands are the same. I never needed to change systemd configs for this to work. Just make sure you don’t kill the tmux process, simply detach form it and exit, or just within tmux
That edit didn’t actually have any effect because #KillUserProcesses=no
is the default anyway
as you can read in the text at the top of the file:
# Entries in this file show the compile time defaults. Local configuration
# should be created by either modifying this file, or by creating "drop-ins" in
# the logind.conf.d/ subdirectory. The latter is generally recommended.
# Defaults can be restored by simply deleting this file and all drop-ins.
I just wanted to say that while I’m grateful for the new suggestions, they didn’t help.
Also, I tested with a test VM and it worked without any effort. I’m sure some config on my machine is wrong. It’s just that I don’t know what it is. It could be the fact that I’m using VPN, or that I’m keys instead of password. But whatever it is, I still lose my processes when I disconnect the SSH. And again, this only happens if I’m not logged in to my account on the server. If I log in to the server first and then SSH remotely, the processes inside tmux will remain.
I just added a new user and this time, I used the password for authentication instead of the key. And the same problem. When I disconnect the SSH, the tmux process is lost.
I’m not sure if adding a new user caused it or something else changed, but now even logging in to server locally does not help. No matter what, when I disconnect the SSH, the processes are lost!
Also, I matched the /etc/ssh/sshd_config line by line on my working VM with my broken machine. I could not break the VM. VM is still working just fine (I do not lose my process after disconnecting SSH from my VM). This means that my problem is not related to SSH config. Unless there’s some config some other place that I missed.
New finding, the process in tmux survives an SSH disconnect if the tmux session was initiated on the local machine and then attached to by the remote SSH. In this case, I won’t lose the process if I reconnect to the SSH.