A robust and user-friendly Bash script to reliably connect your Mac to an iPhone Personal Hotspot — even when macOS fails to detect or join it automatically.
Connecting to an iPhone Personal Hotspot on macOS has long been unreliable. Many users have reported persistent issues, such as those described in this Reddit thread.
After struggling with this problem myself, I discovered that restarting the network interface and disabling IPv6 consistently resolved the issue. This script automates that process — making hotspot connections predictable, repeatable, and far more reliable.
- Enables Wi-Fi if it's off
- Disables IPv6 for improved hotspot stability
- Connects to a user-specified or environment-configured hotspot
- Displays a live connection timer
- Re-enables IPv6 on script exit
- Graceful shutdown with
Ctrl+C
- macOS (tested on Monterey and later)
- Admin privileges (for interface restart and IPv6 config)
Before running the script, you must make your iPhone’s Personal Hotspot discoverable.
🔓 Important: Even if "Allow Others to Join" is enabled, iPhones often do not advertise the hotspot over Wi-Fi unless explicitly made discoverable. You can do this in two ways:
-
Settings method:
Open Settings → Personal Hotspot and keep the screen open. This reliably makes the hotspot visible to nearby devices. -
Control Center method:
Swipe down from the top right of the screen to open Control Center, long-press the tile with Wi-Fi/Bluetooth, and tap the Personal Hotspot icon to turn it on. This also makes the hotspot discoverable for a short time.
💡 Tip: If the script fails to connect immediately, double-check that your hotspot is visible using one of the methods above.
Once your hotspot is discoverable on your iPhone, run the script using either method below:
Define SSID and password through environment variables.
export HOTSPOT_NAME='Your Hotspot SSID'
export HOTSPOT_PASSWORD='YourHotSpotPassword'
./hotspot_connect.shYou can add these export commands to your ~/.bash_profile script to define them permanently.
Or, provide the SSID directly on the command line:
./hotspot_connect.sh --ssid "Your Hotspot SSID"The script will ask for your SSID password in order to connect.
./hotspot_connect.sh --helpBy default, your iPhone’s hotspot SSID is the same as the device name, typically following this format:
"<YourName>’s iPhone", e.g., John’s iPhone.
You can find your current hotspot name by:
- Opening the Wi-Fi menu from the top-right menu bar on your Mac — the hotspot name will appear there when your iPhone is discoverable.
- Or by going to Settings → General → About → Name on your iPhone to view or change it.
⚠️ Note: macOS and command-line tools may misinterpret certain characters (like the curly apostrophe’).
For best results when using this script, consider renaming your iPhone to something simpler, likeJohnsPhoneorMyiPhone— avoiding spaces, quotes, and punctuation.
- Detects your Mac’s Wi-Fi interface
- Turns on Wi-Fi if disabled
- Restarts the interface, flushes DNS, renews DHCP
- Disables IPv6 (this is key for many hotspot issues)
- Attempts to connect to the specified hotspot
- Retries if the hotspot is not yet discoverable
- Shows a live timer indicating how long you've been connected
- On
Ctrl+C, re-enables IPv6 and exits cleanly
The script:
- Never stores your hotspot password
- Uses secure input (
read -rsp) if you don’t want to export your password in your shell
The script traps SIGINT and SIGTERM to:
- Stop the connection timer
- Re-enable IPv6
- Exit cleanly with a success message
🌐 Network Interface Detected: en0
⚡ Wi-Fi is OFF. Turning it ON...
🔄 Restarting network interface en0...
🔐 Admin privileges are active (no password will be prompted).
🧹 Flushing DNS cache...
🔄 Renewing DHCP lease...
🛑 Disabling IPv6 for better hotspot stability...
📡 Connecting to hotspot: JohnsPhone...
⏳ Waiting for Wi-Fi connection to stabilize...
✅ Detected WiFi connection. IP Address: 175.32.20.3
⏱ Connected for: 00:03:45
MIT License — feel free to use, modify, and share.