Getting Started with Mosh

I find I’ve mostly been managing my Raspberry Pi’s via the Blink app on my iPhone1. It is an open source and feature rich SSH/Mosh client. I had vaguely heard of Mosh (the Mobile Shell) but never felt a need to look further. After a dist-upgrade and long apt update && apt upgrade -y, config cycles I would get the boot from SSH after my phone locked and found that I would do more steps to disable (and then re-enable) sleep until I was done.

So what is Mosh?

Remote terminal application that allows roaming, supports intermittent connectivity, and provides intelligent local echo and line editing of user keystrokes.

Mosh is a replacement for interactive SSH terminals. It’s more robust and responsive, especially over Wi-Fi, cellular, and long-distance links.

The key features are:

  • Mosh automatically roams as you move between Internet connections. Use Wi-Fi on the train, Ethernet in a hotel, and LTE on a beach: you’ll stay logged in.
  • With Mosh, you can put your laptop to sleep and wake it up later, keeping your connection intact. If your Internet connection drops, Mosh will warn you — but the connection resumes when network service comes back.
  • No lag like with SSH which waits for a server reply before showing your typing.

I’ve basically started to install Mosh on any server I manage… hit the jump for a quick setup guide pertaining to Mac usage!

Installing Mosh

Mosh can be installed via Homebrew, Ports or a pkg via there site (I’ve even made an AutoPkg install recipe via my recipe repo). Once installed we need to make a few changes on our Mac. Y

Prepare your Mac

You’ll most likely receive some UTF-8 errors when attempting to connect, to fix this you can run the following (or even better, add to your ~/.bash_profile or ~/.zshrc):

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

Prepare your Linux Server

Mosh operateMosh will log the user in via SSH, then start a connection on a UDP port between 60000 and 61000 so you may need to adjust your firewall settings.

via ufw

sudo ufw allow 60000:61000/udp

via iptables (not persistent, you may need to install iptables-persistent)

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

Install Mosh on the Server / Pi

sudo apt install mosh

Connecting to your Server / Pi from the Mac

mosh user@server

If all goes well, you should see the following:

You may need to verify where your mosh-server binary is located on the target machine and adjust the --server setting accordingly. For instance, if you installed Mosh via MacPorts on a Mac mini, mosh-server will most likely be located under /opt.

mosh user@server --server=/opt/local/bin/mosh-server

Fin!

Now, via Terminal or Blink, when you connect via Mosh, there is no need to keep the display awake, just unlock and your session will come back.

  1. Blink is also great if you work a lot with an iPad Pro! []

Leave a Reply