まんま!の備忘録

ソフトウェア・ファームウェア・ハードウェア関連の備忘録

Raspberry PiのAP化(スタンドアロン運用)

スタンドアロン運用APの手順を記載していきます。
internet <-> (PC <-> USB) or (Wi-Fi AP) <-> Raspberry Pi(usb0 or wlan0) <-not connect-> Raspberry Pi(ap0) <-> iPhone

※以下参考にさせていただきました。
www.mikan-tech.net


必要パッケージインストール。

taogya@taogya-pi0:~ $ sudo su -
root@taogya-pi0:~ $ apt-get install -y hostapd dnsmasq

バックアップ

root@taogya-pi0:~ $ cp /etc/network/interfaces /etc/network/interfaces.org
root@taogya-pi0:~ $ cp /etc/dhcpcd.conf /etc/dhcpcd.conf.org
root@taogya-pi0:~ $ cp /etc/default/hostapd /etc/default/hostapd.org
root@taogya-pi0:~ $ mv /etc/dnsmasq.conf /etc/dnsmasq.conf.org
root@taogya-pi0:~ $ mv /lib/systemd/system/hostapd.service /lib/systemd/system/hostapd.service.org

hostapdの設定。

# コンフィグ読み込み先設定
root@taogya-pi0:~ $ sed -i '/#DAEMON_CONF=""/a DAEMON_CONF="/etc/hostapd/hostapd.conf"' /etc/default/hostapd
# サービス起動遅延設定
root@taogya-pi0:~ $ mkdir -p /root/services/hostapd/
root@taogya-pi0:~ $ cp /lib/systemd/system/hostapd.service.org /root/services/hostapd/hostapd.service
root@taogya-pi0:~ $ sed -i "/ExecStart=/a ExecStartPre=/bin/sleep 10" /root/services/hostapd/hostapd.service
# パーミッション設定
root@taogya-pi0:~ $ chmod -R 600 /root/services
# シンボリックリンク作成
root@taogya-pi0:~ $ ln -s /root/services/hostapd/hostapd.service /lib/systemd/system/hostapd.service

APのSSIDとPassphraseを生成。(適宜変更してください。)

root@taogya-pi0:~ $ wpa_passphrase "APSSID" "APPassphrase"
network={
	ssid="APSSID"
	#psk="APPassphrase"
	psk=c43dc6ff2691511575295b51a26a8f574e0d429fe2f430c9094714521d90ef44
}

AP用のインターフェース作成。

root@taogya-pi0:~ $ mkdir -p /root/configs/rules.d/
root@taogya-pi0:~ $ WLAN0_MACADDR="$(iw dev | grep wlan0 -A 6 | \
grep addr | grep -o -E '([0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5})')"
root@taogya-pi0:~ $ iw phy phy0 interface add ap0 type __ap
root@taogya-pi0:~ $ ip link set ap0 address "${WLAN0_MACADDR}"
root@taogya-pi0:~ $ echo "SUBSYSTEM==\"ieee80211\", \
ACTION==\"add|change\", \
ATTR{macaddress}==\"${WLAN0_MACADDR}\", \
KERNEL==\"phy0\", \
RUN+=\"/sbin/iw phy phy0 interface add ap0 type __ap\", \
RUN+=\"/bin/ip link set ap0 address ${WLAN0_MACADDR}\"" > /root/configs/rules.d/99-ap0.rules
root@taogya-pi0:~ $ ln -s /root/configs/rules.d/99-ap0.rules /etc/udev/rules.d/99-ap0.rules

コンフィグファイルを作成。

# ### DHCP設定
# (static ip_addressはお好きなIPで)
root@taogya-pi0:~ $ mkdir -p /root/configs/dhcpcd/
root@taogya-pi0:~ $ echo -n 'interface ap0
static ip_address=192.168.254.1/24
nohook wpa_supplicant
' > /root/configs/dhcpcd/ap_mode

# ### dnsmasqの設定
# (dhcp-rangeはお好きな範囲で)
root@taogya-pi0:~ $ mkdir -p /root/configs/dnsmasq/
root@taogya-pi0:~ $ echo -n '
# https://github.com/imp/dnsmasq/blob/master/dnsmasq.conf.example
interface=ap0
dhcp-range=192.168.254.50,192.168.254.150,255.255.255.0,12h
' > /root/configs/dnsmasq/dnsmasq.conf

# ### hostapdの設定(適宜変更してください。)
root@taogya-pi0:~ $ mkdir -p /root/configs/hostapd/
root@taogya-pi0:~ $ echo -n '
# https://github.com/latelee/hostapd/blob/master/hostapd/hostapd.conf
interface=ap0
driver=nl80211

ssid=APSSID
country_code=JP
hw_mode=g
channel=7
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
ieee80211n=1

wpa=2
wpa_psk=c43dc6ff2691511575295b51a26a8f574e0d429fe2f430c9094714521d90ef44
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
' > /root/configs/hostapd/hostapd_standalone.conf

コンフィグファイルのパーミッションを設定

root@taogya-pi0:~ $ chmod -R 600 /root/configs 

以下シェルスクリプトを作成する

root@taogya-pi0:~ $ mkdir -p /root/shells/ap/

# ### ステータス確認
root@taogya-pi0:~ $ vi /root/shells/ap/status.sh
#! /bin/sh

echo "hostapd:    $(systemctl status hostapd | grep -E '^     Active: ')"
echo "dnsmasq:    $(systemctl status dnsmasq | grep -E '^     Active: ')"
echo "dhcpcd:     $(systemctl status dhcpcd | grep -E '^     Active: ')"

# ### 無効化
root@taogya-pi0:~ $ vi /root/shells/ap/disable.sh
#! /bin/sh

DHCPCD_CONF_PATH=/root/configs/dhcpcd/ap_mode

echo "***** do disable.sh"

echo "===== do disable hostapd"
systemctl stop hostapd
systemctl disable hostapd
systemctl mask hostapd
unlink /etc/hostapd/hostapd.conf
echo "Removed symlink /etc/hostapd/hostapd.conf"
echo "===== done disable hostapd"

echo "===== do disable dnsmasq"
systemctl stop dnsmasq
systemctl disable dnsmasq
systemctl mask dnsmasq
unlink /etc/dnsmasq.conf
echo "Removed symlink /etc/dnsmasq.conf"
echo "===== done disable dnsmasq"

echo "===== do reset dhcpcd"
while read -r LINE || [ -n "${LINE}" ]
do
    sed -i -e "s|^${LINE}$||g"  /etc/dhcpcd.conf
    sed -i -e "/^$/{N; /^\n$/D;}"  /etc/dhcpcd.conf
done < "${DHCPCD_CONF_PATH}"
echo "Removed ${DHCPCD_CONF_PATH}"
systemctl restart dhcpcd
echo "===== done reset dhcpcd"

echo "***** done disable.sh"

# ### スタンドアロン運用有効化
root@taogya-pi0:~ $ vi /root/shells/ap/enable_standalone.sh
#! /bin/sh

DHCPCD_CONF_PATH=/root/configs/dhcpcd/ap_mode
DNSMASQ_CONF_PATH=/root/configs/dnsmasq/dnsmasq.conf
HOSTAPD_CONF_PATH=/root/configs/hostapd/hostapd_standalone.conf

echo "***** do enable_standalone.sh"

echo "===== do set dhcpcd"
cat "${DHCPCD_CONF_PATH}" >> /etc/dhcpcd.conf
echo "Added ${DHCPCD_CONF_PATH} -> /etc/dhcpcd.conf"
systemctl restart dhcpcd
echo "===== done set dhcpcd"

echo "===== do enable dnsmasq"
ln -s "${DNSMASQ_CONF_PATH}" /etc/dnsmasq.conf
echo "Created symlink ${DNSMASQ_CONF_PATH} -> /etc/dnsmasq.conf"
systemctl unmask dnsmasq
systemctl enable dnsmasq
systemctl start dnsmasq
echo "===== done enable dnsmasq"

echo "===== do enable hostapd"
ln -s "${HOSTAPD_CONF_PATH}" /etc/hostapd/hostapd.conf
echo "Created symlink ${HOSTAPD_CONF_PATH} -> /etc/hostapd/hostapd.conf"
systemctl unmask hostapd
systemctl enable hostapd
systemctl start hostapd
echo "===== done enable hostapd"

echo "***** done enable_standalone.sh"

# ### シェルスクリプトパーミッションを設定
root@taogya-pi0:~ $ chmod -R 700 /root/shells


長々書きましたが、最後に起動手順です。

root@taogya-pi0:~ $ sh /root/shells/ap/disable.sh > test.log 2>&1 &
root@taogya-pi0:~ $ sh /root/shells/ap/enable_standalone.sh > test.log 2>&1 &
root@taogya-pi0:~ $ sh /root/shells/ap/status.sh
  • 無効化
root@taogya-pi0:~ $ sh /root/shells/ap/disable.sh > test.log 2>&1 &
root@taogya-pi0:~ $ sh /root/shells/ap/status.sh