树莓派适用的无线网卡

这个地址http://elinux.org/RPi_USB_Wi-Fi_Adapters提供了适合树莓派的一些无线网卡,多方对比之后,我选择了Ralink3070的无线网卡,使用无线网卡很关键的一点就是需要配置一下/boot/config.txt添加max_usb_current=1,使得usb的供电从600mA提高到1.2A,不然无线网卡会供电不足重启。

Raspberry Pi 3板载了无线网络适配器和陶瓷天线,不需要额外增加无线网卡就可以把它打造成一个无线路由器。

有一种方法是给Raspberry Pi刷上OpenWRT等路由器固件,这样它就变成了一个比较正宗的路由器,这个方法本人已经尝试过,时不时断线的情况并没有改善,而且可用的软件会少一些,我还是推荐另一种方法就是开启树莓派无线网络适配器的AP功能,并且共享其有线网络。这样依然使用Raspbian系统,可以发挥Raspberry Pi作为一个微型服务器的优势。

以下描述如何开启树莓派无线网络适配器的AP功能,并且共享其有线网络,实现无线路由功能。

安装软件包

需要安装2个软件包:

sudo apt-get install hostapd dnsmasq

hostapd将开启无线适配器的AP功能,dnsmasq是DHCP和DNS服务器。

1.设置静态IP

需要将无线接口wlan0的IP配置成静态地址。

首先让dhcpcd不再管理wlan0,避免设置冲突。

修改文件:

sudo vi /etc/dhcpcd.conf

在文件开头增加一行:

denyinterfaces wlan0

接下来设置wlan0的静态IP,修改文件:

sudo vi /etc/network/interfaces

把wlan0相关的内容修改成如下内容:

allow-hotplug wlan0 

#iface wlan0 inet manual 
iface wlan0 inet static 
address 192.168.8.1 
netmask 255.255.255.0 
network 192.168.8.0 
broadcast 192.168.8.255

192.168.8.1 是给树莓派做路由器分配的网关IP,这个不能与局域网其他路由器网关IP重复,若重复此处可以修改IP地址为其他网关。

重启服务和wlan0

sudo service dhcpcd restart
sudo ifdown wlan0
sudo ifup wlan0

通过ifconfig可以看到wlan0的IP已经设定好了。

2.安装hostapd

输入命令安装,hostapd将开启无线适配器的AP功能。

sudo apt-get install hostapd dnsmasq

新建配置文件:

sudo vi /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=RaspberryPi
# Use the 2.4GHz band
hw_mode=g
# Use channel 6
channel=6
# Enable 802.11n
ieee80211n=1
# Enable WMM
wmm_enabled=1
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

Ralink 3070不支持[DSSS_CCK-40] 以上内容中,ssid=RaspberryPi是无线网络的名字,wpa_passphrase=raspberry是密码(8位及以上)。

然后测试配置是否正确:

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

通过手机等设备应该可以搜到名为RaspberryPi的WiFi,还不需要连接。

如果没有问题,按Ctrl+C停止测试。

使上述设置生效:

sudo vim /etc/default/hostapd

#DAEMON_CONF=""修改为DAEMON_CONF="/etc/hostapd/hostapd.conf"

3.安装DHCP服务

输入命令

sudo apt-get install isc-dhcp-server

备份配置文件

sudo mv /etc/dhcp/dhcpd.conf /etc/dhcp.dhcpd.config.bak

编辑新的配置文件

sudo vi /etc/dhcp/dhcpd.conf

修改成如下内容:

default-lease-time 600; 
max-lease-time 7200;
log-facility local7;
subnet 192.168.8.0 netmask 255.255.255.0 {
  range 192.168.8.10 192.168.8.100;
  option routers 192.168.8.1;
  option broadcast-address 192.168.8.255;
  option domain-name-servers 8.8.8.8,8.8.4.4;
  default-lease-time 600;
  max-lease-time 7200;
}

此处设置的IP与上面的IP网关对应。

重新启动服务

 sudo service  isc-dhcp-server restart

4.开启ipv4转发

修改文件:

 sudo vim /etc/sysctl.conf

去掉net.ipv4.ip_forward=1前面的“#”号。

通过iptables做NAT转发:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERAD
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 vi /etc/rc.local

在exit 0上方增加:

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

启动服务:

sudo service hostapd start
sudo reboot

等Raspberry Pi重启完成,就可以用手机等设备连接该WiFi上网了,名字和密码是之前在/etc/hostapd/hostapd.conf文件中设定的。

连接后设备会获取到192.168.8.10----192.168.8.100之间的IP,并且可以通过树莓派连接的有线网络连上互联网,起到了无线路由器的功能。

如果遇到问题,可以通过以下命令检查服务情况。

sudo service hostapd status

虽然把树莓派当成无线路由是可行的,但性能真的不行,而且不稳定,时不时会有断线的情况。还是用专业的无线路由器做为AP会比较好用,树莓派就是用来折腾的玩具,没有折腾过怎么会知道是这样的结果,失败也是一种收获。

参考文章

results matching ""

    No results matching ""