If you’re like most ssh users when your connection breaks it’s bad news for you. Not only do you have to reconnect but your session gets destroyed and you have to make all the moves to restore the previous state. This doesn’t have to be that way. I’d like to say some words about two tools that solve these problems in the most elegant way possible.
tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached.
In the world of tmux there are windows and panes within windows. You can think of tmux windows as workspaces on the desktop that are aligned in a horizontal manner. It’s like having a number of virtual monitors next to each other each running different shell sessions. You can move across these windows as desired. With the use of panes you can split individual windows horizontally and/or vertically as desired, each pane housing a different session. This is pretty useful for tailing various log files in different panes and monitoring them at once.
You simply have to run the
tmux command to create a new tmux session. Once a session exists upon reconnecting over ssh you have to invoke
tmux attach to reconnect to your already existing session.
If you’re like me you may want to use tmux by default upon ssh’ing to servers. To make this happen you have to include
export LC_TMUX_SESSION_NAME=yourusername into your
~/.bashrc and wrap scp on the client side and invoke tmux automatically on the server side. On a related note you can also take a look at my tmux.conf which I believe defines more intuitive shortcuts than the default configuration.
There are a number of alternatives to tmux that I’d like to list starting with the most powerful towards the least powerful. GNU Screen is yet another terminal multiplexer but its feature set, usability and configurability is rather limited compared to tmux. dtach is like a minimalistic tmux featuring one pane inside one window and it only provides a minimal set of options. Finally, with the use of the nohup command you can make your (typically long-running) script immune to hangups and hence it can survive ssh disconnects.
Remote terminal application that allows roaming, supports intermittent connectivity, and provides intelligent local echo and line editing of user keystrokes.
mosh is the other piece of the puzzle leading to the remote shell nirvana. After
mosh on the client and
mosh-server on the server instead of invoking
ssh yourserver.com invoke
mosh yourserver.com. From this point on you don’t have to worry about reconnecting to ssh or having to wait for the server to echo back your characters anymore.