# 樹莓派Wifi基地台
****目的:使用樹莓派將實體乙太網路打造成wifi ap分享器****
**準備材料:**
1. 樹莓派3 x1
2. 網路線 x1
3. PL2303(usb to uart)
4. SD x1(8g以上)
**操作環境:**
ubuntu 18.04.2 LTS
## 格式化SD卡
**使用fdisk指令**
>1. fdisk /dev/sdX
**X是SD卡裝置**
> 2. 之後使用**d**刪除分割區以及使用**n**建立你想要的切割區
## 燒錄作業系統raspbian到SD卡上
燒錄檔版本: 2020-02-13-raspbian-buster
**問題1:** 在'sudo apt-get update -y'的時候可能會遇到更新網址過期問題可用以下指令解決
>sudo apt-get -y update --allow-releaseinfo-change
**問題2:** 出現以下訊息需注意電源線和電源是否供電足夠
>>Voltage normalised (0x00000000)
1. 從觀網上下載raspbian的映像擋
2. 使用命令**dd**將映像擋燒錄到SD卡上
> dd status=progress bs=4M conv=sync if=$place_to_image/raspbian.img of=/dev/sdX
3. 成功之後將SD卡拔掉重新插入之後系統如果顯示**boot**和**rootfs**兩個資料夾則表示燒錄成功
## 藉由uart連接電腦和樹莓派
1. 須修改**boot**資料夾裡面的**cmdline.txt**和**config.txt**兩個檔案
cmdline.txt: 刪除檔案裡面的quiet字體
config.txt:加入以下三行
>dtoverlay=pi3-miniuart-bt
core_freq=250
enable_uart=1
2. 硬體部份會使用PL2303連接樹莓派和電腦
> **樹莓派腳位**
![](https://i.imgur.com/05s66VS.png)
> **PL2303**
> PL2303使用上有四條片分別是
> 1. 5v電源(紅色)
> 2. GND(黑色)
> 3. TX(綠色)
> 4. RX(白色)
![](https://i.imgur.com/ZEWIUUL.png)
3. 腳位連接
**PL2303---樹莓派**
3.1 GND(黑色)---pin6,pin9,pin14,pin20,pin25,pin30,pin34,pin34
3.2 TX(綠色)---RX(pin10)
3.3 RX(白色)---TX(pin8)
4. 通訊軟體接
使用putty連接,設定參數如下:
![](https://i.imgur.com/89sbVgx.png)
之後上電之後putty會開始跑出開機訊息,最後要輸入預設的帳號密碼
![](https://i.imgur.com/6DES7qw.png)
> 帳號:pi
> 密碼:raspberry
## 打造樹莓派wifi分享器
****連接內部wifi網路(wlan0)和外部網路(eth0)的橋接器(樹莓派)****
1. 設定乙太網路
1.1 接上網路線
2.1 設定/etc/network/interfaces
> auto eth0
iface eth0 inet static
address 192.168.188.40
network 255.255.255.0
broadcast 192.168.188.255
gateway 192.168.188.254
dns-nameservers 192.168.188.254 168.95.192.1 168.95.1.1
>auto wlan0
iface wlan0 inet static
address 192.168.2.1
netmask 255.255.255.0
2.2 使用ifconfig和ifup指令來設定eth0界面
> ifup eth0
ifconfig eth0 down
ifconfig eth0 up
2.3 使用ifconfig 來看IP是否設定成功
> ifconfig eth0
>
2.4 重啟網路服務:
> /etc/init.d/networking restart.
3. DHCP 伺服器
在內部的wifi網路裡面需要一個DHCP伺服器來動態分配IP
3.1 下載一個DHCP 伺服器-isc-dhcp-server
> apt-get install isc-dhcp-server
>
3.2 修改/etc/dhcp/dhcpd.conf,以下是我修改後的檔案
```
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;
default-lease-time 600;
max-lease-time 7200;
# The ddns-updates-style parameter controls whether or not the server will
# attempt to do a DNS update when a lease is confirmed. We default to the
# behavior of the version 2 packages ('none', since DHCP v2 didn't
# have support for DDNS.)
ddns-update-style none;
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;
# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
#log-facility local7;
# No service will be given on this subnet, but declaring it helps the
# DHCP server to understand the network topology.
#subnet 10.152.187.0 netmask 255.255.255.0 {
#}
# This is a very basic subnet declaration.
#subnet 10.254.239.0 netmask 255.255.255.224 {
# range 10.254.239.10 10.254.239.20;
# option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
#}
# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.
#subnet 10.254.239.32 netmask 255.255.255.224 {
# range dynamic-bootp 10.254.239.40 10.254.239.60;
# option broadcast-address 10.254.239.31;
# option routers rtr-239-32-1.example.org;
#}
# A slightly different configuration for an internal subnet.
#subnet 10.5.5.0 netmask 255.255.255.224 {
# range 10.5.5.26 10.5.5.30;
# option domain-name-servers ns1.internal.example.org;
# option domain-name "internal.example.org";
# option routers 10.5.5.1;
# option broadcast-address 10.5.5.31;
# default-lease-time 600;
# max-lease-time 7200;
#}
subnet 192.168.2.0 netmask 255.255.255.0 {
range 192.168.2.10 192.168.2.50;
option domain-name-servers 168.95.192.1, 168.95.1.1 ;
option domain-name "local";
option routers 192.168.2.1;
option broadcast-address 192.168.188.255;
default-lease-time 600;
max-lease-time 7200;
}
# Hosts which require special configuration options can be listed in
# host statements. If no address is specified, the address will be
# allocated dynamically (if possible), but the host-specific information
# will still come from the host declaration.
#host passacaglia {
# hardware ethernet 0:0:c0:5d:bd:95;
# filename "vmunix.passacaglia";
# server-name "toccata.example.com";
#}
# Fixed IP addresses can also be specified for hosts. These addresses
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot using
# BOOTP or DHCP. Hosts for which no fixed address is specified can only
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp flag
# set.
#host fantasia {
# hardware ethernet 08:00:07:26:c0:a5;
# fixed-address fantasia.example.com;
#}
# You can declare a class of clients and then do address allocation
# based on that. The example below shows a case where all clients
# in a certain class get addresses on the 10.17.224/24 subnet, and all
# other clients get addresses on the 10.0.29/24 subnet.
#class "foo" {
# match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
#}
#shared-network 224-29 {
# subnet 10.17.224.0 netmask 255.255.255.0 {
# option routers rtr-224.example.org;
# }
# subnet 10.0.29.0 netmask 255.255.255.0 {
# option routers rtr-29.example.org;
# }
# pool {
# allow members of "foo";
# range 10.17.224.10 10.17.224.250;
# }
# pool {
# deny members of "foo";
# range 10.0.29.10 10.0.29.230;
# }
#}
```
3.3 修改/etc/default/isc-dhcp-server,以下是我修改後的檔案
```
# Defaults for isc-dhcp-server (sourced by /etc/init.d/isc-dhcp-server)
# Path to dhcpd's config file (default: /etc/dhcp/dhcpd.conf).
#DHCPDv4_CONF=/etc/dhcp/dhcpd.conf
#DHCPDv6_CONF=/etc/dhcp/dhcpd6.conf
# Path to dhcpd's PID file (default: /var/run/dhcpd.pid).
#DHCPDv4_PID=/var/run/dhcpd.pid
#DHCPDv6_PID=/var/run/dhcpd6.pid
# Additional options to start dhcpd with.
# Don't use options -cf or -pf here; use DHCPD_CONF/ DHCPD_PID instead
#OPTIONS=""
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACESv4="wlan0"
INTERFACESv6="wlan0"
```
3.4 重新啟動DHCP服務
>service isc-dhcp-server restart
4. 封包轉送
****將內部網路(wlan0)的封包外部網路(eth0)之間轉送****
4.1 修改/etc/sysctl.conf裡的net.ipv4.ip_forward
>net.ipv4.ip_forward=1
4.2 使用下列指令是封包轉送馬上生效
>sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
4.3 設定 NAT,下以下指令
>iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
>iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
>iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
>
4.4 為了讓NAT在每次啟動時候都可以自動執行須下以下指令
>sh -c "iptables-save > /etc/iptables.ipv4.nat"
以及在/etc/network/interfaces,加入:
>up iptables-restore < /etc/iptables.ipv4.nat
5. hostapd 服務
****需透過hostapd這個軟體將樹莓派本身的無線網卡設定成一個AP(access point)****
5.1 下載hostapd
> apt-get install hostapd
5.2 參考/usr/share/doc/hostapd/examples/hostapd.conf.gz 建立/etc/hostapd/hostapd.conf 設定檔
修改部份可以參考****參考資料1****
5.3 修改/etc/default/hostapd設定檔使 DAEMON_CONF 指向 /etc/hostapd/hostapd.conf
> DAEMON_CONF="/etc/hostapd/hostapd.conf"
5.4 測試hostapd
> hostapd /etc/hostapd/hostapd.conf
>
5.5 /etc/rc.local
```
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
sudo hostapd /etc/hostapd/hostapd.conf &
exit 0
```
****參考資料:****
[1. 樹莓派 Raspberry Pi 設定無線網路 WiFi AP,打造無線 IP 分享器](https://blog.gtwang.org/iot/setup-raspberry-pi-as-wireless-access-point/)
[2. 兩部主機兩點溝通: ping](http://linux.vbird.org/linux_server/0140networkcommand.php#ping)
[3. NAT 伺服器的設定](http://linux.vbird.org/linux_server/0250simple_firewall.php#nat)
[4. GNU / Linux 各種壓縮與解壓縮指令](http://note.drx.tw/2008/04/command.html)
[5. Linux下使用hostapd 建立 WiFi訪問熱點](https://b8807053.pixnet.net/blog/post/349831267-linux%E4%B8%8B%E4%BD%BF%E7%94%A8hostapd-%E5%BB%BA%E7%AB%8B-wifi%E8%A8%AA%E5%95%8F%E7%86%B1%E9%BB%9E)
###### tags: `raspberry pi` `wifi 基地台`