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.
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) :
Client side (typically ~/ssh/config) :
@Ieucos: Sure, thanks for your suggestion! The reason I didn’t include this because my
SendEnv LANG LC_*and
AcceptEnv LANG LC_*by default:
Maybe it’s just a Ubuntu / Linux Mint default though.
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.
I have converged info similar setup. Fish instead of bash and juice for connecting from Android when I am on the move.
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 email@example.com tmux a` ). But sadly from my understanding you’d have to implement mosh into tmux for this to be possible.
@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.
@hicksca Maybe this is what you want: https://blog.filippo.io/my-remote-shell-session-setup/ (mosh HOST — tmux a)
The tmux + mosh + fish feels like I entered new century.