Turn Raspberry Pi Zero 2 W into USB WiFi dongle

Turn Raspberry Pi Zero 2 W into USB WiFi dongle

The Raspberry Pi Zero 2 W is a versatile device capable of emulating multiple functionalities, such as a sound card, USB WiFi adapter, or USB storage using USB OTG (On-The-Go) mode. In this article, we focus on using it as a USB WiFi dongle. In this setup, a plugged-in computer detects the Pi Zero as a Network Interface Card (NIC) and connects to the internet through the Pi’s WiFi connection.

Motivation

You might ask: why use a Raspberry Pi Zero 2 W as a USB WiFi adapter? This setup is incredibly useful for devices without built-in WiFi or for operating systems with limited driver support, such as OpenBSD.

Instead of hunting for a compatible external WiFi dongle, especially in emergency situations, you can simply use a Pi Zero. It is widely recognized by most operating systems as a standard Ethernet device, requiring no additional drivers.

Diagram

Table of content

To make the setup process easier to follow, use the navigation below:

Install Raspberry Pi OS

The most effective way to install Raspberry Pi OS is via the Raspberry Pi Imager. Download it from the software section of the official website.

Follow the prompts to add your WiFi credentials and enable SSH. We strongly recommend using key-based authentication. If you didn’t enable it during the initial setup, you can follow our comprehensive guideto configure it later.

Once you can successfully SSH into your Pi Zero, proceed to the next step.

Enable OTG mode

To enable OTG mode, we need to modify two configuration files config.txt and cmdline.txt.

Open config.txt:

$ sudo vi /boot/firmware/config.txt

Add the following line to the end of the file:

dtoverlay=dwc2

Open cmdline.txt:

$ sudo vi /boot/firmware/cmdline.txt

Find the word rootwait and insert the following text immediately after it on the same line:

modules-load=dwc2,g_ether

Enable IP forwarding

We must enable IP forwarding so the Pi Zero can route packets received from the USB port to the WiFi router. This can be enabled via sysctl:

$ sudo sysctl -w net.ipv4.ip_forward=1

To make this change permanent, create a configuration file:

$ sudo vi /etc/sysctl.d/99-ipv.conf

Add the following line:

net.ipv4.ip_forward=1

Configure NAT (USB to WiFi)

For the USB port to access the network, the Pi Zero must act as a transparent gateway. We need to route packets between the USB interface and the WiFi interface while defining Network Address Translation (NAT) rules.

First, install the necessary tools:

$ sudo apt install -y iptables iptables-persistent

Then, apply the iptables rules:

$ sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
$ sudo iptables -A FORWARD -i usb0 -o wlan0 -j ACCEPT
$ sudo iptables -A FORWARD -i wlan0 -o usb0 -m state --state RELATED,ESTABLISHED -j ACCEPT
  • Rule 1: Replaces the private source IP of USB packets with the wlan0 IP.
  • Rule 2: Permits new connections originating from the USB interface.
  • Rule 3: Permits return traffic for existing sessions.

Save the rules to ensure they persist after a reboot:

$ sudo netfilter-persistent save

Disable Network Manager for the usb0 interface

By default, Network Manager attempts to control the usb0 interface. If nothing is connected at boot, it may disable the interface. To prevent this, we must instruct Network Manager to ignore usb0.

Create the configuration file:

$ sudo mkdir -p /etc/NetworkManager/conf.d
$ sudo vi /etc/NetworkManager/conf.d/usb0.conf

Add the following lines:

[keyfile]
unmanaged-devices=interface-name:usb0

Configure networkd to manage usb0 interface

Since Network Manager is no longer handling usb0, we will delegate this task to systemd-networkd.

Enable the systemd-networkd:

$ sudo systemctl enable systemd-networkd
$ sudo systemctl start systemd-networkd

Create the network configuration file:

$ sudo vi /etc/systemd/network/usb0.network

Add the following content:

[Match]
Name=usb0

[Network]
Address=192.168.7.2/24
IPForward=yes

Assign IPs automatically via DHCP

The final stage is to configure the Pi Zero as a DHCP server. This allows the connected computer to receive an IP address automatically without manual configuration. We will use dnsmasq for this.

Install dnsmasq:

$ sudo apt install -y dnsmasq

Create a new configuration:

$ sudo vi /etc/dnsmasq.d/usb.conf

Define the interface and the IP range:

interface=usb0
dhcp-range=192.168.7.10,192.168.7.50,255.255.255.0,12h

Restart and enable the service:

$ sudo systemctl restart dnsmasq
$ sudo systemctl enable dnsmasq

Verify that the service is listening on the correct port:

$ sudo ss -lunp | grep :67

Finally, reboot your Pi Zero:

$ sudo reboot

Testing the Connection

Connect the Pi Zero to your computer using the USB data port (not the power-only port). Your computer should recognize the Pi Zero as a USB Ethernet adapter, automatically receive an IP address in the 192.168.7.X range, and gain internet access via the Pi’s WiFi.

Conclusion

In this tutorial, we demonstrated how to transform a Raspberry Pi Zero 2 W into a portable USB WiFi adapter. This is a powerful “Swiss Army Knife” solution for adding connectivity to legacy hardware or systems with driver issues.

Inline/featured images credits

  • Featured image generated by Gemini