Configuring hostapd on Raspberry Pi

The daemon hostapd is a Linux service which enables  a “host” computer to become a WiFi Access Point (AP).     Thus  “host” + “AP” + “d”  (for daemon) gives the name hostapd.

I originally learned to set up hostapd from  instructions on Pastebin written by user Dryfire117,1 andlater found useful instructions on the Raspberry Pi website.2 The process originally involved building the daemon from source code to get support for the nl80211 driver, but that is now included with the prepackaged version you can install using apt-get. Here are the main steps:

  1.  Install:  Install hostapd with the following command
    $ sudo apt-get install hostapd
  2. Configure: Create and edit the configuration file, at /etc/hostapd/hostapd.conf.
    For starters, include the following:

    interface=wlan1
    #If this fails, try rt1871xdrv a 
    driver=nl80211
    # Name of the new network: best use the hostname
    ssid=wifipi
    
    # Pick a channel not already in use
    channel=6
    # Change to b for older devices?
    hw_mode=g
    macaddr_acl=0
    auth_algs=3
    # Disable this to insure the AP is visible:
    ignore_broadcast_ssid=0

    This creates an open, unsecured access point. Anybody can connect to it without having to give a password. In some cases that is what you want, but in other cases you will want
    to turn on WPA for security and add a password.  In that case, add the following to the hostapd.conf file:

    wpa=2
    wpa_passphrase="RaspberryWiFi"
    wpa_key_mgmt=WPA-PSK
    wpa_pairwise=TKIP
    rsn_pairwise=CCMP
    

    You should, of course, pick a better password. It has to be at least 8 characters long.

    There are many, many other options which can be set in this configuration file, but these are the ones needed in most cases to get up and running. Full documentation of the options can be found in a sample configuration file at https://w1.fi/cgit/hostap/plain/hostapd/hostapd.conf

    A few things I’ve learned from testing options on the Raspberry pi are that trying to enable the Automated Channel Selection (ACS) algorithm  via “channel=0” does not work, and setting “hw_mode=any” does not work. At least they didn’t work for me when I tried them, but maybe they have been fixed since then.

  3. Test:  Test it manually to make sure the configuration file is okay:
    $  sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf

    If there are errors it will complain and abort.  Unfortunately some older wifi devices don’t support all the features necessary to run an access point.   This is where you find out if yours will work.If there are no problems it will run and you can see the network SSID on a nearby device. Simply press ^C to stop it.

  4. Enable: Have The latest version of hostapd uses systemd startup rather than initd.  It also starts out “masked” so you need to first unmask the service.  The commands are (as root):
    #  systemctl unmask hostapd
    #  systemctl enable hostapd
    

    You can test manually that the service will start this way with

    #  systemctl start hostapd
    

    You can, if you wish, change the location of the configuration file by editing the file /etc/defaults/hostapd and setting the variable DAEMON_CONF to the full path to the alternate configuration file.

Once hostapd is enabled and running, the next step is to enable the DHCP service, so that clients joining the network are automatically assigned IP addresses.

References

  1. How to: Make a Raspberry Pi Powered Wifi Repeater” by Dryfire117,  https://pastebin.com/A4jUp2Nq
  2. Setting up a Raspberry Pi as a routed wireless access point,” https://www.raspberrypi.org/documentation/configuration/wireless/access-point-routed.md
Print Friendly, PDF & Email

12 thoughts on “Configuring hostapd on Raspberry Pi

  • 7 Jan 2023 at 12:23
    Permalink

    If you run the hostapd manually and it works, then you only need to restart the hostapd service after a while after the reboot, or simply add sleep 30 in the service file.

    Reply
  • 27 Sep 2022 at 01:39
    Permalink

    Hi All,

    Anyone know hoe to upgrade hostapd version 2.9 to 2.10 for supporting 802.11ax feature.

    Reply
  • 5 Aug 2021 at 05:43
    Permalink

    Successfully hosted, it connects devices but they have no internet. I am Using to 2 NICs and WIFI card. 1 for my lan, 1 for internet and wifi card for hotspot.

    Can someone assist

    Reply
  • 7 Jun 2021 at 12:02
    Permalink

    Sorry but I am mostly a cut and paste programmer. I’ve updated Raspbian to the latest on my 3B and what I am looking for is simply an AP transmitter of everything that is going on on my LAN and that any device that attaches to the AP will draw the DHCP infor from the router. My pi 4 is running OMV via a LAN cable at a distance from the main router and I want to simply use the radio in there to broadcase an non-routed AP signal. So is this what is going on here? If so do you know what I may be doing wrong?

    pi@raspberrypi:~ $ sudo service hostapd stop
    pi@raspberrypi:~ $ sudo service hostapd start
    pi@raspberrypi:~ $ sudo service hostapd stop

    pi@raspberrypi:~ $ sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf
    Configuration file: /etc/hostapd/hostapd.conf
    wlan0: interface state UNINITIALIZED->COUNTRY_UPDATE
    wlan0: Could not connect to kernel driver
    Using interface wlan0 with hwaddr dc:a6:32:**:**:** and ssid “MySSID”
    wlan0: interface state COUNTRY_UPDATE->ENABLED
    wlan0: AP-ENABLED

    ^Cwlan0: interface state ENABLED->DISABLED
    wlan0: AP-DISABLED
    wlan0: CTRL-EVENT-TERMINATING
    nl80211: deinit ifname=wlan0 disabled_11b_rates=0
    pi@raspberrypi:~ $

    I had to use CTL-C (Windows) to release ….

    Files:

    /etc/dhcpcd.conf

    denyinterfaces wlan0 eth0

    interface br0

    /etc/systemd/network/br0-member-eth0.network

    [Match]
    Name=eth0

    [Network]
    Bridge=br0

    /etc/systemd/network/bridge-br0.netdev

    [NetDev]
    Name=br0
    Kind=bridge

    /etc/hostapd/hostapd.conf

    country_code=US
    interface=wlan0
    bridge=br0
    ssid=Myssid
    hw_mode=g
    channel=10
    macaddr_acl=0
    auth_algs=1
    ignore_broadcast_ssid=0
    wpa=2

    Reply
    • 7 Jun 2021 at 13:47
      Permalink

      What I think you are looking for is called a “bridge” rather than an Access Point. My first attempt at that failed, but I am going to give it a try again when I get some time. The instructions I found for making a bridge were at https://www.raspberrypi.org/documentation/configuration/wireless/access-point-bridged.md and I’m not sure why this didn’t work for me. It looks like what you’ve listed above matches that fairly well, so hopefully one of us will figure it out, but I can’t say that I have yet. Sorry.

      Reply
  • 12 Jan 2021 at 09:22
    Permalink

    Thanks for these posts, very helpful. In your first post on network config you set up wlan0 as your AP, but in this post you have wlan1 in your hostapd.conf file as the access point, is that accurate?

    Reply
    • 12 Jan 2021 at 14:26
      Permalink

      Yes, I switched to using wlan0 for the AP because that was more general, and then either eth0 or wlan1 could be the uplink. In the past I’d used the second WiFi for the uplink, but as I went through the instructions again and updated them I also was changing this to use eth0 with cable run through my basement to get coverage for the other end of the house. I think the current instructions are better for both use cases.

      Reply
  • 18 Jun 2020 at 17:22
    Permalink

    I had a problem that the Wifi never started even after following these instructions. Here is the solution that I found.

    https://unix.stackexchange.com/questions/119209/hostapd-will-not-start-via-service-but-will-start-directly

    I set /etc/default/hostapd line to spell out the full file name of the hotapd config file

    DAEMON_CONF=”/etc/hostapd/hostapd.conf”

    This is because of lines in syslog about config file not found

    “Jun 18 19:44:00 raspberrypi systemd[1]: Stopped Advanced IEEE 802.11 AP and IEEE 802.1X/WPA/WPA2/EAP Authenticator.
    Jun 18 19:44:00 raspberrypi systemd[1]: Starting Advanced IEEE 802.11 AP and IEEE 802.1X/WPA/WPA2/EAP Authenticator…
    Jun 18 19:44:00 raspberrypi hostapd[15736]: Configuration file:
    Jun 18 19:44:00 raspberrypi hostapd[15736]: Could not open configuration file ” for reading.
    Jun 18 19:44:00 raspberrypi hostapd[15736]: Failed to set up interface with
    Jun 18 19:44:00 raspberrypi hostapd[15736]: Failed to initialize interface
    Jun 18 19:44:00 raspberrypi systemd[1]: hostapd.service: Control process exited, code=exited status=1
    Jun 18 19:44:00 raspberrypi systemd[1]: Failed to start Advanced IEEE 802.11 AP and IEEE 802.1X/WPA/WPA2/EAP Authenticator.
    Jun 18 19:44:00 raspberrypi systemd[1]: hostapd.service: Unit entered failed state.
    Jun 18 19:44:00 raspberrypi systemd[1]: hostapd.service: Failed with result ‘exit-code’.

    This is not something that I have ever seen before in any instructions on setting up Hostapd and I certainly didn’t have to do it on my previous setup.

    Reply
    • 12 Jan 2021 at 14:29
      Permalink

      It looks like it’s not getting the name of the config file. The part that says “Could not open configuration file ” for reading.” would have the name between those single quotes. Maybe there is a problem with a closing quote on that line in /etc/default/hostapd?

      Reply
  • 18 Jun 2020 at 07:57
    Permalink

    Excellent article, very useful.
    Big BUT I have been struggling to get a Wifi AP working in Raspbian Buster and Raspi OS folowing the usual instructions like these.
    Have you tested these on later OS’s? This is a problem I beleive

    Reply

Leave a Reply to Eric Myers Cancel reply

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

Skip to toolbar