Skip to content

Instantly share code, notes, and snippets.

@rwuwon
Last active August 22, 2022 19:21
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rwuwon/8f34c39cff9584d123ae3c0d222fa1bd to your computer and use it in GitHub Desktop.
Save rwuwon/8f34c39cff9584d123ae3c0d222fa1bd to your computer and use it in GitHub Desktop.
Connect to a Google Cloud Compute Engine using Mosh (mobile shell)

Connect to a Google Cloud Compute Engine using Mosh (mobile shell)

Mosh is a great way to overcome lag and flakey connection issues when accessing remote terminals over SSH: https://mosh.org/

Unfortunately, there's still not a lot of clear documentation for "idiots" because a lot of the existing guides assume a) "it just works", or b) you're already skilled enough to set everything up from scratch (assumed knowledge/use cases).

One of the problems I ran into for quite a long time yesterday was getting the darn thing to connect to my Google Cloud instance. I kept running into the mosh: Nothing received from server on UDP port 60001. [To quit: Ctrl-^ .] error (by the way - to quit, you have to press . after Ctrl-^ - it's not just Ctrl-^, there is a dot there too!)

FAQs and the like made vague spartan references to NATs, firewalls, iptables and the like and I tried out various commands and configurations on the server and local end (was the NAT on my side the problem? I didn't know!) It all felt like a big drama at the time - what were other people not telling me? What was I not getting?

With a clearer head today, I for some reason decided to dig around to compare cloud pricing between Amazon, Google and Microsoft. I ended up back at my console.cloud.google.com page to try and figure out how much processing/data I was using and what do ya know; I found a link I hadn't previously seen nor cared to notice, next to the Internal IP details (nic0). Hah, it listed firewall rules!

Another click after that (it still wasn't immediately clear how I could create a new rule for mosh), and I was able to find the button to create a new firewall rule.

In short, here's what I did for my specific mosh-gcloud/CentOS instance connection conundrum

  1. Use https://console.cloud.google.com/networking/firewalls/list to create a new a new rule for mosh. Create a new rule for mosh using udp:60000-61000. You shouldn't even need to touch iptables, firewall-cmd or the like within your remote terminal.
  2. ssh to your instance, run mosh-server.
  3. Back on your local terminal - if your normal ssh command is ssh username@address, then just type mosh username@address
  4. That's it. No more, no less - at least for this type of basic set-up.

I believe Amazon EC2 might have a similar process but I didn't get around to looking at those guides. I probably should have done that first before trying to bash around in tmux panes and windows and following iptables red herrings.

Additional tips

If you're using --ssh=COMMAND, do this: mosh --ssh 'ssh -p 11111' localhost

Rather than: mosh localhost --ssh 'ssh -p 11111'

Or maybe it was the other way around? Better yet, perhaps try both (along with pkill mosh-server when you ssh back in. For some reason, both will end up working after the first connection. I'm still trying to figure out why it seems to only work intermittently. It's quite bizarre - turning the firewall off and on, adding a rule, rebooting seemed to be the most reliable way to get it going. It's probably the reboot that is doing the trick to avoid that all-too-familiar mosh: Nothing received from server on UDP port 60001. [To quit: Ctrl-^ .] message.

Replace localhost with username@domain if you're not playing around between your machine and a VirtualBox instance.

On the topic of VirtualBox

  1. Go to Devices > Network > Network Settings...
  2. Under Network/Adaptor 1/NAT, click Advanced and then Port Forwarding
  3. Rule 1: TCP / Host Port 11111 (or anything you'd like that won't conflict) and Guest Port 22
  4. Rule 2: UDP / 60000 / 60000
  5. Rule 3, etc: UDP / 60001 / 60001

Leave the Host IP and Guest IP columns blank. Make sure the 60xxxx ports are the same in both Host and Guest Port columns! For the purposes of testing, remember to either configure the firewall or disable them (sudo ufw disable or sudo systemctl disable firewalld and reboot) and keep passwords short. If you're testing in the cloud, the firewall settings are probably best done via your web browser.

I've tested this with CentOS 7 Minimal as well as Ubuntu 14.04.5 LTS, Trusty Tahr. I specifically went for an older Ubuntu LTS as an experiment, compare and troubleshoot unicode emojis with CentOS 7...

Not working yet

  • Emoji such as 🤔
  • Although these work: ⚽ and ⚠️ - hmm.
  • glibc 2.17 on CentOS 7.5 appears to be the culprit - I'm now considering a migration to Debian 9 Stable or Ubuntu Minimal 18.04 LTS...

Update: I'm going to go ahead with a migration to Debian Stable, which should offer a good enough mix of newness and stability. While Debian Stable doesn't have the latest glibc (2.27) at this stage, most emoji I see work with the existing version (2.24). When testing freezes, I should be able to safely update to that and therefore be pretty much as current as Ubuntu LTS (if not more current). Some might quip that Debian Stable is very old, but its update cycle is significantly faster than CentOS!

Update 2: Upgrade from CentOS 7. Either to CentOS 8, or Debian Stable. That'll fix emoji issues.

@dominicusin
Copy link

gcloud compute firewall-rules create mosh --allow udp:60000-61000

@marcwjj
Copy link

marcwjj commented Nov 27, 2018

Thanks for your post! it helped me to setup my mosh ssh client to cloud VM instance correctly.

Actually your post motivated me to write a more step-by-step guide for beginners how to do the entire setup, in case this is helpful for someone:
https://gist.github.com/marcwjj/d86bc137a302b50d8c0f1b5af5fd19d5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment