Frequently I want to exit my ssh session without also killing the tmux session I am using. To do this I have to run the following commands:

tmux detach

or alternatively use the shortcut Ctrl+B D and then exit.

Is there a way to streamline this into one command? I've tried using an alias but it seems to execute both commands inside the tmux session.


You can use tmux detach -P.

Or use ~. to exit ssh (which will detach tmux because its tty disappears).

  • tmux detach -P did the trick for me thanks! – Adam Griffiths Oct 15 '19 at 8:29

Instead of automating the deconnection, I would change the connection. I’m guessing you currently connect using SSH, then run tmux attach; if you combine both steps,

 ssh -t user@host tmux attach

the SSH session becomes tied to the lifetime of the attaching tmux process, rather than the initial shell, and detaching from tmux will automatically close the SSH connection.

  • Thanks for your answer, it doesn't help me because of my particularly weird setup, but I'm sure it will help others. – Adam Griffiths Oct 15 '19 at 8:11
  • Hopefully someone else will come up with an approach which will also work for you! – Stephen Kitt Oct 15 '19 at 8:16

Unless your setup is too weird, you can kill the SSH connection with Enter ~ . (all separate keypresses). The tmux session should become detached (like with any other SSH disconnection).

  • Yea, I've seen this one but I like to enter commands at the command line rather than using escape sequences :> – Adam Griffiths Oct 15 '19 at 8:29

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.