树莓派3b 路由

1、确认树莓派与网线连好,树莓派通过eth0的静态IP连接网络。
sudo apt-get  install dnsmasq hostapd 
hostapd:能使无线网卡工作在软AP(Access Point)模式,即无线路由器;
dnsmasq:能够同时提供DHCP和DNS服务;
2、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地址不在一个局域网内

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吧。
---------------------
原文:https://blog.csdn.net/weixin_41656968/article/details/79818033

猜你喜欢

转载自www.cnblogs.com/sanmubai/p/10296757.html
今日推荐