Installing MOSH Shell on Ubuntu 20.04

Mosh is a fantastic alternative for standard SSH. While SSH uses the TCP protocol, it uses a session bound to IP address, so it is not suitable for roaming. MOSH, on the other hand uses the UDP protocol and app level sessions. This lends itself to roaming.

Why Does This Matter?

If you are not on a great WiFI connection, or you sometimes switch between different networks, or your laptop goes to sleep, you will lose your connection on a standard SSH connection. Not with MOSH. In addition, Mosh works significantly batter on connections with higher latency.

Some Benefits to Mosh:

  • Mosh stays connected even if your IP changes.
  • Mosh resumes connection with your server when your network comes back online after losing your internet connection or when you put your system to sleep.
  • Mosh uses the same user credentials as SSH.
  • Mosh doesn’t fill up network buffers like SSH, so Control-C will always work to halt processes.
  • Mosh works beatifully on low-bandwidth and intermittent connections.
  • Mosh allows you type at times when SSH would still be waiting for a command to finish or connection to be established.
  • Mosh has a mechanim to handle packet loss

Mosh on the Client Side

Before using it on the client side, you need to insall it on your local machine: (free).

Personally, I use a terminal app called Termius, with I find to be the best out there by far. It supports both SSH and Mosh (plus telnet & SFTP), plus all sorts of other fantastic features you will not find in a standard terminal. It also boasts a really attractive UI to work in. There are versions for Mac, Windowsm Linux, iOS and Android, and the free version is excellent. If you decide to opt for the paid version (well worth it in my opinion), you can get auto syncronization between your devices, SFTP, and Snippets (I could not live with out them). There is also a more advanced "Teams" version for sharing among team members. You can check it out here: Termius.

Setting Up Mosh on Ubuntu 20.014 for Incomming Connections

Let's start by making sure everything is current:

$ sudo apt-get update && sudo apt-get upgrade -y

Next install Mosh:

$ sudo apt install mosh -y

Configure the Firewall to allow Mosh. In this case we are using ufw, so adjust as needed if you are using a different firewall:

$ sudo ufw allow 60000:61000/udp 

So, for example, if you are using iptables:

$ sudo iptables -I INPUT 1 -p udp --dport 60000:61000 -j ACCEPT

That's it! You can now connect using Mosh.

How to Connect to Your Server with Mosh

If you are using Termius, you will see there is a Mosh option for every host. Just turn it on, and you qre good to go. If you downloaded a Mosh client and installed it, rather than conneting using the ssh command, you use mosh like this using the client:

$ mosh username@x.x.x.x

If you changed your ssh server to a different port (If not, click here to see how), you will connect like this:

mosh username@x.x.x.x --ssh="ssh -p 5757"

(assuming you chose 5757 for your ssh port on your server).

And now we are done! Enjoy!

Blog Comments powered by Disqus.