Skip to content

How to rule remote shell sessions with tmux and mosh

If you’re like most SSH users, when your connection breaks, it’s bad news. 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 want to introduce two tools that solve these problems in the most elegant way possible.

tmux

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 horizontally. It’s like having several virtual monitors next to each other, each running a different shell session. You can move across these windows as desired. With the use of panes, you can split individual windows horizontally and/or vertically as desired, with each pane housing a different session. This is useful for tailing various log files in different panes and monitoring them simultaneously.

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 and moving towards the least powerful. GNU Screen is yet another terminal multiplexer, but its feature set, usability, and configurability are 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.

mosh

Remote terminal application that allows roaming, supports intermittent connectivity, and provides intelligent local echo and line editing of user keystrokes.

mosh is the other puzzle piece leading to the remote shell nirvana. After apt-getting mosh on the client and mosh-server on the server instead of invoking ssh yourserver.com invoke mosh yourserver.com. From now on you don’t have to worry about reconnecting to ssh or waiting for the server to echo back your characters.

7 thoughts on “How to rule remote shell sessions with tmux and mosh”

  1. Interesting, thanks ! As a side note, one has to ensure that LC_TMUX_SESSION_NAME is accepted on the server side, and sent from the client side :

    Server side : (typically /etc/ssh/sshd_config) :
    AcceptEnv LC_TMUX_SESSION_NAME

    Client side (typically ~/ssh/config) :
    Host *
    SendEnv LC_TMUX_SESSION_NAME

    Thanks!

  2. @Ieucos: Sure, thanks for your suggestion! The reason I didn’t include this because my /etc/ssh/ssh_config contained SendEnv LANG LC_* and /etc/ssh/sshd_config contained AcceptEnv LANG LC_* by default:

    Maybe it’s just a Ubuntu / Linux Mint default though.

  3. I also had to do the AcceptEnv/SendEnv trick with KRB5CCNAME using heimdal. Otherwise, the tmux session would be assigned a new cache name and not find any forwardable Kerberos tickets.

  4. I have converged info similar setup. Fish instead of bash and juice for connecting from Android when I am on the move.

  5. Slight grammar correction:
    “Not only [do] you have to reconnect but your session gets destroyed …”
    Adding the ‘do’ in there make the sentence read as it should.

    I love mosh and tmux! I do wish it was possible to connect to a tmux session with mosh in the same was you do with ssh ( `ssh user@severexample.com tmux a` ). But sadly from my understanding you’d have to implement mosh into tmux for this to be possible.

  6. @hicksca: Thanks for the correction, just fixed my post! I’m not so sure wether it’s possible to do what you’re after but it should be possible to pass ssh args via mosh.

Leave a Reply

Your email address will not be published. Required fields are marked *