LoginSignup
5
5

More than 3 years have passed since last update.

Raspberry Pi3 wifiのAP/Clientを切り替え

Last updated at Posted at 2019-03-10

本記事の内容は古くなっています。
以下を参照してください。
https://qiita.com/yamato225/items/24acb27403136732e01b

概要

Raspberry Pi3に搭載されたwifiモジュールは、クライアントとしてwifiに接続することも、アクセスポイント(AP)として他の機器からRaspberry piに接続することもできます。
アクセスポイントとして起動すれば、キーボードやモニタを用意せずにスマホからSSH接続して設定を行うことができます。

一方で、アクセスポイント化⇔クライアント化の切り替えがうまく行かず、
少し苦労したので、備忘を兼ねて投稿します。

構成

Raspberry Pi 3

OSバージョン Raspbian stretch lite

$ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 9.8 (stretch)
Release:    9.8
Codename:   stretch

インストール

hostapd, dnsmasqをインストール。

sudo apt -y install hostapd dnsmasq

/etc/hostapd/hostapd.confにhostapdの設定を記述。

interface=wlan0
driver=nl80211
ssid=pipi        ##SSID名
hw_mode=g
channel=6
ieee80211n=1
wmm_enabled=1
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_passphrase=raspberry     ##SSIDパスワード
rsn_pairwise=CCMP

/etc/dhcpcd.confに以下の設定を追記。

## AP
# クライアント時は以下をコメントアウトする。
interface eth0
fallback static_eth0
denyinterface wlan0
interface wlan0
static ip_address=172.24.1.1/24
static routers=172.24.1.1
static domain_name_servers=172.24.1.1
static broadcast 172.24.1.255

いったん、ネットワーク系デーモンの自動起動を無効化。

sudo systemctl disable wpa_supplicant
sudo systemctl disable dhcpcd

切り替え手順

APにする

  1. /etc/dhcpcd.confのAP設定を有効化
  2. sudo systemctl stop wpa_supplicant
  3. sudo systemctl stop dhcpcd
  4. sudo ifconfig wlan0 down && sudo ifconfig wlan0 up
  5. sudo hostapd /etc/hostapd/hostapd.conf &
  6. sudo systemctl start dhcpcd
  7. sudo systemctl start dnsmasq

clientにする

  1. /etc/dhcpcd.confのAP設定を無効化
  2. sudo systemctl stop dnsmasq
  3. sudo systemctl stop dhcpcd
  4. sudo ifconfig wlan0 down && sudo ifconfig wlan0 up
  5. sudo systemctl start wpa_supplicant
  6. sudo systemctl start dhcpcd

疑問点メモ

  • 参考文献他の記事ではhostapdがサービスとして立ち上がるとのことでしたが、今回試した環境ではsystemdのスクリプトのパス(/etc/systemd/system/hostapd.service)に/dev/nullへのシンボリックリンクが貼られており、少なくとも単純にaptでインストールしただけではサービス化できませんでした。
  • Client→AP化する際にはwpa_supplicantがジャマをしているようで、停止してからdhcpcdを再起動しています。
  • AP化の際、dhcpcdの起動よりもhostapdの起動を先にしないと、dhcpcdが設定すべきNICを見つけられずエラーになるようです。clientの場合はwpa_supplicantが、APの場合はhostapdがNICをdhcpcdが操作できる状態にする、ということなんでしょうか?
  • 他記事では設定反映のためにOS再起動しているものがありますが、上記手順ではOSの再起動が不要であることを確認しました。

参考文献

  1. http://nw-electric.way-nifty.com/blog/2018/05/pi-3wifi-access.html
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5