基本的な設定が終わった所で、ようやく WiFi AP としての設定を行う。
ここでやることは以下の通り。なお、今回はひとまずルータにはしない。
以下、各々について書いていく。
■必要なソフトウェアのインストール
Linux における Access Point ツールとして、hostapd がある。
今回はこれを使ってラズパイをアクセスポイント化する。
アクセスポイントにするためのソフトウェアとしては、
アクセスポイントソフトそのものの他、
接続しにきたクライアントに IP address を動的に割り当てるための
DHCP サーバーソフトが必要となる。
よってこれらをあらかじめインストールしておく。
まずはリポジトリをアップデート。
user@raspberrypi$ sudo apt-get update
次に、以下のコマンドでアクセスポイントとして起動させるための hostapd をインストール。
user@raspberrypi$ sudo apt-get install hostapd -y
さらに、DHCP サーバーソフトも以下の通りインストール。
user@raspberrypi$ sudo apt-get install isc-dhcp-server -y
他にも色々ソフトはあると思うが、とりあえずうまくいったもので記載している。
■無線 LAN インタフェースの設定変更
ラズパイの無線 LAN インタフェースは、
デフォルトでクライアントとして動作するようになっている。
このため、アクセスポイントとして動作させるために、
/etc/network/inferface の wlan0 における wpa-conf 行をコメントアウトする。
# For static IP, consult /etc/dhcpcd.conf and ‘man dhcpcd.conf’# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
iface eth0 inet manual
allow-hotplug wlan0
iface wlan0 inet manual
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf ←ここを # でコメントアウト
allow-hotplug wlan1
iface wlan1 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
■IP address の固定化
アクセスポイントの IP address がコロコロ変わると大変なので、固定化する。
他サイトでは /etc/network/interface で設定する記載が多いが、
Raspbian Jessie の場合、/etc/dhcpcd.conf で設定することに注意が必要。
/etc/dhcpcd.conf の最下行に
static ip_address=192.168.0.1/24
の二行を追加する。
■hostapd の設定
続いて hostapd の設定を行う。
ここでは以下の二つを実施する。
・hostapd.conf の修正
まずは設定ファイルの雛形を使えるようにする。
user@raspberrypi$ cd /etc/hostapd/ user@raspberrypi$ sudo cp /usr/share/doc/hostapd/examples/hostapd.conf.gz ./ user@raspberrypi$ sudo gzip -d hostapd.conf.gz
その後、vi や emacs 等の editor を使い、雛形ファイルの必要な部分を修正していく。
* 以下、行数は目安。その辺りを探してもらいたい。
まずは SSID の設定。83 行目の SSID 設定を自分の好きな SSID に変更する。
ssid=my-network # <- = の後に好きな SSID 名(この場合は my-network)を入れる。
次に 96 行目の country code に関して,# を外し、US を JP に変更。
# Set as needed to indicate country in which device is operating.
# This can limit available channels and transmit power.
country_code=JP # <- 行頭の # を外し,US 部分を JP に変更。
242 行目の auth_algs は共通鍵認証方式のみを選択するため、3 -> 1 へ。
*Open System Authenticatio では誰でもつながってしまう。
# configured to allow both of these or only one. Open system authentication
# should be used with IEEE 802.1X.
# Bit fields of allowed authentication algorithms:
# bit 0 = Open System Authentication
# bit 1 = Shared Key Authentication (requires WEP)
auth_algs=1 # <- 3 を 1 に変更する。
IEEE 802.11n を有効化すべく、450 行目のコメントアウトを外す。
*と書いているものの正直よく分かっていない…。
# 0 = disabled (default)
# 1 = enabled
# Note: You will also need to enable WMM for full HT functionality.
ieee80211n=1 # <- 行頭の # を外す
続いて WPA 関連の設定。
*ほんとはセキュリティ確認のために WEP を設定したかったが、うまく接続できなかったため一旦断念。
利用する認証方式を WPA2 にするため、1019 行目のコメントアウトを外し、wpa=1 を wpa=2 とする。
# and/or WPA2 (full IEEE 802.11i/RSN):
# bit0 = WPA
# bit1 = IEEE 802.11i/RSN (WPA2) (dot11RSNAEnabled)
wpa=2 # <- 行頭の # を外した上で、1 を 2 に変更し、WPA2 を有効にする。
WPA2 のパスフレーズを設定する。1027 行目のコメントアウトを外し、wpa_passphrese を自身の好きなものに設定する。
# secret in hex format (64 hex digits), wpa_psk, or as an ASCII passphrase
# (8..63 characters) that will be converted to PSK. This conversion uses SSID
# so the PSK changes when ASCII passphrase is used and the SSID is changed.
# wpa_psk (dot11RSNAConfigPSKValue)
# wpa_passphrase (dot11RSNAConfigPSKPassPhrase)
#wpa_psk=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
wpa_passphrase=hogehoge # <- 行頭の # を外し、最低でも 8 文字のパスフレーズを設定する。
1048 行目のコメントアウトを外し、wpa_key_mgmt=WPA-PSK とする。
# entries are separated with a space. WPA-PSK-SHA256 and WPA-EAP-SHA256 can be
# added to enable SHA256-based stronger algorithms.
# (dot11RSNAConfigAuthenticationSuitesTable)
wpa_key_mgmt=WPA-PSK # <- 行頭の # を外し、= の後を WPA-PSK とする。
最後に 1062 行目のコメントアウトを外す。rsn_pairwise=CCMP となるはず。
# (unicast packets). This is a space separated list of algorithms:
# CCMP = AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0]
# TKIP = Temporal Key Integrity Protocol [IEEE 802.11i/D7.0]
# Group cipher suite (encryption algorithm for broadcast and multicast frames)
# is automatically selected based on this configuration. If only CCMP is
# allowed as the pairwise cipher, group cipher will also be CCMP. Otherwise,
# TKIP will be used as the group cipher.
# (dot11RSNAConfigPairwiseCiphersTable)
# Pairwise cipher for WPA (v1) (default: TKIP)
#wpa_pairwise=TKIP CCMP
# Pairwise cipher for RSN/WPA2 (default: use wpa_pairwise value)
rsn_pairwise=CCMP # <- 行頭の # を外す。
・hostapd の起動時自動実行
アクセスポイントなので、ラズパイが起動したら
自動的にアクセスポイントとして動いてほしい。
よって、自動実行の設定も合わせて実施する。
/etc/default/hostapd を editor で編集する。
10 行目を #DAEMON_CONF=”” から DEAMON_CONF=”/etc/hostapd/hostapd.conf” に変更する。
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
# methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
DAEMON_CONF=”/etc/hostapd/hostapd.conf” # <- この部分。# Additional daemon options to be appended to hostapd command:-
# -d show more debug messages (-dd for even more)
# -K include key data in debug messages
# -t include timestamps in some debug messages
#
# Note that -B (daemon mode) and -P (pidfile) options are automatically
# configured by the init.d script and must not be added to DAEMON_OPTS.
#
#DAEMON_OPTS=””
■dhcpd の設定
さらに DHCP サーバーとしての設定を行う。
実施する内容は以下の三つ。
・/etc/dhcp/dhcpd.conf の修正
まずは /etc/dhcp/dhcpd.conf の修正を行う。
13 行目、14 行目はコメントアウトする。
# option domain-name “example.org”; # <- 例なので、行頭に # をつけてコメントアウトしておく。
# option domain-name-servers ns1.example.org, ns2.example.org; # <- こっちも同じ。
最後に、以下の内容を追加する。
range 192.168.0.10 192.168.0.200; # 発行する IP address の範囲を指定
option domain-name-servers 8.8.8.8, 8.8.4.4; # DNS server に google のものを指定
option routers 192.168.0.1; # router の IP address を指定
option broadcast-address 192.168.0.255; # broadcast address を指定
option subnet-mask 255.255.255.0; # subnet mask を指定
default-lease-time 600;
max-lease-time 7200;
}
・利用するインターフェースの指定
/etc/default/isc-dhcp-server の11 行目の DHCPD_CONF と
21 行目の INTERFACE を以下の通り変更する。
# sourced by /etc/init.d/isc-dhcp-server
# installed at /etc/default/isc-dhcp-server by the maintainer scripts#
# This is a POSIX shell fragment
## Path to dhcpd’s config file (default: /etc/dhcp/dhcpd.conf).
DHCPD_CONF=”/etc/dhcp/dhcpd.conf” # <- この形にする。
# Path to dhcpd’s PID file (default: /var/run/dhcpd.pid).
#DHCPD_PID=/var/run/dhcpd.pid
# Additional options to start dhcpd with.
# Don’t use options -cf or -pf here; use DHCPD_CONF/ DHCPD_PID instead
#OPTIONS=””
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. “eth0 eth1”.
INTERFACES=”wlan0″ # <- アクセスポイントとして動作するインターフェースを指定する。
これで dhcp の設定は一通り終了したので、以下の通りサービスを起動しておく。
user@raspberrypi$ sudo service isc-dhcp-server start
ここで
Job for isc-dhcp-server.service failed. See 'systemctl status isc-dhcp-server.service' and 'journalctl -xn' for details
というエラーが出た場合、素直に指示に従う。
*今回の設定でも実際にでた。
今回は、
no subnet declaration for wlan0 (no ipv4 addresses)
ということだった。おっかしーな、と思いながら色々調べてみたが、よく分からず。
とりあえず ifconfig コマンドでインターフェースの状態を確認してみると、起動してないっぽい。
そりゃダメだろ、ということで、
user@raspberrypi$ sudo ifconfig wlan0 up
としてみたが起きず…。しょうがないから reboot してみたら wlan0 に正しく IP address が振られている。
これはいけるか?と思って再度チャレンジ。
user@raspberrypi$ sudo service isc-dhcp-server start
起動した…。よかった…。
その他、subnet の設定範囲がそもそも変だったとか、
各種設定ファイルのパスが間違っているとか、色々ハマりがち。
はまったら今一度各設定が正しいかどうか、確認してみるとよいと思う。
*実は、以前書いた記事では、 /etc/dhcp/dhcpd.conf に記載する内容に誤りがあった。
*一つ目は、 option domain-name “test.local”;を記載していること。これがあると動かなかった。
*二つ目は、 IP address を一部 198.162.0.255 等としていたこと。範囲が違うと怒られていた。
*動かない場合、こういった点も注意してみて頂きたい。
・dhcpd の起動時自動実行
当然こちらも自動起動してくれないと困る。
なのでひとまず /etc/rc.local に以下の内容を追加。
*exit 0 の前に入れる。
具体的にはこんな感じ。
#
# 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 5;/etc/init.d/isc-dhcp-server start & # <- この部分を追加。
exit 0
sleep 5; の部分は、起動を 5 秒まつ、ということ。
いきなり起動させると他のプロセスとぶつかって落ちる、というのが報告されているため、これを追加。
あと、正直いるのかいらないのかわからないのだけれども、一応以下のコマンドを実行。
user@raspberrypi$ sudo systemctl enable isc-dhcp-server
その後 reboot すると、無事他の WiFi クライアント(スマホとか)から見えた。
これで WiFi アクセスポイント化はひとまず完了。
ピンバック: Raspberry Pi3(Raspbian Stretch)をアクセスポイント化してWiFiで動画配信する | Kazuki Room ~3Dプリンター・電子工作・CAD・Arduino~