smux: SSH with auto reconnect & tmux, a mosh replacement
Mosh was a great idea, but the lack of IPv6 is enough of a nuisance that I've come up with a suitable alternative that's working great for me now.
This approach uses autossh + tmux. The benefits are:
- IPv6 support
- Uses ssh_config, therefore you can use ProxyCommand transparently
- Auto launches a tmux session, or reconnects to a running one
- Reconnects to the host if you lose your connection
The only real feature mosh adds that is not addressed here is the low-latency feedback.
Getting this going on OSX:
- Install autossh. I use MacPorts:
sudo port install autossh
-
Create the new smux script
#!/bin/sh if [ "X$1" = "X" ]; then echo "usage: `basename $0` <host>" exit 1 fi if [ "X$SSH_AUTH_SOCK" = "X" ]; then eval `ssh-agent -s` ssh-add $HOME/.ssh/id_rsa fi AUTOSSH_POLL=20 AUTOSSH_PORT=$(awk 'BEGIN { srand(); do r = rand()*32000; while ( r < 20000 ); printf("%d\n",r) }' < /dev/null) #AUTOSSH_GATETIME=30 #AUTOSSH_LOGFILE=$HOST.log #AUTOSSH_DEBUG=yes #AUTOSSH_PATH=/usr/local/bin/ssh export AUTOSSH_POLL AUTOSSH_LOGFILE AUTOSSH_DEBUG AUTOSSH_PATH AUTOSSH_GATETIME AUTOSSH_PORT # -t is the ssh option to force a pseudo terminal (pty) autossh -t $@ "tmux attach-session"
-
Make the new smux script executable:
chmod 755 /path/to/smux
- On the server make sure you have the following line in your /etc/tmux.conf. This will ensure tmux will start a new session if one doesn't already exist.
new-session -s main
-
Connect to your server with
smux your.serv.er
Test the auto reconnect by disabling your network then re-enabling it, or switching from wire to wifi, etc.
Written by Jinn Koriech
Related protips
9 Responses
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/66062/snoopy.png)
You don't need autossh
monitoring if you use ServerAliveInterval
and ServerAliveCountMax
in your ssh config.
I've documented the something similar https://coderwall.com/p/f528fw, details on my site: http://pempek.net/articles/2013/04/24/vpn-less-persistent-ssh-sessions/
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/62162/jinn.jpg)
@gpakosz. Thanks for that. I do use those settings and it works well in suspend scenarios, but if there's a network issue without the suspend the ServerAliveCountMax
may be triggered and the connection drops. This is where autossh comes in useful.
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/66062/snoopy.png)
It's not about getting rid of autossh. It's about not requiring opening monitoring ports
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/120542/none.jpg)
Look who it is! Hi Jinn.
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/132909/none.jpg)
This is great! I replaced "tmux attach-session" on the last line of the smux script with "tmux attach || tmux new" so that I don't have to modify the .tmux.conf on every server I connect to. Awesome-sauce! ;]
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/139556/none.jpg)
@gpakosz Personally rather than testing with a tmux attach || tmux new I prefer to use the -A option to tmux new-session to attach if a session exists or create one otherwise through tmux itself. This also lets me tweak the session name to include the host it was started from in case I had a lot of different places I was coming from (though you don't want to include the FQDN in case somebody tries to jump back through using your agent session).
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/66062/snoopy.png)
@dragon788 I believe my alias predates the -A option to tmux new-session. Thanks for the tip!
Hey Jinn,
Just to know, do you think this script is deprecated because Mosh supports IPv6 now? I'm doing a comparison between autossh+Tmux and Mosh+Tmux.
Cheers!
Unfortunately, this doesn't work with tmux -CC
Do u have an idea maybe?