使用树莓派3B制作无线路由器

首先需要安装两个制作无线路由器必需的软件:

sudo apt-get update

sudo apt-get install hostapd dnsmasq

  • sudo apt-get update命令作用是更新软件列表,如果有软件需要更新,可以执行命令 sudo apt-get upgrade 安装升级软件

  • hostapd: 该软件能使无线网卡工作在软AP(Access Point)模式,即无线路由器;
    dnsmasq:该软件能够同时提供DHCP和DNS服务;

在最新版的树莓派版本中,所有的网络接口都是默认使用dhcpd程序来配置 ,因为wlan0工作在AP模式,所以需要手动静态配置IP地址,先在配置文件
/etc/dhcpcd.conf中禁用wlan0:

sudo vim /etc/dhcpcd.conf

#interface eth0
#fallback static_eth0

denyinterfaces wlan0

然后再在/etc/network/interfaces中静态配置无线网卡的IP地址:

sudo vim /etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.2.50
netmask 255.255.255.0
gateway 192.168.2.1

allow-hotplug wlan0
iface wlan0 inet static
address 192.168.10.1
netmask 255.255.255.0 
  • auto eth0往下这段代码作用是设置有线连接的IP地址,因为作者使用树莓派有线连接的路由器IP地址是192.168.2.1,所以文中IP地址这样设置,读者可根据自身实际情况设置有线连接的IP地址
  • allow-hotplug wlan0往下这段代码是设置树莓派作为路由器使用时树莓派的IP地址,这个地址可以自行设置,例如:192.168.x.x

配置好IP地址后,重启系统,让无线网卡生效

sudo reboot

然后修改hostapd程序的配置文件:

sudo vim /etc/hostapd/hostapd.conf

这个文件应该是不存在的,需要自己创建,所以vim打开后里面是空的,写入下面的代码:

# 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的启动配置文件,让系统启动时能找到hostapd的配置文件:

sudo vim /etc/default/hostapd

# Defaults for hostapd initscript
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
DAEMON_CONF="/etc/hostapd/hostapd.conf"

# Additional daemon options to be appended to hostapd command:-
# -d show more debug messages (-dd for even more)
# -K include key data in debug messages
# -t include timestamps in some debug messages
#
# Note that -B (daemon mode) and -P (pidfile) options are automatically
# configured by the init.d script and must not be added to DAEMON_OPTS.
#
#DAEMON_OPTS="" 

这时候,可以启动测试hostapd

sudo hostapd -B /etc/hostapd/hostapd.conf

  • 作者在执行这段代码时出了问题,无法启动hostapd程序,通过一顿骚操作冷静分析之后,找到了问题的根源。作者在制作路由器之前,已经给树莓派静态配置过IP地址了,但那时是使用dhcpcd
    method配置的,路由器自身的地址应该是静态的,所以此时启动hostapd服务是不能成功的。可以执行下面的代码来解决这个bug:

    sudo systemctl disable dhcpcd

    sudo systemctl enable networking

    sudo reboot

    最后重启生效!

    开机后再次启动hostapd服务:

    sudo hostapd -B /etc/hostapd/hostapd.conf

  • 当然如果读者启动hostapd服务时没有遇到这个bug,可以跳过上面这一步!

hostapd服务启动成功后,通过电脑或者手机可以发现无线热点 Pi3-AP,但是无法连接上此热点,因为树莓派的无线网卡并没有开启DHCP和DNS服务器,树莓派无法给客户端分配IP地址,接下来配置dnsmasq

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

sudo vim /etc/dnsmasq.conf

interface=wlan0 # Use interface wlan0

listen-address=192.168.10.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=192.168.10.100,192.168.10.200,12h # Assign IP addresses between 172.24.1.50 and 172.24.1.150 with a 12 hour lease time 
  • 读者在复制这段代码时注意文中代码后面的注释没有换行,因为长度不够,所以文中显示的像换行

然后再重启dnsmasq服务:

sudo service dnsmasq restart

  • 开启DHCP和DNS服务之后,电脑可以获取IP地址并连接到树莓派上,但是电脑还不能上网。此时需要开启Linux内核的IP转发以及使用iptables做NAT表,让无线网卡的数据通过有线网卡转发出去。

开启Linux内核的IP转发功能:

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

开启树莓派有线网卡和无线网卡的转发功能:

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

  • 当这些功能都开启后,电脑和手机就可以连接上树莓派上网了。不过这些功能都是手动开启的,树莓派重启后不会自动去开启这些功能,这时可以进行一些配置让系统启动后就能生效:

保存当前的防火墙策略到配置文件中:

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

修改系统启动脚本,添加启动任务:

sudo vim /etc/rc.local

sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
iptables-restore < /etc/iptables.ipv4.nat

exit 0 

然后启动生效:

sudo reboot

然后电脑或者手机就可以连接到树莓派上网了!

猜你喜欢

转载自blog.csdn.net/weixin_42289213/article/details/83064394