Skip to content

配置AP

ShenHengheng edited this page Dec 10, 2017 · 1 revision

3.1 配置流程

3.1.1 必需的包

  1. hostapd
  2. dnsmasq
sudo apt-get install dnsmasq hostapd

3.1.2 配置网卡

将wlan1(购买的网卡)配置一个静态IP地址 首先告诉树莓派DHCP获取IP时忽略WLAN1

sudo nano /etc/dhcpcd.conf

在文件的最后添加:

denyinterfaces wlan1  

配置静态IP

sudo nano /etc/network/interfaces

文件内容如下:

allow-hotplug wlan1  
iface wlan1 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

重启DHCP服务。

sudo apt-get install dhcpcd5 #如果没有这个服务
sudo service dhcpcd restart

然后重启WLAN1设备以适应配置

sudo ifdown wlan1; sudo ifup wlan1

3.1.3 配置HOSTAPD文件

sudo nano /etc/hostapd/hostapd.conf

内容是:

# This is the name of the WiFi interface we configured above
interface=wlan1

# 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=123456789

# Use AES, instead of TKIP
rsn_pairwise=CCMP

检查配置是否起作用:

sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf

如果成功,则会在你的移动设备上(智能手机)上检测到Pi3-AP的路由。如果你尝试连接,则不会成功分配地址,因为还没有做完! 因为我们还需要告诉hostapd在启动时启动时查找配置文件。使用

sudo nano /etc/default/hostapd

打开默认配置文件,找到该行

#DAEMON_CONF=""

并且用下面这行替换掉

DAEMON_CONF="/etc/hostapd/hostapd.conf".

3.1.4 配置DNSMASQ

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig  
sudo nano /etc/dnsmasq.conf  

将下面的内容粘贴上去:

interface=wlan1      # 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

3.1.5 建立IPV4 转发

sudo nano /etc/sysctl.conf

去掉

#net.ipv4.ip_forward=1

前面的#

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

上面这条命令是立即激活转发! 还需要通过在wlan0接口和wlan1接口之间配置NAT来分享Pi的互联网连接到我们通过WiFi连接的设备。可以使用以下命令来执行此操作:

sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE  
sudo iptables -A FORWARD -i wlan0 -o wlan1 -m state --state RELATED,ESTABLISHED -j ACCEPT  
sudo iptables -A FORWARD -i wlan1 -o wlan0 -j ACCEPT

但是,我们需要在每次重新启动Pi时应用这些规则,运行下面的命令

sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

为了避免每次重启都需要运行该命令,打开

sudo nano /etc/rc.local

在exit 0前添加一行

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

3.1.6 我们已经完成了

sudo service hostapd start  
sudo service dnsmasq start  

3.2 注意事项

  1. 树莓派3和树莓派2的配置过程是不一样的,上面的配置过程仅适用于树莓派3.
  2. 上面的配置是WLAN1做AP,WLAN0将数据流转发给WLAN1的,那么转发也可以做以太网和wlan0转发,以太网和wlan1转发。过程类似。
  3. 为了激活,必须重启!