はじめに
Raspberry Pi 4BをWi-Fiルーターにする覚書です。
OSはBusterのみ対応、Bullseyeで動くUSBドングルとWi-Fiルーター設定教えてください。
eth0 ⇒ wlan0ではなくて、wlan1 ⇒ wlan0な感じでルーティングします。
wlan1はWi-Fi USBドングルです。AC接続できる、RTL8812bUの入ったUSBドングルを使いますので、そのドライバインストールからです。
Wi-Fi USBドングルのドライバインストール
sudo apt update
sudo apt install -y bc git dkms build-essential raspberrypi-kernel-headers
git clone -b v5.6.1 https://github.com/fastoe/RTL8812BU_for_Raspbian
cd RTL8812BU_for_Raspbian
# sudo apt install -y bc git flex bison libssl-dev libncurses5-dev
# sudo wget https://raw.githubusercontent.com/RPi-Distro/rpi-source/master/rpi-source -O /usr/local/bin/rpi-source && sudo chmod +x /usr/local/bin/rpi-source && /usr/local/bin/rpi-source -q --tag-update
# rpi-source
make
sudo make install
ここまでやってリブートします。(コメントアウト部は環境によって必要だったり邪魔だったり。無しでやってダメなら入れて再トライ
Raspberry Piの設定
https://www.raspberrypi.com/documentation/computers/configuration.html
https://www.itmedia.co.jp/news/articles/2008/14/news042.html
sudo apt install hostapd
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo apt install dnsmasq
sudo DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent
dhcpcd.confの編集
sudo nano /etc/dhcpcd.conf
以下の行を追記
IPアドレス指定は自分の環境に合わせて
interface wlan0
static ip_address=192.168.4.1/24
nohook wpa_supplicant
interface wlan1
static ip_address=192.168.1.***/24
static routers=192.168.1.***
static domain_name_servers=192.168.1.***
wlan1の接続先APをwpa_supplicant.confに書き込む
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1
country=JP
network={
ssid="SSID"
psk="パスワード"
key_mgmt=WPA-PSK
}
ルーティングの設定ファイルを作成
sudo nano /etc/sysctl.d/routed-ap.conf
# Enable IPv4 routing
net.ipv4.ip_forward=1
sudo iptables -t nat -A POSTROUTING -o wlan1 -j MASQUERADE
sudo netfilter-persistent save
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo nano /etc/dnsmasq.conf
interface=wlan0 # Listening interface
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
# Pool of IP addresses served via DHCP
domain=wlan # Local wireless DNS domain
address=/gw.wlan/192.168.4.1
# Alias for this router
sudo rfkill unblock wlan
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=****
wpa_passphrase=********* #パスワードは8文字以64文字以下
sudo nano /etc/sysctl.conf
としてファイルを表示されたら、以下の行の先頭にある「#」を削除します。
net.ipv4.ip_forward=1
sudo nano /etc/default/hostapd
追記
DAEMON_CONF="/etc/hostapd/hostapd.conf"
sudo nano /etc/rc.local
「exit 0」とある行の前に、以下の内容を追記します。
iptables-restore < /etc/iptables.ipv4.nat
sudo systemctl reboot
さらにポートフォワーディングする
https://iairu.com/en/dev/iptables-portforward/
https://qiita.com/ponsuke0531/items/6b6255c0402e6ea4a950
https://blnlabs.com/redirect-network-traffic-using-raspberry-pi-in-10-min-2/
現在の
sudo iptables -t nat -L
sudo iptables -t nat -I PREROUTING 1 -p tcp --dport 8001 -j DNAT --to-destination 192.168.4.2:808
消すとき
iptables -t nat -D CHAIN N