Skip to content

Instantly share code, notes, and snippets.

@greenido
Created January 6, 2025 22:53
Show Gist options
  • Save greenido/f236e026c139f54073f454be966e66c6 to your computer and use it in GitHub Desktop.
Save greenido/f236e026c139f54073f454be966e66c6 to your computer and use it in GitHub Desktop.
Uses macOS's built-in Screen Sharing instead of installing a VNC server
#!/bin/bash
# Check if running on macOS
if [ "$(uname)" != "Darwin" ]; then
echo "This script is for macOS only"
exit 1
fi
# Check if running as root
if [ "$EUID" -eq 0 ]; then
echo "Please do not run as root/sudo"
exit 1
fi
# Enable Remote Management
echo "Enabling Remote Management..."
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart \
-activate -configure -access -on \
-restart -agent -privs -all
# Enable VNC with password
echo "Setting up VNC password..."
read -s -p "Enter VNC password: " VNC_PASSWORD
echo
read -s -p "Confirm VNC password: " VNC_PASSWORD_CONFIRM
echo
if [ "$VNC_PASSWORD" != "$VNC_PASSWORD_CONFIRM" ]; then
echo "Passwords do not match!"
exit 1
fi
# Set VNC password
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart \
-configure -clientopts -setvnclegacy -vnclegacy yes \
-clientopts -setvncpw -vncpw "$VNC_PASSWORD"
# Configure firewall if it's enabled
if [ "$(sudo defaults read /Library/Preferences/com.apple.alf globalstate)" != "0" ]; then
echo "Configuring firewall..."
# Allow screen sharing through firewall
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add /System/Library/CoreServices/RemoteManagement/ARDAgent.app
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --unblock /System/Library/CoreServices/RemoteManagement/ARDAgent.app
fi
# Enable Screen Sharing
echo "Enabling Screen Sharing..."
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.screensharing.plist
sudo defaults write /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing -dict Disabled -bool false
# Get IP address
IP_ADDRESS=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}')
echo "VNC server setup complete!"
echo "Your Mac's IP address is: $IP_ADDRESS"
echo "To connect from another machine:"
echo "1. Use a VNC viewer and connect to: vnc://$IP_ADDRESS"
echo "2. Enter the password you just set"
echo ""
echo "Note: You can also enable/disable Screen Sharing through:"
echo "System Preferences → Sharing → Screen Sharing"
echo ""
echo "For enhanced security, consider using SSH tunneling:"
echo "ssh -L 5900:localhost:5900 username@$IP_ADDRESS"
echo "Then connect your VNC viewer to: localhost:5900"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment