LoginSignup
34
35

More than 3 years have passed since last update.

RaspberryPi(Raspbian)の無線LAN親機(アクセスポイント)設定

Last updated at Posted at 2016-04-02

IoTのお勉強用に、接続状態が確認しやすいアクセスポイントが必要になり、過去記事を見直しました。

RaspberryPi3以降は2までと違って、結構発熱します。たまに、熱暴走っぽい感じで停止していることがあるので、24時間安定稼働が必要な場合は、おっきめなヒートシンクを付けたり、ファンを回して空冷効率高めるなりの工夫が必要な場合があります。

無線LANの有効化

$ sudo raspi-configを実行し、

  • 4 Localisation Options
    • I4 Change Wi-fi Country
      • JP Japan

でWiFiの国情報を設定する。
直接/etc/wpa_supplicant/wpa_supplicant.confを編集する方法もあります。

/etc/dhcpcd.conf

dhcpcd.confの最後の方(先頭でも真中辺でも可)に下記3行を追加します。

denyinterfaces wlan0
interface wlan0
static ip_address=172.16.1.1/24

hostapd

インストール

$ sudo apt-get install -y hostapd

設定

  • /etc/hostapd/hostapd.conf 下記のような内容で/etc/hostapd/hostapd.confを用意します。
    ssid、wpa_passphraseは適宜変更して下さい。
/etc/hostapd/hostapd.conf
interface=wlan0
driver=nl80211
ssid=Pi3-AP
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=r@spberry
rsn_pairwise=CCMP

ステルスSSIDがうまく使えないかもしれないです。

  • /etc/default/hostapd /etc/init.d/hostapd でhostapdの起動・停止を行う場合、hostapd起動時に設定するコンフィグファイルのパス設定がこのファイルだったはず。
/etc/default/hostapd
DAEMON_CONF="/etc/hostapd/hostapd.conf"

自動起動設定

なぜか、$ sudo systemctl enable hostapdで作成された/etc/systemd/system/hostapd.serviceは/dev/nullのシンボリックリンクだったので、自動起動の手動設定が必要な場合があるようです。

下記のどちらかの方法が楽だと思います。

rc.localから起動する場合

/etc/rc.localに「/etc/init.d/hostapd start」を追加しておけば、RaspberryPi起動時にhostapdを起動してくれます。

/etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

/etc/init.d/hostapd start    #### この行追加

exit 0

一応、

$ sudo systemctl enable rc-local

を実行しておいた。

hostapd.serviceファイルを用意する場合

まず、下記の内容でhostapd.serviceを用意します。ファイルの中身がsystemdのserviceファイルとして正しいか?については、後々検討します(とりあえず動くからOK)。

hostapd.service
[Unit]
Description=Hostapd IEEE 802.11 AP, IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator
After=network.target

[Service]
ExecStart=/usr/sbin/hostapd /etc/hostapd/hostapd.conf

[Install]
WantedBy=multi-user.target

で、

$ sudo systemctl enable <<hostapd.serviceのフルパス>>

を実行すると下記のシンボリックリンクが生成され、systemctlコマンドでhostapdを起動/停止できるようになります。

  • /etc/systemd/system/multi-user.target.wants/hostapd.service
  • /etc/systemd/system/hostapd.service

DHCP

インストール

$ sudo apt-get install isc-dhcp-server

設定

  • /etc/dhcp/dhcpd.conf
/etc/dhcp/dhcpd.conf
中略
・・・
subnet 172.16.1.0 netmask 255.255.255.0 {
  range 172.16.1.100 172.16.1.200;
  option domain-name-servers 8.8.8.8, 8.8.4.4;
  option domain-name "raspi.localnet";
  option routers 172.16.1.1;
  option broadcast-address 172.16.1.255;
  default-lease-time 600;
  max-lease-time 7200;
}
  • /etc/default/isc-dhcp-server
/etc/default/isc-dhcp-server
中略
・・・
INTERFACESv4="wlan0"

自動起動

$ sudo systemctl enable isc-dhcp-server

DHCPサーバの遅延実行

DHCPサーバは(たぶん)hostapdの起動にお構いなしで起動します。
起動順序は別に気にしないのですが、hostapdが起動しないとwlan0にIPアドレスがセットされないため、DHCPサーバの起動に失敗する、という罠があります。
回避し手段はいろいろあると思いますが、とりあえず、/etc/rc.localで適当にsleepした後にDHCPサーバを起動するようにして回避しました。

/etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

sleep 10 && /etc/init.d/isc-dhcp-server start    ### この行追加

exit 0

IPマスカレード

とりあえず、セキュリティは無視しまして、一番基本的なNAT設定だけをします。

適当なファイル名で下記の内容のシェルを作成する

ここでは「set_iptables」とします。

set_iptables
#!/bin/sh

# IPv4 forwarding機能の有効化
echo 1 > /proc/sys/net/ipv4/ip_forward

# 既存設定の初期化
iptables -F
iptables -X

# Deafult Rule
iptables -P INPUT   ACCEPT
iptables -P OUTPUT  ACCEPT
iptables -P FORWARD ACCEPT

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

root権限で実行する

$ sudo ./set_iptables

参考サイト

最近(2020年8月現在)

上記の設定例では rc.local を使ってhostapdとdhcpサーバの起動制御をしていますが、最近では、そのような面倒な方法をしなくてもよいらしいです。

ラズパイを無線LANルーター化する ~アクセスポイント編~ (1/2)

34
35
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
34
35