# 設定無線基地台(AP)

## 所需套件

從安裝所需的套件開始。

```
$ sudo apt-get install hostapd dnsmasq
```

## 設定網路介面

為了防止樹莓派的Wi-Fi介面重新向DHCP索取動態IP位址，與原有的固定IP位址發生衝突，需修正/etc/dhcpcd.conf的檔案內容。在檔案結尾處加入此行。

```
denyinterfaces wlan0
```

接著修改/etc/network/interfaces檔案中的Wi-Fi介面設定，設定成靜態的固定IP位址。我們選擇 10.0.0.1作為MoodleBox的地址。

這個檔案的wlan0部份，看起來像這樣：

```
allow-hotplug wlan0
iface wlan0 inet static
    address 10.0.0.1
    netmask 255.255.255.0
    network 10.0.0.0
    broadcast 10.0.0.255
#  wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
```

檔案的其他部分不需要修改。

## 設定基地台(AP，hostapd)

接下來修正hostapd.conf檔案，在此訂定Wi-Fi網路連線名稱 (SSID)、連線密碼和其他選項，例如廣播通道。 網路連線名稱是 MoodleBox 密碼為 moodlebox。

/etc/hostapd/hostapd.conf檔案內容：

```
# Set country code
country_code=TW
# Name of the Wi-Fi interface
interface=wlan0
# Use the nl80211 driver
driver=nl80211
# Wi-Fi network name
ssid=MoodleBox
# Use the 2.4GHz band
hw_mode=g
# Use channel 11
channel=11
# Enable 802.11n
ieee80211n=1
# Enable WMM
wmm_enabled=1
# Enable 40 MHz channels with short guard interval for 20 Mhz
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
# Accept all MAC addresses
macaddr_acl=0
# Use WPA authentication
auth_algs=1
# Use WPA2
wpa=2
# Use a pre-shared key
wpa_key_mgmt=WPA-PSK
# The network passphrase
wpa_passphrase=moodlebox
# Use AES, instead of TKIP
rsn_pairwise=CCMP
# Enable hostapd_cli
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
```

***驗證***

```
啟動 hostapd：
$ sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf
會有錯誤顯示(因為設定還沒有完成)，但是會自動由 wlan0:AP-ENABLED 終止。就可以掃瞄到 MoodleBox 的Wi-Fi網路連線了。
```

接著要定義 hostapd所需的啟動設定，即修改/etc/default/hostapd檔案。

用 DAEMON\_CONF="/etc/hostapd/hostapd.conf" 取代 #DAEMON\_CONF=""。

## 設定DHCP伺服器和DNS伺服器

修改 /etc/dnsmasq.conf 檔案 。 內容如下：

```
interface=wlan0         # Use interface wlan0
listen-address=127.0.0.1     # Explicitly specify the address to listen on
listen-address=10.0.0.1     # Explicitly specify the address to listen on
bind-interfaces             # Make sure we aren't sending things elsewhere
server=209.244.0.3         # Forward DNS requests to Level3 DNS
server=209.244.0.4         # Forward DNS requests to Level3 DNS
domain-needed             # Don't forward short names
bogus-priv             # Don't forward addresses in the non-routed spaces
domain=home             # Set private domain name to 'home'
local=/home/             # Don't forward queries for private domain 'home'
expand-hosts             # Add private domain name to hostnames
dhcp-range=wifi,10.0.0.100,10.0.0.199,255.255.255.0,12h # Assign IP addresses with12h lease, subnet name 'wifi'
dhcp-option=wifi,6,10.0.0.1 # Set DNS server for subnet wifi
# log-facility=/var/log/dnsmasq.log # Enable log
```

然後修改 /etc/hosts檔案，更換最後一行，由127.0.0.1開頭，取代為下面的內容：

```
10.0.0.1  moodlebox
```

此設定允許任何類型的設備，即使是那些不屬於zeroconf的技術科技，也可以使用地址 <http://moodlebox.home/> 存取MoodleBox。

## 路由配置

修改 /etc/sysctl.conf檔案，取消註釋（或增加）此行

```
net.ipv4.ip_forward=1
```

在介面中加入這些行:

```
$ 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 sh -c "iptables-save > /etc/iptables.ipv4.nat"
$ sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
```

接著，新建一個檔案 /lib/dhcpcd/dhcpcd-hooks/70-ipv4-nat。它的內容如下:

```
iptables-restore < /etc/iptables.ipv4.nat
```

最後，重新啟動樹莓派即完成。

```
$ sudo reboot
```

## 測試無線基地台

***驗證***

```
在啟動樹莓派時，客戶端應該可以透過連線名稱為MoodleBox、密碼moodlebox的Wi-Fi基地台連上網際網路。
```

## mDNS服務

為了在網路上可以看見MoodleBox提供的網路服務，新建/etc/avahi/services/moodlebox.service 檔案，內容如下：

```
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">%h</name>
    <service>
        <type>_device-info._tcp</type>
        <port>0</port>
        <txt-record>model=MoodleBox</txt-record>
    </service>
    <service>
        <type>_ssh._tcp</type>
        <port>22</port>
    </service>
    <service>
        <type>_sftp-ssh._tcp</type>
        <port>22</port>
    </service>
    <service>
        <type>_http._tcp</type>
        <port>80</port>
    </service>
</service-group>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gxiangco.gitbook.io/moodlebox-on-raspberry-pi-3/chapter4.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
