Skip to content

Instantly share code, notes, and snippets.

@factorin-j
Last active May 12, 2025 04:43
Show Gist options
  • Select an option

  • Save factorin-j/90a015827dc2086a3df8fcec082e7054 to your computer and use it in GitHub Desktop.

Select an option

Save factorin-j/90a015827dc2086a3df8fcec082e7054 to your computer and use it in GitHub Desktop.
RaspberryPi Zero W Bridge usb0 to wlan0 (access point)
## /home/pi/bridge.sh
#!/usr/bin/env bash
service hostapd stop
service dnsmasq stop
service dhcpcd restart
ifdown wlan0
ifup wlan0
sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
iptables --flush
iptables --table nat --flush
iptables --delete-chain
iptables --table nat --delete-chain
iptables --table nat --append POSTROUTING --out-interface usb0 -j MASQUERADE
iptables --append FORWARD --in-interface wlan0 -j ACCEPT
service dnsmasq start
service hostapd start
## /etc/dhcpcd.conf
denyinterfaces wlan0
interface usb0
static ip_address=192.168.2.50/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1 8.8.8.8
## /etc/dnsmasq.conf
interface=wlan0
listen-address=192.168.220.1
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=192.168.220.50,192.168.220.150,12h
## /etc/default/hostapd
DAEMON_CONF="/etc/hostapd/hostapd.conf"
## /etc/hostapd/hostapd.conf
interface=wlan0
driver=nl80211
ssid=WiFiNameHere
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=3
wpa_passphrase=anyPasswordHere
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
## /etc/rc.local
/bin/bash /home/pi/bridge.sh
## /etc/network/interfaces.d/wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.220.1
netmask 255.255.255.0
network 192.168.220.0
broadcast 192.168.220.255
@GrRivero
Copy link

GrRivero commented Feb 5, 2020

Hi John Marlo,

Did you get a raspberry zero connected by usb to wifi?

I followed your files but it doesn't work, the error in hostapd is:

  • Feb 05 14:54:35 raspberrypi systemd[1]: hostapd.service: Service RestartSec=2s expired, scheduling restart.
  • Feb 05 14:54:35 raspberrypi systemd [1]: hostapd.service: Scheduled restart job, restart counter is at 39.
  • Feb 05 14:54:35 raspberrypi systemd [1]: Stopped Advanced IEEE 802.11 AP and IEEE 802.1X / WPA / WPA2 / EAP Authenticator.
  • Feb 05 14:54:35 raspberrypi systemd [1]: Starting Advanced IEEE 802.11 AP and IEEE 802.1X / WPA / WPA2 / EAP Authenticator ...
  • Feb 05 14:54:35 raspberrypi hostapd [708]: Configuration file: /etc/hostapd/hostapd.conf
  • Feb 05 14:54:35 raspberrypi hostapd [708]: Configuration file: /etc/hostapd/hostapd.conf
  • Feb 05 14:54:35 raspberrypi hostapd [708]: wlan0: Could not connect to kernel driver
  • Feb 05 14:54:35 raspberrypi hostapd [708]: Using interface wlan0 with hwaddr b8: 27: eb: ac: 74: 75 and ssid "olivo"
  • Feb 05 14:54:35 raspberrypi hostapd [708]: wlan0: interface state UNINITIALIZED-> ENABLED
  • Feb 05 14:54:35 raspberrypi hostapd [708]: wlan0: AP-ENABLED
  • Feb 05 14:54:35 raspberrypi hostapd [708]: Could not set channel for kernel driver
  • Feb 05 14:54:35 raspberrypi hostapd [708]: Interface initialization failed
  • Feb 05 14:54:35 raspberrypi hostapd [708]: wlan0: interface state UNINITIALIZED-> DISABLED
  • Feb 05 14:54:35 raspberrypi hostapd [708]: wlan0: AP-DISABLED
  • Feb 05 14:54:35 raspberrypi hostapd [708]: wlan0: Unable to setup interface.
  • Feb 05 14:54:35 raspberrypi hostapd [708]: wlan0: interface state ENABLED-> DISABLED
  • Feb 05 14:54:35 raspberrypi hostapd [708]: wlan0: AP-DISABLED
  • Feb 05 14:54:35 raspberrypi hostapd [708]: wlan0: CTRL-EVENT-TERMINATING
  • Feb 05 14:54:35 raspberrypi hostapd [708]: nl80211: deinit ifname = wlan0 disabled_11b_rates = 0
  • Feb 05 14:54:36 raspberrypi hostapd [708]: wlan0: interface state DISABLED-> DISABLED
  • Feb 05 14:54:36 raspberrypi hostapd [708]: wlan0: AP-DISABLED
  • Feb 05 14:54:36 raspberrypi hostapd [708]: wlan0: CTRL-EVENT-TERMINATING
  • Feb 05 14:54:36 raspberrypi hostapd [708]: hostapd_free_hapd_data: Interface wlan0 wasn't started
  • Feb 05 14:54:36 raspberrypi hostapd [708]: nl80211: deinit ifname = wlan0 disabled_11b_rates = 0
  • Feb 05 14:54:36 raspberrypi systemd [1]: hostapd.service: Control process exited, code = exited, status = 1 / FAILURE
  • Feb 05 14:54:36 raspberrypi systemd [1]: hostapd.service: Failed with result 'exit-code'.
  • Feb 05 14:54:36 raspberrypi systemd [1]: Failed to start Advanced IEEE 802.11 AP and IEEE 802.1X / WPA / WPA2 / EAP Authenticator.

Any ideas???

Thanks a lot

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