Wednesday, April 24, 2013

Creating Persistent SSH Connections with TMUX

Persistent ssh sessions are the reason I began using tmux and yet I have still not been able to find a good tutorial on how they can be created. If you're reading this hopefully I can save you some time and you can start incorporating tmux into your bag-o'-tricks quickly.

First off, what do I mean by a persistent session? I want to be able to log into a remote machine, do some stuff, and log out without losing my running processes. Later, I should be able to log back in and pick up exactly where I left off. As an example, I am constantly running long simulations on the machine in my lab. I would like to be able to tunnel into my lab from home, start a simulation, and log out without interrupting the simulation. After a couple of hours, I should be able to log in again and check on the progress of the simulation. Simple enough, right?

Tmux allows you to do this by detaching a session. The important thing to remember is that the session lives on the remote machine. Therefore tmux must be installed (and running) on the remote machine. The tmux server takes care of processes that you started and `returns` them to you when you reattach.

So, in a few simple steps:

Log into your remote machine,
dkudrow@local_machine:~$ ssh dkudrow@remote_machine

Create a tmux session on the remote machine,
dkudrow@remote_machine:~$ tmux

After starting your simulation or doing some work, detach the tmux session. In the tmux command line enter,
:detach

No you can log out of the remote machine without interrupting the session,
dkudrow@remote_machine:~$ logout

To resume the session, log back into the remote machine and enter,
dkudrow@remote_machine:~$ tmux attach

And voila, just as you left it.

This is just a basic example. Things get more complicated with multiple sessions across multiple machines but hopefully this gets you started.

No comments:

Post a Comment