Skip to content

Instantly share code, notes, and snippets.

@ericswpark
Last active May 6, 2023 03:57
Show Gist options
  • Save ericswpark/357371e6fc0d23321c1c4f947be33674 to your computer and use it in GitHub Desktop.
Save ericswpark/357371e6fc0d23321c1c4f947be33674 to your computer and use it in GitHub Desktop.
Fix mosh server on macOS
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.example.my_script</string>
<key>ProgramArguments</key>
<array>
<string>/Users/ericswpark/fix_mosh_server.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>root</string>
</dict>
</plist>
#!/bin/bash
# Thanks to @anhdle14 for the original script
# Find it here: https://github.com/mobile-shell/mosh/issues/1074#issuecomment-978832126
#!/bin/bash
fix_mosh_server() {
local fw='/usr/libexec/ApplicationFirewall/socketfilterfw'
local mosh_sym
mosh_sym="$(which mosh-server)"
local mosh_abs
mosh_abs="$(greadlink -f "$mosh_sym")"
# Turn off firewall temporarily
sudo "$fw" --setglobalstate off
# Add symlinked location to firewall
sudo "$fw" --add "$mosh_sym"
sudo "$fw" --unblockapp "$mosh_sym"
# Add symlinked location to firewall
sudo "$fw" --add "$mosh_abs"
sudo "$fw" --unblockapp "$mosh_abs"
# Re-enable firewall
sudo "$fw" --setglobalstate on
}
fix_mosh_server

Fix mosh server on macOS

Why?

The macOS firewall has a bug where it will block connections that you explicitly allowed for a given program, if said program is added as a symlink.

If you are getting Nothing received from port XXXXX when connecting to your Mac via mosh, this solution is for you!

Setup

The fix_mosh_server.sh script goes in your user directory.

The fix_mosh_server.plist file goes in /Library/LaunchDaemons/. Once created, give it the proper permissions:

sudo chown root:wheel /Library/LaunchDaemons/fix_mosh_server.plist
sudo chmod 644 /Library/LaunchDaemons/fix_mosh_server.plist

Then load it:

sudo launchctl load /Library/LaunchDaemons/fix_mosh_server.plist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment