https://frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/
声明:本文仅供交流使用,版权归原文作者所有!
本文系嵌入式Linux中文站志愿者千年人鱼翻译投递,感谢志愿者们的辛勤付出,希望更多的朋友加入嵌入式Linux中文站志愿者团队。
这是一个新的树莓派,太令人惊喜了。它还板载WIFI,这是双重惊喜啊!我的第一个想法是,我能用它来当做softAP吗?这个想法出现时,他也没那么难,因为BCM43438芯片是有开源的brcmfmac驱动的。
资源包
首先第一步我们要去安装两个资源包:dnsmasq、hostapd;运行命令:sudo apt-get install dnsmasq、hostapd;这两个包的作用是:
· Hostapd - 这个包允许树莓派3板载的WIFI作为一个接入点(AP)
· Dnsmasq - 这个能够使DHCP和DNS服务配置变得更加容易
如果你需要更多一点的功能的话,你可以使用isc-dhcp-server和bind9资源包分别对DHCP和DNS进行配置,但是对于我们现在的需求,dnsmasq已经足够了。
配置网卡
首先你要给你的wlan0网卡设置一个静态的IP。在最新的树莓派版本,网卡已经默认的被dhcpcd处理。我们可以先不管他,因为我们将要为它分配一个静态的IP。现在请你打开dhcpcd配置文件:sudo vim /etc/dhcpcd.conf并在文件的最后一行添加以下内容
denyinterfaces wlan0
Note: This must be ABOVE any interface lines you may have added!
现在我们需要配置我们的静态IP。请打开接口的配置文件:sudo vim /etc/network/interfaces并编辑wlan0部分内容,他看起来像这样:
allow-hotplug wlan0
iface wlan0 inet static
address 172.24.1.1
netmask 255.255.255.0
network 172.24.1.0
broadcast 172.24.1.255
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
然后sudo service dhcpcd restart重启dhcpcd服务,sudo ifdown wlan0; sudo ifup wlan0重启网卡
配置HOSTAPD
接下来,我们需要去配置hostapd了。创建一个新的配置文件vim /etc/hostapd/hostapd.conf,然后加入以下内容:
# This is the name of the WiFi interface we configured above
interface=wlan0
# Use the nl80211 driver with the brcmfmac driver
driver=nl80211
# This is the name of the network
ssid=Pi3-AP
# Use the 2.4GHz band
hw_mode=g
# Use channel 6
channel=6
# Enable 802.11n
ieee80211n=1
# Enable WMM
wmm_enabled=1
# Enable 40MHz channels with 20ns guard interval
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
# Accept all MAC addresses
macaddr_acl=0
# Use WPA authentication
auth_algs=1
# Require clients to know the network name
ignore_broadcast_ssid=0
# Use WPA2
wpa=2
# Use a pre-shared key
wpa_key_mgmt=WPA-PSK
# The network passphrase
wpa_passphrase=raspberry
# Use AES, instead of TKIP
rsn_pairwise=CCMP
到这里我们需要确认一下hostapd是否能够工作了,执行sudo /usr/sbin/hostapd和/etc/hostapd/hostapd.conf.如果一切还顺利的话,你应该能够看到名为Pi3-AP的WiFi网络了。如果你这时候试图连接他,你会看到有一些东西会喷在你的PI上,但是你是获取不到IP地址的,你还需要在下一步设置一下dnsmasq文件。Ctrl+C把它关掉吧。
我们还没有完成,因为我们需要告诉hostapd当他启动的时候应该去哪里找配置文件。打开默认的配置文件vim /etc/default/hostapd并找到#DAEMON_CONF=""将他替换成这DAEMON_CONF="/etc/hostapd/hostapd.conf"
配置DNSMASQ
原始的dnsmasq配置文件包含了一些很丰富的信息,但是对于我们来说太多了,我们用不着。建议将他移除(注意:不是删除),我们创建一个新的替代它。
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo nano /etc/dnsmasq.conf
复制粘贴下面的内容进新的文件:
interface=wlan0 # Use interface wlan0
listen-address=172.24.1.1 # Explicitly specify the address to listen on
bind-interfaces # Bind to the interface to make sure we aren't sending things elsewhere
server=8.8.8.8 # Forward DNS requests to Google DNS
domain-needed # Don't forward short names
bogus-priv # Never forward addresses in the non-routed address spaces.
dhcp-range=172.24.1.50,172.24.1.150,12h # Assign IP addresses between 172.24.1.50 and 172.24.1.150 with a 12 hour lease time
设置IPV4的转发
在我们成功的转发我们的包之前,还需要做最后一件事。打开sysctl.conf文件vim /etc/sysctl.conf并移除这个# from the beginning of the line containing net.ipv4.ip_forward=1.这将会在下次重启时生效,如果你没有耐心,可以这样让他立即活动:sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
我们需要将PI的WIFI连接分享出去,通过配置NAT在wlan0和eth0之间。使用下面命令实现。
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
然而,我们需要这些规则去应用于我的Pi在每次重启机器的时候,所以runsudo sh -c "iptables-save > /etc/iptables.ipv4.nat"去保存这些规则在/etc/iptables.ipv4.nat文件中。现在我们需要每次重启机器时都要去启用这些规则,所以还需要配置这个文件vim /etc/rc.local在这个文件的exit 0这行的上面加入下面的内容:
iptables-restore < /etc/iptables.ipv4.nat
差不多成功了!
现在我们只需要去重启一下我们的服务就好了:
sudo service hostapd start
sudo service dnsmasq start
就是他了!你应该可以通过你的PI的WIFi连接到Internet了吧!
再次重启确认我们的配置是不是正确的!
EDIT: Thanks to Justin for helping iron out some of the errors in this post!
EDIT2: Thanks to Ashok for several performance related enhancements!
EDIT3: Thanks to Lasse for some amendments to the dnsmasq configuration!
EDIT4: Fixed race condition between dhcpcd and dnsmasq, wlan0 is no longer configured by dhcpcd.
PHIL MARTIN