Mosh to the rescue

As an average geek I often use SSH to login to remote servers. As I’m also using a laptop, I had to reopen the connections when I switched locations or more specifically networks. Sure it’s not a big thing to just wait until the old connection timed out and go back one step in the shell history to recall the connection.

But us geeks are also bloody lazy when it comes to computer stuff and every keystroke you can save is possibly worth doing so 😉

This is where mosh comes in handy. It’s magical little wrapper around the SSH connection that gives me a persistent SSH connection across actual network connection switches. That means my SSH connections feel like they don’t die when I roam from home to the office or from a caffee WiFi to the next.

MOSH tackles the problem of changing network connections at a completely different level:

Remote-shell protocols traditionally work by conveying a byte-stream from the server to the client, to be interpreted by the client’s terminal. (This includes TELNET, RLOGIN, and SSH.) Mosh works differently and at a different layer. With Mosh, the server and client both maintain a snapshot of the current screen state. The problem becomes one of state-synchronization: getting the client to the most recent server-side screen as efficiently as possible.

And the best thing with mosh… it easy to install on most platforms. You just install a pre-build package for your favorite operating system or compile from source. You have to open certain firewall ports to allow the special UDP connection mosh is using to synchronize the state.

The latter might be a problem for corporate environments where you have to use a proxy or where UDP is not allowed.

On my Macs I had to fiddle a bit with the Terminal environment settings to get mosh working properly. Initially it was complaining about incompatible LANG settings. But with the following settings you should be able to get it working (put that in your ~/.bashrc) :

export LANG="en_US.UTF-8"
export LC_COLLATE="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
export LC_MESSAGES="en_US.UTF-8"
export LC_MONETARY="en_US.UTF-8"
export LC_NUMERIC="en_US.UTF-8"
export LC_TIME="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"

For everyday usage you just replace your “ssh” command with “mosh” to open a connection. Mosh will keep that connection open as long as you want. It’ll magically reconnect when you’re switching networks.

Autor: falko

a *nix nerd