Mosh Server as an SSH Alternative.
Indeed, even with a robust internet connection, SSH can sometimes prove unreliable. This is due to the fact that even brief interruptions in the connection between the client and server can lead to complete stalls, forcing you to terminate and re-establish the connection.
Experiencing this frustration once is bearable, but enduring it repeatedly becomes increasingly irritating and wastes precious time. Therefore, if you frequently work with unreliable mobile connections or high-latency satellite connections, for instance, Mosh is the perfect solution for you.
🎧
What Exactly is Mosh?
Mosh, short for "mobile shell" is a remote terminal application that allows for more robust connections, especially in situations with unreliable network connections. Unlike traditional SSH (Secure Shell), which relies heavily on the stability of the network connection, Mosh is designed to maintain connections even when the network is intermittent or experiencing high latency. It allows you to access any server on the command-line that you have SSH access to.
And yes; MOSH uses SSH for authentication (so you will need to have OpenSSH server installed). Mosh achieves this by using UDP (User Datagram Protocol) rather than TCP (Transmission Control Protocol), which allows it to better handle packet loss and latency spikes. Additionally, Mosh provides features like predictive typing and local echo, which can improve the user experience in situations with high latency.
Vs - vs
Unlike SSH, MOSH is designed to excel in unstable internet environments, ensuring persistent connections despite periods of inactivity. Mosh effortlessly adapts as you switch between internet connections. Whether you're connected via Wi-Fi on a train, Ethernet in a hotel, or LTE on a beach, your session remains uninterrupted furthermore you can put your laptop to sleep and wake it up later, all while maintaining your connection intact; most network programs lose their connections.
How Secure is MOSH?
Mosh establishes the initial connection over SSH (Secure Shell), ensuring that the authentication is as secure as SSH itself. It employs AES-128 (Advanced Encryption Standard) encryption for transmitting traffic over UDP (User Datagram Protocol), thereby preventing any potential sniffing of your data.
How does it Work?
Mosh uses what it calls the State Synchronization Protocol (SSP), which is responsible for synchronizing two snapshots of the current screen state, one on the server, and another on the client. This protocol, based on UDP, is intended to present the client with the latest server-side state. It's crafted to utilize datagrams and heartbeats (a heartbeat is a specific type of communication packet used to maintain or verify the liveliness of a connection between two devices or systems) to sustain the connection, even during transitions between Wi-Fi hotspots or across different IP addresses.
More Responsive and Faster
Mosh also notably enhances latency, thereby improving real-world productivity. With SSP synchronizing two states instead of relying on back-and-forth data transfer, Mosh can implement changes to the user interface much more rapidly. In a study conducted, the median keystroke response time for SSH was 503ms, while for Mosh, it was only 5ms.
Install from the apt package manager
Just like SSH, Mosh utilizes one version of its application on the remote server and another on the local client.
Let's install it.
#Ubuntu/Debian
$ sudo apt install mosh
#macOS
$ brew install mosh
Building from Source
If you still can’t find ways to install Mosh on your distribution, you can still get the source code at https://mosh.org/mosh-1.4.0.tar.gz.
(SHA-256: 872e4b134e5df29c8933dff12350785054d2fd2839b5ae6b5587b14db1465ddd)
$ wget https://mosh.org/mosh-1.4.0.tar.gz
$ tar -xvf mosh-1.4.0.tar.gz
$ cd mosh-1.4.0
$ ./configure
$ make
make install
Compiling from Git
You can also get it from GitHub.
$ git clone https://github.com/mobile-shell/mosh
$ cd mosh
$ ./autogen.sh
$ ./configure
$ make
make install
Firewall Configuration
Mosh operates by exchanging UDP datagrams between the client and server. By default, it uses port numbers between 60000 and 61000, although users can select a specific port using the -p
flag. Opening 1000 ports isn’t really good security practice. However, if you have a small number of concurrent connections, then a smaller range of ports can be opened. (e.g 60000:60025)
On a server using UFW (Uncomplicated Firewall) such as Ubuntu Server, the command to open those ports would be:
$ sudo ufw allow 60000:60025/udp
How to use MOSH
Let's consider the scenario where the default SSH port, 22, remains unchanged.
We will run the following command:
$ mosh [REMOTE_USER]@[REMOTE-SERVER_IP]
Now, let's consider the scenario where the default SSH port, 22, has been changed to port 1500.
We will run the following command:
$ mosh [REMOTE_USER]@[REMOTE-SERVER_IP] --ssh="ssh -p 1500"
We can also select a more specific UDP port combined with an SSH port like so:
$ mosh -p 60012 [REMOTE_USER]@[REMOTE-SERVER_IP] --ssh="ssh -p 1500"
Where 60012 is the UDP port number from our opened ports range. (e.g 60000:60025)
ISSUE
You might also come across the following error message.
"/usr/local/bin/mosh – did not find mosh server startup message."
To address this issue, you'll want to locate the directory where the mosh-server binary is installed. Then, ensure to pass this path to the server option in your mosh command; as such:
- run:
$ which mosh-server
Output
/usr/local/bin/mosh-server
- then:
$ mosh --server=/usr/local/bin/mosh-server [REMOTE_USER]@[REMOTE-SERVER_IP] --ssh="ssh -p xxxx"
As we've just seen; Mosh proves to be an invaluable tool for remote system administration, particularly in situations where network conditions are less than ideal. This tutorial marks just the beginning of our quest for deeper understanding. Thank you all once again, and best wishes on your learning journey ahead!