furuhashilab / Tochizi-hai-OpenDataHackathon

都知事杯 オープンデータハッカソンのリポジトリ
https://furuhashilab.github.io/Tochizi-hai-OpenDataHackathon/
1 stars 0 forks source link

Local Wi-fi アクセスポイント化 #27

Open ShogoHirasawa opened 2 years ago

ShogoHirasawa commented 2 years ago

Raspberry Piをローカルwi-fi接続するためのIsuue

ShogoHirasawa commented 2 years ago

現状: Raspberry Piをwifi接続でssh接続できている状態 IMG_8964

スクリーンショット 2021-12-20 12 14 04
ShogoHirasawa commented 2 years ago

現状の理解 IMG_8967

ShogoHirasawa commented 2 years ago

tips: アクセスポイント化の資料はここを参考。結構わかりやすかった https://www.youtube.com/watch?v=ec4Mn-knTIE

ShogoHirasawa commented 2 years ago

このサイトをみながらアクセスポイント化にチャレンジする https://www.itmedia.co.jp/news/articles/2008/14/news042.html

ShogoHirasawa commented 2 years ago
ShogoHirasawa commented 2 years ago

初っ端からエラー発生

sudo systemctl stop dhcpcd
``
<img width="483" alt="スクリーンショット 2021-12-20 14 47 02" src="https://user-images.githubusercontent.com/29940264/146717863-bb99b83b-c455-4d60-b75a-1be8967c4571.png">
<img width="485" alt="スクリーンショット 2021-12-20 14 47 28" src="https://user-images.githubusercontent.com/29940264/146717894-c96f4b33-83f2-4e28-9214-4b2312a25a0f.png">
スクリーンショット 2021-12-20 14 47 54

ちなみに、Raspberry Piをみたら緑のランプが消えていた...

ShogoHirasawa commented 2 years ago

control + C も受け付けず何もできない状態

ShogoHirasawa commented 2 years ago

緑ランプがつかない原因

たぶん、しばらく操作せず放置していると不具合がでるよう。(中のデータが破損する場合もあるとか、ないとか) GUI操作での例だが、CUIの場合でも同じなのかも。 https://dev.classmethod.jp/articles/restore-the-raspberry-pi-4-model-b-os-destroyed-by-a-biginner/

ShogoHirasawa commented 2 years ago

OSをこの手順にそって再インストール

https://github.com/furuhashilab/raspberrypi4unvt/issues/3

ShogoHirasawa commented 2 years ago

再インストールしてssh接続完了

スクリーンショット 2021-12-20 15 15 04
ShogoHirasawa commented 2 years ago

wifiでRaspberry Piに接続してるのに、dhcpcdストップしたのが原因

wifiでRaspberry Piに接続してるのに、dhcpcdをストップさせるコマンド打ったのが原因。 Ethernetケーブルで接続してsudo systemctl stop dhcpcdを実行したら無事に通った

スクリーンショット 2021-12-20 15 25 51
ShogoHirasawa commented 2 years ago

sudo nano /etc/dhcpcd.confを実行。 以下の画面が表示される

# A sample configuration for dhcpcd.
# See dhcpcd.conf(5) for details.

# Allow users of this group to interact with dhcpcd via the control socket.
#controlgroup wheel

# Inform the DHCP server of our hostname for DDNS.
hostname

# Use the hardware address of the interface for the Client ID.
clientid
# or
# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
# Some non-RFC compliant DHCP servers do not reply with this set.
# In this case, comment out duid and enable clientid above.
#duid

# Persist interface configuration when dhcpcd exits.
persistent
ShogoHirasawa commented 2 years ago

最後の行に以下を記述

interface wlan0
static ip_address=192.168.87.1/24   
ShogoHirasawa commented 2 years ago

dhcp再起動

以下のコマンド実行
sudo systemctl restart dhcpcd

ShogoHirasawa commented 2 years ago

hostapdのインストール

以下のコマンド実行
sudo apt install hostapd

ShogoHirasawa commented 2 years ago

エラー発生

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfuse2
Use 'sudo apt autoremove' to remove it.
The following NEW packages will be installed:
  hostapd
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 665 kB of archives.
After this operation, 1,817 kB of additional disk space will be used.
Err:1 http://raspbian.raspberrypi.org/raspbian bullseye/main armhf hostapd armhf 2:2.9.0-21
  Temporary failure resolving 'raspbian.raspberrypi.org'
E: Failed to fetch http://raspbian.raspberrypi.org/raspbian/pool/main/w/wpa/hostapd_2.9.0-21_armhf.deb  Temporary failure resolving 'raspbian.raspberrypi.org'
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
ShogoHirasawa commented 2 years ago

apt-get updateを実行したところ以下のエラー発生

Reading package lists... Done
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
ShogoHirasawa commented 2 years ago

Permissionで問題になっている部分を権限を"755"に変更

ShogoHirasawa commented 2 years ago

同じようなエラーがでたので、/var/lib/apt/lists/lock と/var/cache/aptを再帰的に777に変更

ShogoHirasawa commented 2 years ago

再度apt-get updateをやるも以下のエラー発生

Err:1 http://raspbian.raspberrypi.org/raspbian bullseye InRelease
  Temporary failure resolving 'raspbian.raspberrypi.org'
Err:2 http://archive.raspberrypi.org/debian bullseye InRelease
  Temporary failure resolving 'archive.raspberrypi.org'
Reading package lists... Done             
W: Problem unlinking the file /var/lib/apt/lists/partial/raspbian.raspberrypi.org_raspbian_dists_bullseye_InRelease - PrepareFiles (13: Permission denied)
W: Problem unlinking the file /var/lib/apt/lists/partial/archive.raspberrypi.org_debian_dists_bullseye_InRelease - PrepareFiles (13: Permission denied)
W: Failed to fetch http://raspbian.raspberrypi.org/raspbian/dists/bullseye/InRelease  Temporary failure resolving 'raspbian.raspberrypi.org'
W: Failed to fetch http://archive.raspberrypi.org/debian/dists/bullseye/InRelease  Temporary failure resolving 'archive.raspberrypi.org'
W: Some index files failed to download. They have been ignored, or old ones used instead.
ShogoHirasawa commented 2 years ago

Permission エラーが出ている部分を再び777に変更する

ShogoHirasawa commented 2 years ago

再度、sudo apt install hostapdをするも以下のエラー発生

sudo apt install hostapd
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfuse2
Use 'sudo apt autoremove' to remove it.
The following NEW packages will be installed:
  hostapd
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 665 kB of archives.
After this operation, 1,817 kB of additional disk space will be used.
Err:1 http://raspbian.raspberrypi.org/raspbian bullseye/main armhf hostapd armhf 2:2.9.0-21
  Temporary failure resolving 'raspbian.raspberrypi.org'
E: Failed to fetch http://raspbian.raspberrypi.org/raspbian/pool/main/w/wpa/hostapd_2.9.0-21_armhf.deb  Temporary failure resolving 'raspbian.raspberrypi.org'
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
ShogoHirasawa commented 2 years ago
pi@admin:~ $  hostapd -v
-bash: hostapd -v: command not found   

hostapdがインストールできていないもよう....。

mq-sol commented 2 years ago

Slackにも書きましたが、

権限は元に戻してください。

インターネットと疎通できない場合は。一旦 dhcpdを停止してwifiをルータに接続してパッケージのインストールが終わった後に改めて設定したらいいと思います。

ShogoHirasawa commented 2 years ago

dhcpdを停止してwifiをルータに接続してパッケージのインストールが終わった後に改めて設定

dhcpdを停止した後に Wi-Fiで通信ってできるものなのでしょうか? Wi-Fiで通信するためのものがdhcpdだと認識していました....。すいません。勉強不足で誤って認識しているかもしれません... 。

ShogoHirasawa commented 2 years ago

@mq-sol さんのアドバイス通り、hostapdとdnsmasqのインストールをしてから、最後にdhcpcd.confの設定をしてみる

ShogoHirasawa commented 2 years ago

支障なささそうな、dhcpcd.confの書き換えからスタート

sudo nano /etc/dhcpcd.conf に以下の記述をした

interface wlan0
static ip_address=192.168.87.1/24 
ShogoHirasawa commented 2 years ago

sudo apt install hostapd をやってみる。成功

pi@Shogo:~ $ sudo apt install hostapd
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfuse2
Use 'sudo apt autoremove' to remove it.
The following NEW packages will be installed:
  hostapd
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 665 kB of archives.
After this operation, 1,817 kB of additional disk space will be used.
Get:1 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian bullseye/main armhf hostapd armhf 2:2.9.0-21 [665 kB]
Fetched 665 kB in 2s (373 kB/s) 
Selecting previously unselected package hostapd.
(Reading database ... 99508 files and directories currently installed.)
Preparing to unpack .../hostapd_2%3a2.9.0-21_armhf.deb ...
Unpacking hostapd (2:2.9.0-21) ...
Setting up hostapd (2:2.9.0-21) ...
Created symlink /etc/systemd/system/multi-user.target.wants/hostapd.service → /lib/systemd/system/hostapd.service.
Job for hostapd.service failed because the control process exited with error code.
See "systemctl status hostapd.service" and "journalctl -xe" for details.
Created symlink /etc/systemd/system/hostapd.service → /dev/null.
Processing triggers for man-db (2.9.4-2) ...   
ShogoHirasawa commented 2 years ago

hostapdの設定

sudo nano /etc/hostapd/hostapd.conf

以下の通り記述

interface=wlan0
driver=nl80211
hw_mode=b
channel=1
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
ieee80211ac=0
wmm_enabled=1
ieee80211d=1
country_code=JP
ieee80211h=1
local_pwr_constraint=3
spectrum_mgmt_required=1
wpa=3
wpa_key_mgmt=WPA-PSK
ssid=raspi
wpa_passphrase=admin
ShogoHirasawa commented 2 years ago

hostapdのデフォルト動作ファイルを編集

sudo nano /etc/default/hostapd

「#DAEMON_CONF=""」の部分を以下の通り記述

DAEMON_CONF="/etc/hostapd/hostapd.conf"
ShogoHirasawa commented 2 years ago

hostapdを動かす

マスクを外すため、以下のコマンドを実行

sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl start hostapd   

sudo systemctl status hostapd を打ち動作を確認

pi@Shogo:~ $ sudo systemctl status hostapd
● hostapd.service - Access point and authentication server for Wi-Fi and Ethernet
     Loaded: loaded (/lib/systemd/system/hostapd.service; enabled; vendor preset: enabled)
     Active: activating (auto-restart) (Result: exit-code) since Sat 2021-10-30 20:53:12 JST; 44ms ago
       Docs: man:hostapd(8)
    Process: 1702 ExecStart=/usr/sbin/hostapd -B -P /run/hostapd.pid -B $DAEMON_OPTS ${DAEMON_CONF} (code=exited, status=1/FAILURE)
        CPU: 22ms
ShogoHirasawa commented 2 years ago

ポートフォワード機能の設定

sudo nano /etc/sysctl.confで設定ファイルを開く

以下の行の先頭にある「#」を削除。

net.ipv4.ip_forward=1
ShogoHirasawa commented 2 years ago

dnsmasqをインストール

以下のコマンドを実行

sudo apt install dnsmasq
ShogoHirasawa commented 2 years ago

エラー発生。ネットにつながっていない模様。


pi@Shogo:~ $ sudo apt install dnsmasq
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfuse2
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  dns-root-data dnsmasq-base libnetfilter-conntrack3 libnfnetlink0
The following NEW packages will be installed:
  dns-root-data dnsmasq dnsmasq-base libnetfilter-conntrack3 libnfnetlink0
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 498 kB of archives.
After this operation, 1,159 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Err:1 http://raspbian.raspberrypi.org/raspbian bullseye/main armhf dns-root-data all 2021011101
  Temporary failure resolving 'raspbian.raspberrypi.org'
Err:2 http://raspbian.raspberrypi.org/raspbian bullseye/main armhf libnfnetlink0 armhf 1.0.1-3+b1
  Temporary failure resolving 'raspbian.raspberrypi.org'
Err:3 http://raspbian.raspberrypi.org/raspbian bullseye/main armhf libnetfilter-conntrack3 armhf 1.0.8-3
  Temporary failure resolving 'raspbian.raspberrypi.org'
Err:4 http://raspbian.raspberrypi.org/raspbian bullseye/main armhf dnsmasq-base armhf 2.85-1
  Temporary failure resolving 'raspbian.raspberrypi.org'
Err:5 http://raspbian.raspberrypi.org/raspbian bullseye/main armhf dnsmasq all 2.85-1
  Temporary failure resolving 'raspbian.raspberrypi.org'
E: Failed to fetch http://raspbian.raspberrypi.org/raspbian/pool/main/d/dns-root-data/dns-root-data_2021011101_all.deb  Temporary failure resolving 'raspbian.raspberrypi.org'
E: Failed to fetch http://raspbian.raspberrypi.org/raspbian/pool/main/libn/libnfnetlink/libnfnetlink0_1.0.1-3%2bb1_armhf.deb  Temporary failure resolving 'raspbian.raspberrypi.org'
E: Failed to fetch http://raspbian.raspberrypi.org/raspbian/pool/main/libn/libnetfilter-conntrack/libnetfilter-conntrack3_1.0.8-3_armhf.deb  Temporary failure resolving 'raspbian.raspberrypi.org'
E: Failed to fetch http://raspbian.raspberrypi.org/raspbian/pool/main/d/dnsmasq/dnsmasq-base_2.85-1_armhf.deb  Temporary failure resolving 'raspbian.raspberrypi.org'
E: Failed to fetch http://raspbian.raspberrypi.org/raspbian/pool/main/d/dnsmasq/dnsmasq_2.85-1_all.deb  Temporary failure resolving 'raspbian.raspberrypi.org'
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
mq-sol commented 2 years ago

スタンドアロンだったらdnsは無くてもいいよ。 hostapdとdhcpdを止めてから、入れなおしてみて

ShogoHirasawa commented 2 years ago

SSID: dronebirdとしてアクセスポイント化は成功。しかし、パスワードを打ってもWi-Fiに接続しない。跳ね返されてしまう

スクリーンショット 2021-12-21 11 48 34 スクリーンショット 2021-12-21 11 48 42
mq-sol commented 2 years ago

Raspbian

IPアドレスの設定

sudo systemctl restart network

wlanをインターネットにつなげる。(確認する)

ping ntp.nict.jp

パッケージをインストールする

sudo apt update -y
sudo apt install -y apache2 hostapd dnsmasq
sudo apt upgrade -y
sudo apt autoremove -y

DHCP

デーモンを停止する

sudo systemctl stop dhcpcd

confの修正 (末尾に追記する。)

sudo echo "interface wlan0" >> /etc/dhcpcd.conf
sudo echo "static ip_address=192.168.20.1/24" >> /etc/dhcpcd.conf

hostapd

Confの修正

cat << _EOF_ | sudo tee /etc/hostapd/hostapd.conf
interface=wlan0
driver=nl80211
hw_mode=b
channel=1
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
ieee80211ac=0
wmm_enabled=1
ieee80211d=1
country_code=JP
ieee80211h=1
local_pwr_constraint=3
spectrum_mgmt_required=1
wpa=3
wpa_key_mgmt=WPA-PSK
ssid=dronebird
wpa_passphrase=dronebird
_EOF_

sudo sed -i -e "s@#DAEMON_CONF=\"\"@DAEMON_CONF=\"/etc/hostapd/hostapd.conf\"@g" /etc/default/hostapd

起動設定

sudo systemctl status hostapd
sudo systemctl unmask hostapd
sudo systemctl enable hostapd

DNSMSQ

停止

sudo systemctl stop dnsmasq

Confの編集

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.def

cat << _EOF_ | sudo tee /etc/dnsmasq.conf
interface=wlan0
dhcp-range=192.168.20.2,192.168.20.100,255.255.255.0,24h
_EOF_

デーモンの起動

sudo systemctl restart dhcpcd
sudo systemctl restart hostapd
sudo systemctl restart dnsmasq

設定のバックアップ

動作確認

ShogoHirasawa commented 2 years ago

@mq-sol さんの指示通りに実行してみる。 まずは、Mac側のIPの固定化

スクリーンショット 2021-12-21 19 17 16
ShogoHirasawa commented 2 years ago

ラズパイの有線のIP固定化

スクリーンショット 2021-12-21 19 20 09
ShogoHirasawa commented 2 years ago

restartができない

pi@dronebird4:~ $ sudo systemctl restart network
Failed to restart network.service: Unit network.service not found.
ShogoHirasawa commented 2 years ago

代わりに以下のコマンドを実行

sudo ifconfig eth0 down
sudo ifconfig eth0 up
ShogoHirasawa commented 2 years ago

sudo ifconfig eth0 down実行後固まる。

スクリーンショット 2021-12-21 19 23 56
ShogoHirasawa commented 2 years ago

その後、ssh接続もできなくなったので、OS再インストール

スクリーンショット 2021-12-21 19 25 14
mq-sol commented 2 years ago

RaspberryPIの固定IPは以下の方法を試してください

cat << _EOF_ | sudo tee -a /etc/dhcpcd.conf
interface eth0
static ip_address=192.168.10.1/24
_EOF_

sudo systemctl restart dhcpcd
ShogoHirasawa commented 2 years ago

ありがとうございます。一度、この方法で試してみますm( )m

ShogoHirasawa commented 2 years ago

上記の指示どおりにやったら、IPの固定化成功。 IP固定化後、この通りにやったが、Wi-Fiの一覧にdronebirdの文字は現れず....。

スクリーンショット 2022-01-01 8 57 02

IMG_110AACF1126E-1

ShogoHirasawa commented 2 years ago

以下のコマンドを再度打ち込んだら、表示された

sudo systemctl restart dhcpcd
sudo systemctl restart hostapd
sudo systemctl restart dnsmasq

IMG_D9ECE1FD0546-1

ShogoHirasawa commented 2 years ago

無事、接続を確認

スクリーンショット 2022-01-01 9 06 39
ShogoHirasawa commented 2 years ago

上記の作業でうまく行かなかったときはここも参考になると思う https://dekuo-03.hatenablog.jp/entry/2020/06/08/225915