How to gain persistent SSH connections with the help of Mosh

Estimated read time 4 min read

Jack Wallen shows you how to keep an SSH connection alive, so you can work uninterrupted.

Secure ssh on linux servers.
Image: pixeltrap/Adobe Stock

How many times have you been taking care of some critical admin work, only to have your Secure Shell connection broken? It happens for all sorts of reasons and when it does, bad things can happen. I’ve had it happen in the middle of a kernel compilation and had no idea where the process was when I finally reconnected. Although that situation worked itself out, there are plenty of instances where it might not.

SEE: 40+ open source and Linux terms you need to know (TechRepublic Premium)

Because of that, having a persistent SSH connection can be absolutely crucial. With a tool like Mosh (Mobile Shell), you gain roaming and intermittent connectivity, intelligent echo, and line editing of keystrokes. For anyone who requires a reliable SSH connection, Mosh should be at the top of your list of tools. With this tool in place, you can even switch IP addresses and remain connected. Mosh works with the help of UDP and a special synchronization protocol.

Let me show you how to install and use Mosh.

What you’ll need to create persistent SSH connections with Mosh

Mosh is installed on both the server and client, so you’ll need at least two machines to install it on. Mosh can be installed on Linux, macOS, Windows, Chrome/Chrome OS, Android and iOS. I’m going to demonstrate with Pop!_OS Linux as the client and Ubuntu Server 22.04 as the server. Beyond the installation, using Mosh is the same, regardless of the operating system.

With that said, let’s get to work.

How to install Mosh

Remember, you must install Mosh on both desktop and server, otherwise, it won’t work. So, log into your desktop, open a terminal window, and issue the command:

sudo apt-get install mosh -y

When that completes, do the same thing on your server to install the app.

Once Mosh is installed, you won’t find a graphical user interface app in your desktop menu. Instead, Mosh is a command that is run, almost exactly like SSH. Let me show you.

How to use Mosh

So, we have Mosh installed on both machines. Let’s make that persistent connection. On your desktop, go back to the terminal and issue the command:


In this command, USER is the remote username, and SERVER is the IP address of the server.

The connection is made exactly like it is with SSH. In fact, the only difference you will notice is the use of mosh instead of ssh.

If you’re using a firewall, you might have to open up the 60000:61000 ports. For example, with ufw, the commands for that would be:

sudo ufw allow 60000
sudo ufw allow 61000

If you’re using iptables, the commands would be:

sudo firewall-cmd --zone=public --add-port=60000/udp
sudo firewall-cmd --zone=public --add-port=61000/udp

With firewall-cmd you must also reload the firewall with:

sudo firewall-cmd --reload

Mosh does everything in the background by transferring the binary stream of input and output between the two computers. It achieves this by synchronizing keystrokes and screens, such that if the connection is interrupted it can be easily resumed because both client and server remain in sync. And because Mosh only needs to buffer the keystrokes and the latest frame or screen, it is capable of “pausing” until the two machines are reconnected.

And that is all there is to using Mosh. Enjoy those persistent SSH connections.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Source link

You May Also Like

More From Author