Raspberry Pi Zero W as both wifi client and access point

Sometimes, it can be great to have a separate access point for some devices, like for example Internet of Things devices. The Pi Zero W can just do that. But what if you need it to connect to the internet at the same time? In this article, I present how to configure the Raspberry Pi Zero W as both a wifi client and an access point.I recently read an article on Hack a Day about doing the same and inspired myself from this other article. However, I cleaned a bit the scripts and introduced a new feature which will be useful when moving your raspberry in different locations: the access point channel will automatically be changed to match the client channel.

Configuring wifi client

Configuring the wifi client is need first since you will need to install some packages. Process is very standard and described everywhere on the internet.

Main thing to configure is the list of access points in /etc/wpa_supplicant/wpa_supplicant.conf. You can provide configurations for several wifi networks. Each configuration should be in the following format:


Preparing the Raspberry Pi 0W

First, we need to install some packages needed for the access point:

sudo apt-get install hostapd dnsmasq

Then we need to disable the automatic launch of both services:

sudo systemctl disable hostapd
sudo systemctl disable dnsmasq

Creating configuration files

First, you need to know that there will be a virtual device called uap0 which is bound to wlan0.
In file /etc/network/interfaces add the following lines to configure the access point IP:

iface uap0 inet static

Then, we create a template file /etc/hostapd/hostapd.conf.tmpl for the access point (see content below). Please note that the initialization script below will take it, change channel and overwrite the file /etc/hostapd/hostapd.conf.


Next is the configuration file for hostapd service, located at /etc/default/hostapd, which must contain the following:


And finally, the configuration for dnsmasq which will assign IP addresses to your devices. The file is /etc/dnsmasq.confg:


Note that I used Google’s DNS ( in my configuration and that allow IPs from 50 to 150.

Script to put it all together

Last but not least, comes the script that put it all together. Its goal is to create the virtual device, create the configuration file for hostapd from the current wifi network and launch all services. I put the script in /usr/local/bin/hostapdstart:

# Create the virtual device
/sbin/iw dev wlan0 interface add uap0 type __ap
ifup uap0
# Fetch wifi channel
CHANNEL=`iwlist wlan0 channel | grep Current | sed 's/.*Channel \([0-9]*\).*/\1/g'`
export CHANNEL
# Create the config for hostapd
cat /etc/hostapd/hostapd.conf.tmpl | envsubst > /etc/hostapd/hostapd.conf
# Uncomment the two following lines to get access to internet
#sysctl net.ipv4.ip_forward=1
#iptables -t nat -A POSTROUTING -s ! -d -j MASQUERADE

systemctl start dnsmasq
systemctl start hostapd

And use the /etc/rc.local file to launch the script at boot time. For this, we need to add just one line:

/bin/bash /usr/local/bin/hostapdstart

Final note

I tried to apply the very same method to the Raspberry Pi 3, but ended with a routing problem (default route on access point). I did not dug to find the solution but I believe solution may come from /etc/network/interfaces.

9 Replies to “Raspberry Pi Zero W as both wifi client and access point”

  1. Hi
    good tutorial ,how about first create AP then user connect to AP and then open some webpage and put main internet connection from there.is ir possible if yes any idea same thing we do with exp8266 wifi manager system only different that here we still keep access point.

  2. Hi
    It is a good tutorial, but incomplet, you forgot to specify the content of file “/etc/hostapd/hostapd.conf.tmpl” and without that, the script hostapdstart don’t create hostapd.conf file.
    Can you fix it?

  3. Hi,
    Difficult for me…
    I have “RTNETLINK answers: invalid argument” when executing sudo ifup uap0 from the script. (i tried to extract the root cause of my error).

    but now I am stuck.

    Thks in advance for help.

  4. Hi. I would like to have that config, but isolate clients to talk to each other. Is that possible, please

  5. I have really no idea if it is possible and how to do it. Sorry. If you find any answer fill free to keep us updated in the comments.

  6. there’s an *excellent* script available on github that, with minor mods, works on Raspbian Buster (Dec 2019). see https://github.com/lukicdarkoo/rpi-wifi. the mods:



    next, insert `#!/bin/bash` as below:

    # Populate `/bin/start_wifi.sh
    sudo bash -c ‘cat > /bin/rpi-wifi.sh’ << EOF
    echo 'Starting Wifi AP and client…'

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.