Introduction

Mosh is a replacement for SSH's terminal feature. SSH has been the mostly used and one of the best tools for system and network admins for decades, replacing telnet , ftp, rlogin and others since 1995. https://mosh.org/

So why we are talking about replacing it?

SSH uses TCP protocol underneath, as this protocol uses a session and is bound to IP address, SSH is not suitable for roaming. This is where Mosh comes in to the rescue. By using UDP protocol and application level sessions, Mosh is able to provide roaming for us. This is a great enhancement for the admin working on sketchy wifi connections or switching between different networks or just putting their laptop to sleep and still being able to preserve their session to the server. This is only one of the reasons, Mosh is also very successful on high latency connections. It's local feedback with predictive echo of the commands you type is another useful feature.

How Mosh Works Behind the Scenes

Mosh initially connects to the server via SSH, it starts up mosh-server. In this stage Mosh uses SSH to exchange the required keys before terminating the SSH session. After that user - using mosh-client- connects to the UDP port that mosh-server opened.

From security point of view Mosh uses AES-128 for encryption and every datagram is encrypted.

Prerequisites for Our Setup

Any recent Ubuntu Server would work with all the commands provided.

We are using an Ubuntu 20.04 LTS in this article.

Preparing Our Server

Let's prepare our server for our setup, we will update the local package index and upgrade any packages that are outdated.

$ apt-get update
Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Reading package lists... Done                        


$ apt-get -y upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
....
....

Installing Mosh

Let's install the required packages for our system. Following will install Mosh server and client using the mosh package which is available on default Ubuntu repositories.

$ apt install -y mosh
Reading package lists... Done
Building dependency tree       
Reading state information... Done
....
....

Configuring Firewall to Allow Required Ports

Mosh uses UDP protocol and uses port range from 60,000 to 61,000. Let's open this port range. You can restrict this to a specific host putting "from x.x.x.x" at the end of the command.

$ sudo ufw allow 60000:61000/udp                                                             
Rule added                                                                                                                  
Rule added (v6)                                                  

Verify the firewall rules:

$ sudo ufw status numbered                    
Status: active                                                               
                                                                             
     To                         Action      From                           
     --                         ------      ----                           
[ 1] 80                         ALLOW IN    Anywhere                       
[ 2] 22                         ALLOW IN    Anywhere                       
[ 3] 60000:61000/udp            ALLOW IN    Anywhere                       
[ 4] 80 (v6)                    ALLOW IN    Anywhere (v6)                  
[ 5] 22 (v6)                    ALLOW IN    Anywhere (v6)             
[ 6] 60000:61000/udp (v6)       ALLOW IN    Anywhere (v6)

Great, our server is setup, now next step is to install a Mosh client to our client machine.

Installing Mosh Client

Linux

See section "Installing Mosh" above. This installation command installs both Mosh server and client

Mac OS X

We can install mosh using varios methods, here we are using "brew" package manager.

$ brew install mosh
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
....
==> Downloading https://homebrew.bintray.com/bottles/protobuf-3.12.1.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/b0/b09bdcad4bde73a6d9f5675979da6f10b16b9620a26b5bc6a7639d03e3b7fccb?__gda__=
######################################################################## 100.0%
==> Downloading https://homebrew.bintray.com/bottles/mosh-1.3.2_11.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/1f/1f46edf8fbd83303ea4156530357207b6ad538a6abbbd5118f9c39e4898a4a19?__gda__=
######################################################################## 100.0%
==> Installing dependencies for mosh: protobuf
==> Installing mosh dependency: protobuf
==> Pouring protobuf-3.12.1.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/protobuf/3.12.1: 270 files, 19.9MB
==> Installing mosh
==> Pouring mosh-1.3.2_11.mojave.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
🍺  /usr/local/Cellar/mosh/1.3.2_11: 14 files, 838.8KB
==> Caveats
==> mosh
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
$ 

Windows

For Windows there is no direct executable that we know at the time of writing of this article however we can install Mosh from Chrome App Store, reference link below.

Testing Locale to make sure Mosh is compatible

Before proceeding further we need to talk about locale. Locale is important to Mosh both client and the server locale must be UTF-8. In some cases this is not the case so we might need to modify it.

If your client and server's locale is UTF-8, you can skip this step.

Testing Client for Locale

We can see our local locale via the "locale" command

$ locale
LANG=
LC_COLLATE="C"
LC_CTYPE="C"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=

Testing Server for Locale

We will use the same command but run it on the server side using SSH.

$ ssh [email protected] locale
LANG=C.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8

We can see that our client has problems but server configuration is compatible.

To fix the client problem we need to set the locale, we will set the following environment variables. You can add these export statements to your bash_profile / profile files to make this permanent.

$ export LC_ALL=en_US.UTF-8  
$ export LANG=en_US.UTF-8

$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8" 

Connecting to the Server with Mosh

Now we can connect to our server as usual, usually replacing ssh with mosh on the commands is sufficient to use it.

mosh [email protected]

SSH Options to provide to Mosh

Additional SSH options can be provided to Mosh via ''--ssh' flag.

mosh remotehost --ssh="ssh -p 2222"

Conclusion

We have explored the features and installation steps required for Mosh. Now you can easily use Mosh for your terminal. Now we are able to roam!

Please note that scroll feature of Mosh is not that great so it is recommended to use it with Screen or Tmux.

code
and
stuff