树莓派3B制作无线wifi(AP with hostapd)

参考:https://frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/

这是一款新型的树莓派,并且自带wifi模块,不用大家自己另购,很是方便。

网上写树莓派做wifi的文档不少但参差不齐,我试的也不少,都没有成功,不过最终还是在大家的帮助下制作出来了。这里给大家推荐总结一下我的做法,有问题请留言。


1、最先我们要确认树莓派与网线连好,树莓派通过eth0的静态IP连接网络。

接着我们要下载两个重要的包,敲入命令 sudo apt-get  install dnsmasq hostapd 

hostapd:能使无线网卡工作在软AP(Access Point)模式,即无线路由器;

dnsmasq:能够同时提供DHCP和DNS服务;

你也可以使用isc-dhcp-server和bind9包分别服务DHCP和DNS,但我们使用dnsmasq已经足够了。


2、在最新的树莓派版本中,所有的网络接口默认使用dhcpd程序来进行配置,因为wlan0工作在AP模式,所以我们要手动给他静态配置IP地址,先在配置文件 /etc/dhcpcd.conf 中最下面添加一行去禁用 wlan0  ,否则wlan0和eth0会发生冲突。

因为eth0是uplink,连接Internet,而wlan0是downlink,供给其他设备网络。

在命令行敲下 sudo vim /etc/dhcpcd.conf ,在文档最下面添加:

denyinterfaces wlan0


3、接下来我们在 /etc/network/interfaces 中静态配置无线网卡的IP地址,sudo vim /etc/network/interfaces 进去在里面添加如下代码,特别注意wlan0和eth0的静态IP地址不在一个局域网内,否则它们两个又会打架导致你的ssh登录失败。

auto lo
iface lo inet loopback

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

allow-hotplug wlan0
iface wlan0 inet static
address 192.168.1.20
netmask 255.255.255.0
然后重启dpchcd服务,sudo service dhcpcd restart 
重新载入wlan0的配置, sudo ifdown wlan0 和 sudo ifup wlan0


4、好了之后,我们来配置hostapd,也就是我们wifi的信息,sudo vim /etc/hostapd/hostapd.conf  ,完成下面配置,ssid名字和wpa_passphrase密码可以自己更改。

# 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

接着我们运行,sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf 。

现在我们应该可以发现Pi3-AP了,但是我们还不能连接,因为我们还没有开始配置dnsmaq

别急,我们需要告诉hostapd 开机的时候在哪里寻找配置文件, sudo vim /etc/default/hostapd ,然后找到#DAEMON_CONF="",去掉它的引号,改成DAEMON_CONF="/etc/hostapd/hostapd.conf"


5、因为dnsmasq包含大量信息文件,但对于我们这次的目的来说大部分是多余的。我建议先将原来的重命名,再创建一个新的 dnsmasq配置文件。

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

sudo vim /etc/dnsmasq.conf  

再在其中添加:

interface=wlan0 # Use interface wlan0
listen-address=192.168.1.20 # 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.1.100,192.168.1.200,12h # Assign IP addresses between 192.168.2.100 and 192.168.2.200 with a 12 hour lease time  


完后我们重启dnsmasq, sudo service dnsmasq restart 

现在我们可以连接树莓派的wifi了,但是还是不能上网。


6、最后,我们设置ipv4的转发。  sudo nano /etc/sysctl.conf net.ipv4.ip_forward=1 这行之前的#号去掉,然后执行

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward" 开启IP转发功能。

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

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


这时候电脑或手机就可以连上wifi了,但上述命令都是手动操作,下次开机之后树莓派并不会执行,所以我们要保存一下规则便于我们每次开机就自动开启。

执行sudo sh -c "iptables-save > /etc/iptables.ipv4.nat" ,然后再 sudo vim /etc/rc.local  ,在里面的exit0之前添加:

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

我们重启hostapd和dnsmasq服务

sudo service hostapd start

sudo service dnsmasq start

这下我们就终于可以连接树莓派的wifi上网了,最后我们sudo reboot继续连接我们的wifi吧。

如果制作有什么问题或者我的博客有其他错误的话,请在下方评论吧!



评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姜亚轲

你花钱的样子真帅

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值