將樹梅派當成Wifi路由器(Raspberry Pi Become a Wifi Router)
本文介紹如何將樹莓派變成一個Wifi路由器。
步驟一:Install the router software
sudo apt-get install isc-dhcp-server
上圖是顯示DHCP server啟動失敗,不會影響後續的安裝與設定。
步驟二:Configure the ISC-DHCP-Server
執行底下指令來修改 ISC-DHCP Server的設定檔,當然可以使用其他的文字編輯器來操作:
sudo nano /etc/dhcp/dhcpd.conf
註解底下兩行:
option domain-name “example.org”;
option domain-name-servers ns1.example.org, ns2.example.org;
修改/etc/default/isc-dhcp-server
sudo nano /etc/default/isc-dhcp-server
#auto wlan0
#allow-hotplug wlan0
#iface wlan0 inet manual
#wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
up iptables-restore < /etc/iptables.ipv4.nat
修改ssid與password
Change the parameter “” to the SSID wifi name you want
and the parameter “wpa_passphrase=YourPassPhrase “ to your own password.
將下面這一行加到檔案的結尾處:
net.ipv4.ip_forward=1
將ip forward的功能啟用:
sudo sh -c “echo 1 > /proc/sys/net/ipv4/ip_forward”
設定ethernet (eth0) 與wireless card (wlan0) 之間的 port 轉換:
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 service isc-dhcp-server start
sudo service hostapd start
開機自動啟動Wifi Router:
sudo update-rc.d hostapd enable
sudo update-rc.d isc-dhcp-server enable
sudo sh -c “iptables-save > /etc/iptables.ipv4.nat”
若出現「update-rc.d: error: no runlevel symlinks to modify, aborting!」時請執行底下指令
sudo ln -s /etc/init.d/hostapd /etc/rc2.d/S02hostapd
sudo update-rc.d hostapd enable
sudo update-rc.d isc-dhcp-server enable
sudo sh -c “iptables-save > /etc/iptables.ipv4.nat”
註:可使用底下指令來還原網路設定。
up iptables-restore < /etc/iptables.ipv4.nat
此時即可執行 sudo reboot 來重開 Raspberry Pi做測試。
Originally published at https://yunlinsong.blogspot.com.