Pi Zero W USB Ethernet Gadget with Asus Router Fix

It appears to be possible to setup a Pi Zero W running Pi-Hole in USB Ethernet Gadget mode and connect it to an Asus Router with USB ports (RT-AC68U) running Merlin firmware. Unknown if this procedure works with non Merlin firmware ASUS routers or with other manufacturer routers. It does involve quite a bit of editing/creating files on both the Pi Zero W and the Asus router firmware. These directions assume one has knowledge of Linux commands, and using SSH (Putty and or WinSCP) Standard disclaimer: Proceed at your own risk!!! It is entirely possible one could brick their router or Pi Zero W by editing the firmware/OS files.

Hardware Used:
Asus RT-AC68U
Merlin firmware version: 384.10_2
Pi Zero W with Raspbian Stretch Lite.
Single USB to microUSB cable

Note: Make sure Pi and router are up to date with latest updates.

Directions/Steps on the Pi Zero:

Note: I used an existing running microSD card running Stretch LIte and Pi-Hole. These directions assume you are using an existing running version of Pi Zero W with Pi-Hole. And that Pi Zero W is currently connected to the local network using a USB to Ethernet adapter. If starting from a fresh microSD card the steps should be similar for initial setup. Instead of running a reconfiguration of Pi-Hole one would install from scratch.

Note: I tried to note every step I used. So proceed at your own risk!!!.

On existing running Pi Zero W, SSH into it and run the following command:
Ifconfig eth0

Write down the MAC address used by eth0. It will be used later.

On the Pi Zero W, issue the following command:
sudo nano /boot/config.txt
Add the following line at the very end of the config.txt file.
dtoverlay=dwc2

On the existing Pi Zero W, run the following command:
sudo nano /boot/cmdline.txt
Add the following line at the very end of the single line after the rootwait text:
modules-load=dwc2,g_ether

Note: Make sure there is a space after the rootwait line and the modules text you paste/type in. Make sure all code is on a single line with no carriage returns.

Power down the Pi Zero W and attach the USB cable from the router’s USB port to the Pi’s USB port (not the power port). Using SSH log into the router and issue the following two commands which should bring up the usb0 port and bind it to an IP address.
ifconfig usb0 up
brctl addif br0 usb0

Using SSH, access the Pi Zero either by its new IP address assigned by the router or by its name (ex: raspberrypi.local). Once accessed, run the following command to create the /etc/modprobe.d/g_ether.conf file:
echo 'options g_ether host_addr='$(dmesg | awk '/: HOST MAC/{print $NF}')' dev_addr='$(dmesg | awk '/: MAC/{print $NF}') | sudo tee /etc/modprobe.d/g_ether.conf

While still using SSH to the Pi Zero W, edit the /etc/modprobe.d/g_ether.conf file and replace the dev_addr= Mac address with the MAC address for your Pi Zero W’s eth0 MAC you obtained previously. Make note of the host_addr= MAC address in the g_ether.conf as it will be needed later.

Note: If you do not use the Pi Zero eth0 MAC address you may have trouble at the end of this procedure accessing websites by their name.

sudo nano /etc/modprobe.d/g_ether.conf
dev_addr=(replace with your Pi’s MAC address)

While still connected to the Pi via SSH, issue the following command to add the new code to the end of the cmdline.txt file after the text modules-load=dwc2,g_ether:

sudo nano /boot/cmdline.txt
g_ether host_addr=(host_addr MAC) dev_addr=(Pi’s MAC address)

Reboot the Pi Zero W.

Access the router via SSH and issue the following two commands:
ifconfig usb0 up
brctl addif br0 usb0

Log into the Pi Zero W via SSH and issue the following command to reconfigure/reset the Pi-Hole.
pihole –r
Select: Reconfigure

Directions/Steps on the Asus router running Merlin firmware:

Steps below are to be issued from the router via SSH or using WinSCP.

Access router via SSH and create the following file with the following text/code:
sudo nano /jffs/scripts/init-start

#!/bin/sh
# Replace hotplug2 rules with our own, and restart hotplug2
ln -sf /jffs/hotplug2.rules /etc/hotplug2.rules
killall hotplug2

Next, access the /etc/hotplug2.rules file and copy the contents. Then create a new file with the following command and paste the copied /etc/hotplug2.rules contents to the new hotplug2.rules file.

sudo nano /jffs/hotplug2.rules

While still in the hotplug2.rules file add the following line to the SUBSYSTEM == net section:

exec /jffs/scripts/script_hotplug.sh ;

Example:
SUBSYSTEM == net, ACTION is set {
exec /sbin/hotplug %SUBSYSTEM% ;
exec /jffs/scripts/script_hotplug.sh ;
}

Next create the script_hotplug.sh file with the following command and paste in the code:
sudo nano /jffs/scripts/script_hotplug.sh

#!/bin/sh
echo $(date)" Action: "$ACTION" Product: "$PRODUCT" Interface: "$INTERFACE >> /tmp/hotplug.log
if [ "${INTERFACE}" = "usb0" ]; then
if [ "${ACTION}" = "add" ]; then
ifconfig usb0 up
brctl addif br0 usb0
fi
fi

Set permissions to 0755 on the following two files:
/jffs/scripts/init-start
/jffs/scripts/script_hotplug.sh

Set permissions to 0644 on the following file:
/jffs/hotplug2.rules

Note: Failure to set the permission on the three files may cause the scripts not to run.

It may help to access the router’s admin page and assign a static IP address using the Pi’s MAC address.

Reboot the router and it should mount the Pi Zero W and Pi-Hole should (fingers crossed ) work properly. The router should auto mount the Pi when the Pi reboots or is reconnected.

In rare instances the router may drop the Pi Zero W or not properly load the Pi on router reboot or may just spontaneously reboot. If this happens and the Pi Zero W is not obtaining an IP address try issue the two commands via SSH to the router.

ifconfig usb0 up
brctl addif br0 usb0

Main Source Links:

http://www.circuitbasics.com/raspberry-pi-zero-ethernet-gadget/

Notes:
If one wasn’t using a USB to Ethernet adapter with their Pi Zero W, one can generating a fake MAC address using sites like the following: https://www.miniwebtool.com/mac-address-generator/

2 Likes

Great post. I actually learned a lot for this while I was using a SoundCloud ripper.. It was quite insightful.