Mosh: Mobile Friendly Remote Terminal

Kiki Luqman Hakiem
3 min readJan 11, 2024

--

One of the typical needs for software engineers and system engineers is the ability to remotely log into servers. Whether you administer the server or use the server as a development machine, you will need a secure channel to do that. SSH is invaluable for this.

But SSH can be frustrating at times. Consider the following cases:

  • Your internet connection is unreliable, even disconnected randomly.
  • You have poor signal strength because you’re far from a router or cellular tower.
  • You switch to different networks/VPNs.
  • You’re in a moving car/train where your network is constantly roaming.
  • You close your laptop’s lid to get to somewhere else and reopen.

You’re expecting to start where you left off right away. But all the above scenarios will make your SSH connection drop. Your shell will be frozen for a few seconds. And then this error message appears:

Connection to xx.xxx.xxx.xx port 22: Broken pipe

All you can do is reconnect. But you will need to reattach to your previous tmux session if you work with tmux. Or else you will have dangling vim temporary files, or even worse you might lose your progress.

So, is there any alternative? Let me introduce Mosh.

What is Mosh?

From their official website: Mosh is a remote terminal application that allows roaming, supports intermittent connectivity, and provides intelligent local echo and line editing of user keystrokes. It aims to be a replacement for interactive SSH terminals.

It uses a UDP-based protocol, called State Synchronisation Protocol (SSP), to synchronize the states of both client and server, as opposed to TCP used by SSH, which reduces the network latency and allows changes to the IP address. This results in a smoother typing experience as if you are typing in your local shell, and the ability to stay “connected” even after the aforementioned scenarios above.

Mosh uses SSH to establish an initial connection. The mosh client logs in to the server via SSH, and users present the same credentials (e.g. password, public key) as when you use SSH. Then Mosh runs the mosh-server remotely and connects to it over UDP.

Getting Started with MOSH

As mentioned above, Mosh relies on SSH to establish its initial connection. So, I assume that you already have SSH properly set up.

Install Mosh

Similar to SSH, Mosh also uses a client-server architecture, which means you need to install Mosh both on your remote server and on your local machine as a client. You can install Mosh with any package manager your OS has. For example, if you’re running a Debian-based server:

$ sudo apt-get install mosh

And if you’re using MacOS, then you can use homebrew:

$ brew install mosh

Installation for other OS can be found here.

Configure Server’s Firewall

Mosh uses UDP ports between 60000 and 61000. If your server is configured with a firewall, update the firewall to allow ports in that range. If your server is running Linux, you can use ufw:

$ sudo ufw allow 60000:61000/udp

If you’re using the cloud, consult their documentation on how to configure the firewall.

Mosh needs only 1 open port per connection. It depends on how many concurrent connections you expect, so you don’t have to open all 1000 ports.

Establish Connection

Mosh setup is done and it’s ready to use. Assuming that your SSH command looks like thisssh johndoe@example.com , then you can establish your Mosh connection with:

$ mosh johndoe@example.com

You can try to turn off and on your wifi, and continue typing. You’ll see the difference between using only SSH and using Mosh.

What if I use a different port for SSH? How do I select a particular port for my connection? You can find the answers, along with other scenarios, on their usage page.

--

--