Mosh can be best described as an alternative to ssh.
It is very useful when the network is pretty weak or the network has high latency.
Even though I’ve heard and used mosh a little bit before, only recently I started using it all the time.
it was due to me buying a VPS from OVH (the stocks were pretty limited and only available at certain times) at Europe or something and when I tried to do some serious work on that VPS while connected to ssh, the latency became a bottleneck.
In ssh, when we type a character, that character travels all the way to the vps and after the confirmation from the vps reaches our device, we can actually see the character in our system.
because of that, using ssh in unreliable connections is very problematic.
but mosh on the other hand, does some little things to make our life easier.
ie, if the network is too slow or the latency is too much, instead of waiting the confirmation from the server, mosh shows them instantly on our terminal. I’ve learnt that the official term for this is ‘predictive echo’ or something.
Apart from this, another feature is, it NEVER disconnects. ie, even if we change networks, whether its switching between different wifi networks or switching between wifi and mobile networks, the mosh session won’t be disconnected. It also survives even if we put our device to sleep :)
if mosh is unable to connect to the server for some time, it will show a friendly message saying mosh: Last contact <n> seconds ago.
and if we reconnect it, it’ll instantly show the latest view of the terminal.
Setup
first, we need to install the mosh
package on both server and client. for ubuntu/debian family etc, it’s available in the apt repo. we can install it by running
apt update && apt install mosh -y
after that, we need to install it on our client. for macos it’s available in the homebrew.
brew install mosh
for termux, we can install it from pkg
pkg update && pkg upgrade && pkg install mosh
After installing on both client and server, we can simply connect to our server by changing to mosh instead of ssh
mosh root@<my-ip-address>
ie, we only need to change the word ssh
to mosh
in our commands