本文を読み飛ばす

hostapd: How to Set Up a WiFi Access Point with hostapd on Raspberry Pi OS

You can easily turn a Raspberry Pi into a WiFi access point using the hostapd software package.

However, if you're using an external USB WiFi adapter, you may need a helper script to reliably unblock the interface at boot.

This guide provides step-by-step instructions to get it set up correctly.

Install the required packages

First, install hostapd:

apt install -y hostapd

Create the hostapd Configuration file

Next, create the hostapd configuration file at /etc/hostapd.conf with the following content.

Be sure to change the ssid and wpa_passphrase to your liking.

interface=wlan0
bridge=br0
driver=nl80211

logger_syslog=-1
logger_syslog_level=3
logger_stdout=-1
logger_stdout_level=3

ctrl_interface=/var/run/hostapd
ctrl_interface_group=0

ssid=test-test
macaddr_acl=0

country_code=JP
ieee80211d=1

hw_mode=a
ieee80211n=1
ieee80211ac=1
channel=40      # 40: 5200MHz, see: iw phy

auth_algs=1
wpa=2
wpa_passphrase=strong-pw-a
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP

wmm_enabled=1
wme_enabled=0

Unblock the WiFi Interface with rfkill

Before hostapd can run, the WiFi interface must be unblocked using rfkill.

The challenge is that the rfkill ID is not fixed for removable hardware like USB WiFi adapters; it can change each time you reboot.

To solve this, I wrote a script that automatically finds the correct device and unblocks it.

This example is for a device using the 8812au driver. Save the following script as /root/unblock-rtl8812au.sh:

#! /bin/bash
find_tgt=/sys/class/net/wlan*
find_opt="-q 8812au"

function find_8812au_dev() {
    for i in $find_tgt; do
        if udevadm info -a --path=$i | grep $find_opt; then
            udevadm info -a --path=$i | sed -n 's/[ \t]*KERNEL=="\(.*\)"/\1/p'
            return 0
        fi
    done
    return 1
}


function find_phy_name() {
    n="$(iw dev $1 info | sed -n 's/[ \t]\+wiphy[ \t]\+\([0-9]\+\)/\1/p')"
    wiphy="Wiphy phy$n"
    iw list | grep -e '^\S' | while read l; do
        #cho "$l - $n - $wiphy"
        if [[ "$l" = $wiphy ]]; then
            echo "phy$n"
            return 0
        fi
    done
    return 1
}


function find_rfkill_id() {
    tmp=$(rfkill list | grep $1 | cut -d " " -f 1)
    echo ${tmp%%:}
}


function unblock() {
    sleep 10
    dev=$(find_8812au_dev)
    echo "got device: $dev"
    phy=$(find_phy_name $dev)
    echo "got phy: $phy"
    id=$(find_rfkill_id $phy)
    rfkill unblock $id
}

unblock

Modify the systemd Service

To run the script created above, we will modify the hostapd systemd service.

We'll use the ExecStartPre directive, which runs a command before the main service starts.

You can either edit the service file directly or, preferably, create an override file using systemctl edit hostapd.service.

Add the following lines:

[Service]
...
ExecStartPre=/root/unblock-rtl8812au.sh

Start and Enable the Service

After modifying the systemd settings, reload the daemon, start the service, and then check its status to confirm it's running without errors:

systemctl daemon-reload
systemctl start hostapd
systemctl status hostapd

If everything looks good, enable the service to start automatically on boot:

systemctl enable hostapd

Your Raspberry Pi should now be functioning as a WiFi access point! 🎉

コメント

Comments powered by Disqus
宣伝: