How to Enable Mosh in an Azure VM

in #linux3 years ago

item_13_istock_1137011043_scaled.png

Azure is a great service for hosting cloud services. One of the many things you can do is host a Virtual Machine in the cloud. Of course, you need to be able to connect to it with SSH to use it. Mosh is better than SSH, due to its extra features and enhancements, so it makes sense to use Mosh to connect to devices instead of SSH. You can change IPs, put your laptop to sleep, have high latency, and Mosh will survive, unlike SSH. By default, VMs have the SSH port open, but if you try to login with Mosh, you’ll get an error about port 60001. How do you fix this? It’s actually pretty easy.

First, login to the Azure Portal, and select the resource group that contains your Linux VM. Second, select your virtual machine. Then, select “Networking” under “Settings” in the sidebar. You’ll see the network traffic rules. By default, the only port open to the Internet is 22, for SSH. Mosh uses port 22 to authenticate with SSH, but it needs extra ports to work properly.

To enable Mosh, click “Add inbound port rule”. Set the source to “Any” since we want to be able to access it from the open Internet and within other Azure services. If you’re using a VPN to access it, set the source to that. Really, just copy whatever you have chosen for SSH. Set “source port ranges” to “*”. Set “destination” to “any”.

Now, we need to choose what ports to open. Mosh uses ports 60001-69999, but I’m never going to have more than 10 Mosh sessions open, so I’m going to use ports 60001-60010. Set “protocol” to “UDP”, “action” to “Allow”, and “priority” to the priority number you want (I think Mosh is less important than SSH, so I set it to 400) and set the name to “Mosh”. Click “Add” and you’re done. You can open the entire port range if you want... but are you really going to have 10,000 Mosh sessions open?

Once you're all done with that, you can connect to your VM using Mosh. Now you can access your VM wherever you go, without having to worry about SSH problems!

I'd recommend using Tmux in conjunction with Mosh, because Mosh doesn't provide scroll back, which is the main drawback of Mosh.

Cross posted from my blog.