If you’re a technical professional, you undoubtedly know about Secure Shell (SSH) and use it regularly to connect to remote systems. Though a great tool, SSH assumes that you have a reliable wired connection. It doesn’t always work as well on wireless connections, which can be unreliable. One new tool that has changed that is Mosh, or Mobile Shell, which allows users to connect to remote systems and stay connected even when the network goes down or you switch networks.
Why Mosh?
Secure Shell is indispensable for administrators, programmers and power users who need to log into servers, either across the hall or across the country. It’s a secure, encrypted replacement for Telnet. It’s a great tool, but it was designed in the 90s, an era before mobile computers on Wi-Fi became common. SSH assumes a reliable wired connection, which isn’t always the case when users go mobile. Wi-Fi connectivity, as many people have found, is often spotty, and it’s impossible to change connections, say from Wi-Fi to LTE, without breaking a connection.
Other times it’s not the fault of the connection, but rather the user. Or, as they say in IT, it’s a PEBKAC (problem exists between keyboard and chair). My all-time favorite is closing the lid while forgetting that I’m logged into a remote machine via SSH. I open the lid and type in the terminal window and nothing happens. All I can do is kill the session and log in again. It’s annoying, but I’ve done it countless times and I’m sure you have as well.
Many people try to mitigate this by using programs like GNU Screen and tmux. These are terminal multiplexers that not only give you something like tabbed browsing for the command line, but can also preserve your session in case your connection goes down. Just log back in and you can pick up right where you left off. As handy as these programs are, the problems with SSH itself still remain.
I was resigned to my SSH fate, but one day I stumbled on a new program on a shell server I hang out on. Mosh is an attempt to improve SSH, making it suitable for use on laptops. It’s developed by some clever people at MIT, the folks who brought us Lisp (so they probably know a good thing when they see one). Keith Winstein, one of the principal developers, has created a video showing how it works.
Mosh’s creators describe it as a replacement for SSH that’s more robust and responsive, especially over Wi-Fi, cellular and long-distance links.
How It Works
Mosh uses a new protocol called the State Synchronization Protocol (SSP). This builds on traditional remote connection protocols like Telnet and SSH. Under SSH, the server just sends some bytes downstream to the client to be interpreted.
SSP adds another layer. The server and the client keep track of what has been sent using sequence numbers. If the server gets a sequence number that is higher than the one it previously sent, it’s smart enough to figure out that the client has moved to another connection. This means that it is easy to roam from Wi-Fi network to Wi-Fi network, or from a Wi-Fi network to a cell network, or from a Wi-Fi network to a wired connection – and so on.
Another nice feature Mosh provides is real-time character echo. Usually, if you’re on SSH, you might find a delay between typing and waiting for your characters to appear on the screen as the server echoes what you’re typing back to you.
Getting It
If you’re intrigued, you’ll be glad to know that installing Mosh is easy enough. Most major Linux and Unix distributions have it in their repositories. The Mosh homepage shows examples using Debian and Ubuntu, as well as Gentoo, Arch and Fedora. Because Mac OS X is based on Unix, Mac users can join in as well. There’s a native package, and people who want to compile it on the Mac using Homebrew and MacPorts. If your system doesn’t have Mosh as a package, you can download and compile it yourself if you need to.
When you’ve got the client installed, you’re ready to get Mosh up and running and log in. You log in exactly like you do with SSH. In fact, Mosh doesn’t actually handle the logins; it hands off the login information to SSH. This is useful if you use SSH’s public key encryption features to log in securely without the need for a password.
There is one catch, however. You need the Mosh server to connect. Fortunately, you don’t have to be a superuser to install it. You can even put it in your home directory and launch it that way if you can’t talk the sysadmin into installing it. Mosh is still new, so perhaps someday it will become as ubiquitous on servers as SSH is.
Another thing: Mosh expects your terminal to support UTF-8. Just about all the modern terminal emulators do, but you might have to set the $LANG environment variable before the server on the remote system accepts the connection.
Staying Alive
Now that you’re connected, you can get to work coding or editing your configuration files or administering remote machines, either at home, at work, on the train or at cruising altitude.
You can test how robust Mosh is by disconnecting your Wi-Fi. Mosh will display a message at the top of the terminal saying it doesn’t have a connection, along with a timer. Reconnect and your session will pick up right where it left off. It also works if you close your laptop lid.
Mosh is even better when paired with a terminal multiplexer like GNU Screen or tmux. They’ve been used in the past the way Mosh is, to keep SSH sessions active over unreliable connections, but they still offer some advantages when used with Mosh. For example, you can detach from one terminal, log out and log in from another machine, call up the multiplexer and have it continue. This is very useful for long jobs. It’s also popular for avid users of IRC to stay connected on a shell server and detach as necessary.
Time for Mosh?
Now that you’ve gotten a taste of how Mosh can make your mobile remote login experience much better, why not try it for yourself?