知乎專欄 | 多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者” |
pi@raspberrypi:~ $ networkctl IDX LINK TYPE OPERATIONAL SETUP 1 lo loopback carrier unmanaged 2 eth0 ether routable unmanaged 3 wlan0 wlan no-carrier unmanaged 3 links listed. pi@raspberrypi:~ $ networkctl status ● State: routable Address: 192.168.0.147 on eth0 fe80::108e:eede:2340:564e on eth0 Gateway: 192.168.0.1 (Tenda Technology Co.,Ltd.Dongguan branch) on eth0
自動連接區域內WIFI sudo vi /etc/wpa_supplicant/wpa_supplicant.conf 在檔案的末尾添加WIFI網絡的名稱以及密碼,將要連接的wifi名稱和密碼替換即可。 network={ ssid="SSID" psk="wifi_password" } 使用sudo wpa_cli reconfigure命令啟動連接 pi@raspberrypi:~ $ sudo wpa_cli reconfigure Selected interface 'wlan0' OK
準備樹莓派 Raspberry Pi 3 B+
eth0 接入本地網絡 wlan0 做WiFi熱點
首先安裝必須的軟件,dnsmasq 是 DNS 域名解析服務。udhcpd 是 DHCP 服務,主要功能是為熱點動態分配 IP地址。hostapd 是熱點服務
sudo apt upgrade sudo apt install dnsmasq hostapd udhcpd
在 /etc/network/interfaces.d/ 目錄中創建 wlan0 檔案
sudo vim /etc/network/interfaces.d/wlan0 auto wlan0 iface wlan0 inet static address 172.16.0.254 netmask 255.255.255.0
啟用 DHCP
sudo vim /etc/default/udhcpd DHCPD_ENABLED="no" 改為 DHCPD_ENABLED="yes"
配置DHCP分配IP地址範圍
sudo cp /etc/udhcpd.conf{,.original} sudo vim /etc/udhcpd.conf start 172.16.0.20 end 172.16.0.200 interface wlan0 opt dns 172.16.0.254 #opt dns 8.8.8.8 4.4.4.4 option subnet 255.255.255.0 opt router 172.16.0.254 opt wins 172.16.0.254 option dns 114.114.114.114 option domain local option lease 864000 # 10 days of seconds
start和end是分配IP的起始與結束範圍,interface wlan0 是指定 wlan0 介面廣播DHCP,這樣不會影響 eth0, 注意分配地址必須與wlan0在同一個網段。
opt dns 8.8.8.8 4.4.4.4 使用Google的DNS,如果希望使用 DNSMASQ 需要設置為 172.16.0.254
如果使用 dnsmasq 解析域名,上面的DHCP需要配置 opt dns 172.16.0.254,這樣DHCP分配地址的時候DNS被設置為 172.16.0.254。
sudo cp /etc/dnsmasq.conf{,.original} sudo vim /etc/dnsmasq.conf interface=wlan0 bind-interfaces server=8.8.8.8 server=4.4.4.4 server=114.114.114.114 domain-needed bogus-priv dhcp-range=172.16.0.20,172.16.0.200,12h
sudo vim /etc/default/hostapd 找到 #DAEMON_CONF= "" 修改為: DAEMON_CONF="/etc/hostapd/hostapd.conf"
創建 /etc/hostapd/hostapd.conf 配置檔案
sudo vim /etc/hostapd/hostapd.conf # Basic configuration interface=wlan0 ssid=netkiller channel=1 #bridge=br0 # WPA and WPA2 configuration macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=3 wpa_passphrase=13113668890 wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP # Hardware configuration wmm_enabled=1
開啟ipv4轉發
sudo vim /etc/sysctl.conf # Uncomment the next line to enable packet forwarding for IPv4 net.ipv4.ip_forward=1
轉發規則
sudo iptables -F sudo iptables -X sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo bash -c iptables-save > /etc/iptables.up.rules sudo vim /etc/network/if-pre-up.d/iptables #!/bin/bash /sbin/iptables-restore < /etc/iptables.up.rules sudo chmod 755 /etc/network/if-pre-up.d/iptables
sudo systemctl restart networking sudo systemctl restart udhcpd sudo systemctl restart dnsmasq sudo systemctl restart hostapd
如果 hostapd 啟動失敗,可以運行下面命令調試
sudo hostapd -d /etc/hostapd/hostapd.conf
日誌
$ cat /var/log/syslog | egrep "hostapd|dhcpcd"
執行 iptable 提示如下
pi@raspberrypi:~ $ sudo iptables -L iptables v1.6.0: can't initialize iptables table `filter': Table does not exist (do you need to insmod?) Perhaps iptables or your kernel needs to be upgraded.
解決方案
pi@raspberrypi:~ $ sudo rpi-update