[Raspberry Pi] 設定Raspberry Pi成為Wi-Fi基地台(wifi ap)


基本上LINUX中,設定Wi-Fi基地台(wifi ap)分享網路需要dhcp server和hostap兩套件軟體,還需要設定iptables和開啟ipforward功能就可以了。因此,在Raspberry Pi中也是同樣的道理,開始動工吧!!


Raspberry Pi - Wi-Fi基地台(WiFi AP)安裝與設定

Step1. 安裝udhcp和hostapd套件 (開啟終端機)
           $sudo apt-get update
           $sudo apt-get install udhcpd
           $sudo apt-get install hostapd



Step2. 編輯/etc/network/interface,加入以下內容

allow-hotplug wlan0
auto wlan0

iface wlan0 inet static
address 100.101.102.1
netmask 255.255.255.0
gateway 100.101.102.254


Step3. 編輯hostapd.conf (Wi-Fi AP)設定檔,路徑/etc/hostapd/hostapd.conf,寫入以下內容

interface=wlan0
driver=nl80211
ssid=RpiWiFi_ap
hw_mode=g
channel=8
wpa=1
wpa_passphrase=(填入密碼)
wpa_key_mgmt=WPA-PSK


Step4. 編輯udhcpd.conf(DHCP Server)設定檔,路徑/etc/udhcpd.conf,修改以下內容

# Sample udhcpd configuration file (/etc/udhcpd.conf)

# The start and end of the IP lease block

start           100.101.102.50  #default: 192.168.0.20
end            100.101.102.70  #default: 192.168.0.254

# The interface that udhcpd will use
interface       wlan0 
     .
     .
     .
#Examles
opt     dns     168.95.1.1 8.8.8.8
option  subnet  255.255.255.0
opt     router  100.101.102.1
opt     wins    100.101.102.1
#option dns     129.219.13.81   # appened to above DNS servers for a total of 3
option  domain  local
option  lease   864000          # 10 days of seconds


Step5. 編輯預設執行hostapd,路徑/etc/default/hostapd
           將     DAEMON_CONF=""
           改為 DAEMON_CONF="/etc/hostapd/hostapd.conf",存檔離開


Step6. 編輯預設執行udhcpd,路徑/etc/default/udhcpd
           將     DHCPD_ENABLED="no"
           改為 DHCPD_ENABLED="yes",存檔離開


Step7. 編輯sysctl.conf設定檔,開啟ip forward 轉發,路徑/etc/sysctl.conf
           將    #net.ipv4.ip_forward=1
           移除註解 #
           改為 net.ipv4.ip_forward=1,存檔離開

           立刻執行生效,可輸入指令
            $sudo sysctl -p /etc/sysctl.conf


Step8. 新增或編輯iptables,儲存檔案於路徑/etc/init.d

#!/bin/bash
iptables -F

iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT


iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i eth0  -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -i wlan0 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 51413 -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -s 100.101.102.0/24 -j MASQUERADE

為檔案增加執行權限
$sudo chmod +x iptables.sh

Step9. 設定開機執行
    $sudo update-rc.d iptables defaults

設定完成後,重新將Rpi開機即可以使用。


留言

這個網誌中的熱門文章

[Ubuntu] 幫 Ubuntu 更換 Kernel

[Ubuntu] ModelSim Linux 安裝