Skip to main content
SSH

Get Rid of Network Connectivity Issues in SSH with Mosh

Struggling with SSH connection over a slow and inconsistent network? Mosh overcomes the shortcomings of SSH by giving you a steady connection over SSH.

seeni

To login into a remote system, we used to use telnet. Due to the severe security vulnerabilities of telnet, it was replaced by ssh.

SSH allows users to login into remote systems with/without password but with the help of a private key and a public key. The main advantage of SSH is its encryption. It allows user to communicate with the remote system without any fear of eavesdropping or tampering.

The shortcomings of SSH

Server Network Connection

To put working of ssh in simple terms, it is a TCP connection of encrypted packets.

SSH sends the packets between the local and remote system just like telnet but the contents of the packet are encrypted. There is no buffering and processing in the middle. Even the characters you type are sent to the remote system and then the remote system sends its response (which contains the character typed to be displayed on the screen) to the local system.

Can you see that whole round trip to see a single character you typed ???

“””It just sends/receives encrypted packets“””

But what happens when the network is disconnected or is very slow. TCP connection will be broken and the local system will be left hanging in the air without any error. It’s not the typical frozen Linux system, just a frozen terminal with the disconnected SSH connection.

You might have had frustration with ssh in one of the below situations.

  1. Your network connection is disconnected. Your ssh session will not show anything. When you type in the shell, you will notice that nothing is happening. And then you will check the internet/Wi-fi icon.
  2. When you have a slow internet connection, the same situation will arise. The characters you type in the local system may appear after seconds, or in worse cases, even minutes.
  3. When you are inactive for a long period, sometimes SSH will stop working even though your computer weren’t idle and network connection was active.

To summarize, SSH provided great security but little usability in high latency connections. You may use the nohup command in Linux to continue running the commands even after SSH connection drops out, but it’s not really a good solution. You know what’s better? Mosh!

Mosh: SSH without the connectivity issues

Mosh SSH
Mosh is better than SSH

Mosh stands for MObile SHell. It gives a simple solution to all the problems mentioned above. It uses a technique similar to buffering and synchronization of items (objects) using SSP protocol (state-Synchronization Protocol). And to transmit packets, it uses UDP which is connection-less protocol.

To know how mosh works, read its official page.

To avoid reinventing the wheel, mosh uses ssh for authentication. So you need not create new key pairs or users and the like. So every security advantage that ssh has is also with mosh. It also implies that you should have a working SSH setup both in the local and remote system.

Install mosh on Ubuntu and other Linux distribution

If you don’t have ssh installed and configured, please learn the basics of SSH and enable SSH on Ubuntu or whatever Linux you are using.

After ensuring you have a working SSH setup, installing mosh is a straight forward task. The package you need to install is the same in both local and remote systems.

To install mosh in Ubuntu or Debian based Linux distributions, you may use the following commands, one by one:

sudo apt update
sudo apt install mosh

Mosh is available in all major Linux distributions. Use the package manager of your distribution to install it.

After you are done installing mosh, everything is ready on the client/local side. On the remote side (i.e. server), you have to do a simple configuration.

Configuring mosh on remote server

Remember the part that I said mosh uses UDP? By default, mosh uses UDP ports 60000-61000. So if you have a firewall on the remote system, you must rewrite the firewall rule for those ports as follows. ufw is the firewall manager.

sudo ufw allow 60000:61000/udp

Let’s connect through mosh

As of now, we have a working SSH, mosh that is installed in both systems, and a firewall rule that allows UDP traffic on ports 60K-61K at the remote server.

To use mosh, it as simple as below

mosh [email protected]

Yeah! I know. It is that simple just like ssh.

But what if I need to specify the ssh port other than the default port or to use ssh specific options. mosh has got you covered. You just need to pass the “ssh option [argument]” to the option –ssh.

For example, I need to connect to ssh running at port 2222 on the remote server. It can be done as follows.

mosh [email protected] --ssh="ssh -p 2222"

That’s it systems are connected. Now for realizing the power of mosh, try turning your network connection off and on again. You can see mosh is still working. How cool is that!

I hope you liked this tutorial on Mosh. Ever since I discovered Mosh, I have stopped using ssh for connecting to remote servers. How about you? Did you like Mosh or do you still prefer SSH? Do share your views.

seeni