Skip to content

Instantly share code, notes, and snippets.

@brenr
Last active May 8, 2025 06:23
Show Gist options
  • Save brenr/d85592e4d2a66120d12e0ad63cc9b9a2 to your computer and use it in GitHub Desktop.
Save brenr/d85592e4d2a66120d12e0ad63cc9b9a2 to your computer and use it in GitHub Desktop.
OpenWRT Smartphone Tethering

OpenWRT Router Pre-configured for Smartphone Tethering

In this guide, I'll be using:

  • iPhone 12 Pro on iOS 15.1.1 as my tethering device
  • Raspberry Pi 4 with OpenWRT version 21.02.1
  • 64GB Sandisk micro SD card

The goal with this guide is to create a pre-configured OpenWRT router that:

  • Is plug and play to provide out-of-the-box smartphone tethering
  • Supports both iPhone and Android (comment below if you are having any issues).

Building the firmware

Trust me, this is extremely simple. Since the goal is zero configuration, we'll build the OpenWRT image to be flashed onto either the router, or SD card for the Raspberry Pi 4 in my case.

  1. Go to the image builder tool and search for the model of your router. In my case, I'll choose Raspberry Pi 4B/400/4CM (64bit).

  2. Below the search box, expand the Customize section.

  3. In the Custom package selection text box, you'll see a space-separated list of packages that is required by default for your OpenWRT router to work. Add a space at the end then add the following:

    kmod-usb-net-rndis kmod-nls-base kmod-usb-core kmod-usb-net kmod-usb-net-cdc-ether opkg kmod-usb2 kmod-usb3 kmod-usb-storage kmod-usb-storage-uas usbutils kmod-usb-net-ipheth usbmuxd libimobiledevice usbutils hub-ctrl rsync xxd vim iptables-mod-ipopt kmod-ipt-ipopt
    
  4. In the next section, Custom script to run on first boot (optional), paste the following. Note: you may need to replace eth1 with usb0 in this script, depending on your router - in my case, it's eth1.

    # Patch ipheth for iOS 14+
    cd /lib/modules/$(uname -r)
    cp -ia ipheth.ko ipheth.ko.orig
    xxd ipheth.ko.orig | sed 's/05EC/05EA/g' | xxd -r > ipheth.ko
    rmmod ipheth; modprobe ipheth
    
    # Call usbmuxd
    usbmuxd -v
    
    # Add usbmuxd to autostart
    sed -i -e "\$i usbmuxd" /etc/rc.local
    
    # Enable tethering
    uci add_list dhcp.wan.ra_flags='none'
    uci set network.wan=interface
    uci set network.wan.proto='dhcp'
    uci set network.wan.device='eth1'
    uci set network.wan.ttl='65'
    uci commit network
    /etc/init.d/network restart
    
    # Save connectivity checking script
    cat << "EOF" > /root/wan-watchdog.sh
    #!/bin/sh
    
    # Fetch WAN gateway
    . /lib/functions/network.sh
    network_flush_cache
    network_find_wan NET_IF
    network_get_gateway NET_GW "${NET_IF}"
    
    # Check WAN connectivity
    TRIES="0"
    while [ "${TRIES}" -lt 5 ]
    do
        if ping -c 1 -w 3 "${NET_GW}" &> /dev/null
        then exit 0
        else let TRIES++
        fi
    done
    
    # Restart network
    /etc/init.d/network stop
    hub-ctrl -h 0 -P 1 -p 0
    sleep 1
    hub-ctrl -h 0 -P 1 -p 1
    /etc/init.d/network start
    EOF
    chmod +x /root/wan-watchdog.sh
    
    # Add cron job
    cat << "EOF" >> /etc/crontabs/root
    * * * * * /root/wan-watchdog.sh
    EOF
    
    # Save watchdog script
    mkdir -p /etc/lockdown
    cat << "EOF" > /etc/lockdown/watchdog.sh
    #!/bin/sh
    # A small script to make life with iPhone tethering less cumbersome on OpenWrt
    # Petr Vyskocil, Apr 2020
    # Public domain
    
    # After you successfully allow iPhone tethering, copy files with name like
    # /var/lib/lockdown/12345678-9ABCDEF012345678.plist to /etc/lockdown/locks.
    # That way, you won't have to set up trust again after router reboots.
    if [ -e /etc/lockdown/locks ]
    then
        mkdir -p /var/lib/lockdown
        cp -f /etc/lockdown/locks/* /var/lib/lockdown/
    fi
    
    # lockdown records restored, now we can launch usbmuxd. Don't launch it sooner!
    usbmuxd
    
    # We are up and running now. But unfortunately if your carrier signal is weak, iPhone will
    # drop connection from time to time and you'd have to unplug and replug USB cable to start tethering
    # again. Script below automates that activity.
    
    # First wait a bit - we just brought the interface up by usbmuxd
    sleep 20
    
    # If we see iPhone ethernet interface, try to ping iPhone router's address (172.20.10.1).
    # When the ping is unsuccessful, rebind iPhone ethernet USB driver and wait for things to settle down
    while :
    do
        for i in /sys/bus/usb/drivers/ipheth/*:*
        do
            test -e "${i}" || continue
            ping -w 3 172.20.10.1 &> /dev/null
            if [ "${?}" -ne 0 ]; then
                echo "${i##*/}" > "${i%/*}"/unbind
                echo "${i##*/}" > "${i%/*}"/bind
                sleep 20
            fi
        done
        sleep 1
    done
    EOF
    chmod +x /etc/lockdown/watchdog.sh
    
    # Add watchdog script to autostart
    sed -i -e "\$i (/etc/lockdown/watchdog.sh) &" /etc/rc.local
  5. Now click the Request Build button. Now go make some coffee (yes it's required otherwise it won't work).

  6. Once finished, you should see four possible image downloads at the bottom. If you have a router that's currently running and you want to flash over it, you'll want the sysupgrade (ext4) however in my case, it'll be a factory flash, so of course I'll download the factory (ext4) image.

  7. After it's downloaded, go ahead and uncompress it. (Archive Utility on macOS, 7-Zip/Winrar on Windows, etc).

Writing the firmware

Option 1) Using a router with an OS already on it

Find the setting to upgrade the firmware and in the file chooser, choose the .img file you just uncompressed. Upgrade the firmware and sit back.

Option 2) Writing directly to an SD card

Use Balena Etcher (on macOS) to write the .img file to the SD card or if you're on Windows, you can use something like Rufus. Once finished, slot the micro SD card into the Raspberry Pi 4 and boot it up.

Enable tethering on smartphone

  1. Plug your smartphone into the USB port of the router or Raspberry Pi and enable tethering. On an iPhone, you'll be prompted to Trust the first time.

Recommendations

  • Set a strong password on the router's web interface
  • Disable SSH via the OpenWRT web interface if you have no need for it
  • Set a strong password for the root user. Preferably authenticate with keypair.

Credits

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