Using Mosh

Let’s take a simple situation: you are on a slow and / or unreliable access to the Internet, for example a tethering access on your mobile phone while on a train.

You have received an urgent monitoring mail that says a machine from your IT infrastructure requires immediate attention.

This is it: you know you need to SSH to this machine. Nightmare: the connection freezes, drops, time outs, lags, things take minutes where they should take seconds and frustration and anger grows.

Fear not: the tool I’m going to introduce to you is the solution !


What’s the problem with my SSH connection?

Well, that’s quite simple actually, SSH is based on the TCP protocol, that works like this:

mosh

To make things simple, let’s say that every TCP “segment” is at least acknowledged one time (delivery guarantee), and this segment is not considered delivered until the response is received (there are optimizations around that, see this link for details).

Also, it means that TCP uses the “session” concept, (every new connection must be established, used and then destroyed), and also that additional constraints apply such as timeouts if the session is inactive for too much time, lags if the packet ordering is messed up or your connection has a large jitter

Basically, TCP just sucks big time when you are on a train and using a mobile Internet access. Really.

So, how can this “mosh” thingie help me?

mosh tries to solve the problems we saw before, by using two concepts:

  • Using predictive typing

The UNIX gurus / embedded systems users out there will remember their last telnet session on a machine.

mosh tries to reproduce the same feeling telnet offers when using the local line echo mode: everything you type is echoed to you directly instead of waiting for the remote server to acknowledge the keystrokes, underlining the text that has not been acknowledged yet as you type.

The result is a fluid typing experience even on slow and lossy links, instead of endless frustration and typos.

  • Using UDP

UDP is the ultra simplified counterpart of TCP: no sessions, no acknowledgements, no delivery guarantee, it is purely a “fire and forget” approach.

mosh uses UDP to work around the problems that SSH has by using TCP: no more timeouts, a slightly more responsive behavior…

TL;DR, how does it work?

Everything is available here.

On most GNU/Linux distributions, a simple apt-get/yum/zypper/… install mosh is enough.

Then, just type “mosh me@mymachine.domain.com” instead of “ssh me@mymachine.domain.com”. mosh will initialize the connection using SSH, and will then fallback to its own protocol. It’s as simple as that!

Tips

  • If using a non standard port for SSH and no SSH configuration, you can use the “–ssh=’’” switch to specify which arguments you want to pass to the ssh command while connecting, just like -e with rsync
  • mosh runs entirely as the user it is connected with, no additional daemons needed at all
  • By default, mosh uses the ports 60000 to 61000 (UDP) to listen for connections, you’ll want to open them in your firewall (a shorter interval is possible, you need as much open ports as you will have connections opened)
  • An opened connection will survive a sleep from the computer, even if you switch from the office or your home to somewhere else! Quite cool, eh?
  • The JuiceSSH client for Android supports mosh natively

I hope you will enjoy using mosh 🙂 I have come to depend on it and would never live without it again!

Share this post

Scroll to Top
Rudder robot

Directive NIS2: how to get ready? Rudder can help you

Security management module details

This module targets maximum security and compliance for managing your infrastructure, with enterprise-class features such as:
Learn more about this module on the Security management page

Configuration & patch management module details

This module targets maximum performance and reliability for managing your infrastructure and patches, with enterprise-class features such as:

Learn more about this module on the Configuration & patch management page