基礎設定 先將樹莓派連上有線網路,並將系統進行更新
1 2 sudo apt-get update sudo apt-get upgrade
安裝 Hostapd、ISC DHCP Server 之套件
1 sudo apt-get install hostapd isc-dhcp-server
編輯 Netpan 設定檔如下面範例所示(/etc/netplan/設定檔.yaml),下例以 192.168.10.1 作爲 dhcp4 的網關地址。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 network: version: 2 ethernets: wlan0: dhcp4: no addresses: - 192.168 .10 .1 /24 optional: true eth0: dhcp4: true nameservers: addresses: [127.0 .0 .1 ] search: [] optional: true
註:eth0 設定 127.0.0.1 爲使用 Adguard Home 作爲 DNS 伺服器才使用。
編輯 /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg,去除 cloud init 的配置干擾
1 sudo nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
內容如下
1 network: {config: disabled}
完成設定後,測試設定檔是否有錯誤
沒有錯誤即可正式使用該配置
2.4 GHz WiFi 設定 編輯 hostapd 設定檔
1 sudo nano /etc/hostapd/hostapd.conf
內容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 interface=wlan0 driver=nl80211 ssid=想要設定的 WiFi 的 SSID hw_mode=g channel=11 # WiFi 信道 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=WiFi的密碼 wpa_key_mgmt=WPA-PSK #wpa_pairwise=TKIP rsn_pairwise=CCMP ieee80211n=1 wmm_enabled=0
編輯完成後,啓動 Hostapd 服務
1 2 3 sudo systemctl unmask hostapd sudo systemctl enable hostapd sudo systemctl start hostapd
重新啓動系統
5 GHz WiFi 設定 先安裝 iw 套件
編輯 /etc/default/crda
1 sudo nano /etc/default/crda
內容如下
再執行
編輯 hostapd 設定檔
1 sudo nano /etc/hostapd/hostapd.conf
內容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ssid=想要設定的 WiFi 的 SSID wpa_passphrase=WiFi的密碼 country_code=US interface=wlan0 driver=nl80211 wpa=2 wpa_key_mgmt=WPA-PSK rsn_pairwise=CCMP macaddr_acl=0 logger_syslog=0 logger_syslog_level=4 logger_stdout=-1 logger_stdout_level=0 hw_mode=a wmm_enabled=1 # N ieee80211n=1 require_ht=1 ht_capab=[MAX-AMSDU-3839][HT40+][SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40] # AC ieee80211ac=1 require_vht=1 ieee80211d=0 ieee80211h=0 vht_capab=[MAX-AMSDU-3839][SHORT-GI-80] vht_oper_chwidth=1 channel=149 vht_oper_centr_freq_seg0_idx=155
編輯完成後,啓動 Hostapd 服務
1 2 3 sudo systemctl unmask hostapd sudo systemctl enable hostapd sudo systemctl start hostapd
重新啓動系統
DHCP 伺服器架設與配置 編輯 /etc/default/isc-dhcp-server
1 sudo nano /etc/default/isc-dhcp-server
並更改下行之值,讓 DHCP Server bind 到用作分享 WiFi 的界面
編輯 /etc/dhcp/dhcpd.conf
1 sudo nano /etc/dhcp/dhcpd.conf
內容如下,注意因爲我先前已經先設置 AdGuard Home 作爲 DNS 伺服器,並監聽 0.0.0.0:53,因此 option domain-name-servers
填寫本機地址(192.168.10.1),不然可用如 8.8.8.8 等 Google 或其他 DNS 伺服器。
1 2 3 4 5 6 7 8 9 10 11 12 13 option domain-name "隨意填寫"; option domain-name-servers 192.168.10.1; default-lease-time 600; max-lease-time 7200; ddns-update-style none; authoritative; log-facility local7; subnet 192.168.10.0 netmask 255.255.255.0 { range 192.168.10.101 192.168.10.200; option subnet-mask 255.255.255.0; option routers 192.168.10.1; option broadcast-address 192.168.10.255; }
啓動 DHCP 伺服器,並查看啓用狀態
1 2 3 sudo systemctl restart isc-dhcp-server sudo systemctl enable isc-dhcp-server sudo systemctl status isc-dhcp-server
利用 Iptables 進行 forwarding 編輯 /etc/ufw/sysctl.conf,開啓 ip forward
1 sudo nano /etc/ufw/sysctl.conf
將此行前面的 #
註解拿掉
開啓 UFW 防火牆(若使用 SSH 則要注意是否有開啓 SSH 的連接埠,以免被鎖在外面)
新建 /etc/rc.local
內容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #!/bin/bash # /etc/rc.local # Default policy to drop all incoming packets. iptables -P INPUT DROP iptables -P FORWARD DROP # Accept incoming packets from localhost and the LAN interface. iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -i wlan0 -j ACCEPT # Accept incoming packets from the WAN if the router initiated the connection. iptables -A INPUT -i eth0 -m conntrack \ --ctstate ESTABLISHED,RELATED -j ACCEPT # Forward LAN packets to the WAN. iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT # Forward WAN packets to the LAN if the LAN initiated the connection. iptables -A FORWARD -i eth0 -o wlan0 -m conntrack \ --ctstate ESTABLISHED,RELATED -j ACCEPT # NAT traffic going out the WAN interface. iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE # rc.local needs to exit with 0 exit 0
註:若不想要連接 WiFi 的客戶端存取伺服器上所有服務或端口,則註解此句 iptables -A INPUT -i wlan0 -j ACCEPT
設置正確權限
1 sudo chmod 755 /etc/rc.local
重新啓動應可在其他設備找到自己的 WiFi 熱點,並連接至網路
參考資料 How To: Build a Simple Router with Ubuntu Server 18.04.1 LTS (Bionic Beaver)
RPi 4 running Ubuntu Server 20.04: can’t connect to WiFi