sudo apt install hostapd
ssid
以改變名稱,填寫wpa_passphrase
以改變密碼# wireless interface name
interface=wlan0
ssid=<AP's name you want>
# IEEE 802.11g (2.4 GHz) with 7 channels
hw_mode=g
channel=7
# cipher protocol
wpa=2
auth_algs=1
wpa_pairwise=CCMP
wpa_key_mgmt=WPA-PSK
wpa_passphrase=<password you want>
# create hostapd.conf and fill in the above content
sudo hostapd hostapd.conf
在執行完上面步驟後,打開手機便可找到與你設定的SSID的存取點。
在輸入正確密碼後會發現和上圖一樣卡在取得IP位址的地方
獲得了IP後會發現裝置仍然沒有連上網際網路,這是因為樹莓派預設沒有執行封包轉送。
ip_forward
來達到這像功能sysctl net.ipv4.ip_forward
確認當前ip_forward
是否啟動sudo sysctl net.ipv4.ip_forward=1
可以啟動ip_forward
sysctl
藉由讀寫/proc/sys/net/ipv4/ip_forward
內容來改變Linux核心設定封包路由轉送的規則是透過在封包中的IP來決定, 目前我們使用的無線存取點的子網域是自己定義的私有IP, 這個IP是連上網際網路的路由器所不認得的。 因此當外界要回傳封包時,根據IP並無法將封包回傳。
因此除了設定ip_forward
之外,還要設定網路位址轉換
網路位址轉換可以透過iptables
工具來進行。
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
在完成以上步驟之後可以藉由在Google搜尋my ip
,確認IP是和樹莓派的相符則設定成功。
dhcpd
支援動態分配IP的服務sudo apt install isc-dhcp-server
option domain-name-servers 8.8.8.8, 8.8.4.4;
option subnet-mask 255.255.255.0;
option routers 192.168.20.1;
subnet 192.168.20.0 netmask 255.255.255.0 {
range 192.168.20.10 192.168.20.20;
}
# create dhcpd.conf and fill in the above content
sudo dhcpd -cf dhcpd.conf