无网卡设备利用树莓派wifi联网 | DHCP
原文链接: 无网卡设备利用树莓派wifi联网 | DHCP | 不会Debug (fater.top)
利用树莓派的一张有线网卡和一张无线网卡,将wifi转为有线信号,使无网卡设备可以网线直连树莓派进行上网
起因
手边有个丐版的NUC主板,没有wifi没有蓝牙,只有两个有线网口,但是房间的地方离主路由器太远,拉网线连接非常不方便。本着能用则用的原则,正好这次回家把树莓派一起带上了,就压榨一下树莓派的两张网卡,让这个NUC主板上网。另外说明一下,树莓派和NUC主板都是无屏幕的,必要时是用视频采集卡在我的笔记本电脑屏幕上显示,大部分操作都可以通过ssh完成。
设备/环境介绍
主路由器:广电网络,基本没有操作空间
旁路由(以下用树莓派代指):树莓派4B 4G,arrch,Ubuntu20.04,一个有线网口(eth0),一个无线网卡(wlan0)
需联网设备(以下用nuc代指):工控主板,amd,Ubuntu20.04,双有限网口,无无线网卡
PC:笔记本,Windows11,无网口
主路由网关:192.168.1.1
旁路由ip:192.168.1.24
计划配置服务:DHCP范围172.21.252.2-172.21.252.254;网关地址为172.21.252.1
示意图:
配置过程
前置准备
确保树莓派已连接wifi,使用PC连接SSH
ssh ubuntu@192.168.1.24
配置树莓派eth0接口
开启eth0,并指定ip为
172.21.252.1
ifconfig eth0 172.21.252.1 netmask 255.255.255.0 up
配置DHCP服务
使用
dnsmasq
为eth0网卡提供DHCP服务
安装
dnsmasq
sudo apt install dnsmasq
添加
/etc/dnsmasq.conf
配置
sudo nano /etc/dnsmasq.conf
port=0
interface=eth0
listen-address=172.21.252.1
dhcp-range=172.21.252.2,172.21.252.254,12h
解释一下配置参数 -
interface=eth0
是设置dhcp服务的接口为树莓派上的有线网卡 -
listen-address=172.21.252.1
是将监听地址设为有限网卡的ip地址 -
dhcp-range=172.21.252.2,172.21.252.254,12h
是设置DHCP分配的ip范围,从
172.21.252.2
到
172.21.252.254
,租赁时限为12h - 要注意这里如果不设置
port=0
会开启dns服务,但是我们不需要,设为0就标识关闭dns服务,如果不设置的话可能会出现下面报错,意思是端口53已经被占用了,解决方法可以关闭dns服务或将其设为其他端口,比如5533
Jan 16 02:49:30 ubuntu systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...
Jan 16 02:49:30 ubuntu dnsmasq[1570]: dnsmasq: syntax check OK.
Jan 16 02:49:31 ubuntu dnsmasq[1571]: dnsmasq: failed to create listening socket for port 53: Address already in use
Jan 16 02:49:31 ubuntu dnsmasq[1571]: failed to create listening socket for port 53: Address already in use
Jan 16 02:49:31 ubuntu dnsmasq[1571]: FAILED to start up
Jan 16 02:49:31 ubuntu systemd[1]: dnsmasq.service: Control process exited, code=exited, status=2/INVALIDARGUMENT
Jan 16 02:49:31 ubuntu systemd[1]: dnsmasq.service: Failed with result 'exit-code'.
Jan 16 02:49:31 ubuntu systemd[1]: Failed to start dnsmasq - A lightweight DHCP and caching DNS server.
重启dnsmasq服务
sudo systemctl restart dnsmasq
正确运行后执行
sudo systemctl status dnsmasq
输出应该大致如下:
● dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server
Loaded: loaded (/lib/systemd/system/dnsmasq.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-01-16 02:51:26 UTC; 1min 0s ago
Process: 1606 ExecStartPre=/usr/sbin/dnsmasq --test (code=exited, status=0/SUCCESS)
Process: 1607 ExecStart=/etc/init.d/dnsmasq systemd-exec (code=exited, status=0/SUCCESS)
Process: 1616 ExecStartPost=/etc/init.d/dnsmasq systemd-start-resolvconf (code=exited, status=0/SUCCESS)
Main PID: 1615 (dnsmasq)
Tasks: 1 (limit: 4433)
CGroup: /system.slice/dnsmasq.service
└─1615 /usr/sbin/dnsmasq -x /run/dnsmasq/dnsmasq.pid -u dnsmasq -7 /etc/dnsmasq.d,.dpkg-dist,.dpkg-old,.dp>
Jan 16 02:51:25 ubuntu systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...
Jan 16 02:51:25 ubuntu dnsmasq[1606]: dnsmasq: syntax check OK.
Jan 16 02:51:26 ubuntu dnsmasq[1615]: started, version 2.80 DNS disabled
Jan 16 02:51:26 ubuntu dnsmasq[1615]: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP connt>
Jan 16 02:51:26 ubuntu dnsmasq-dhcp[1615]: DHCP, IP range 172.21.252.2 -- 172.21.252.254, lease time 12h
Jan 16 02:51:26 ubuntu systemd[1]: Started dnsmasq - A lightweight DHCP and caching DNS server.
lines 1-17/17 (END)
开启SNAT
Ubuntu20.04的
ip_forward
是默认关闭的,需要手动开启,编辑
/etc/sysctl.conf
文件找到
net.ipv4.ip_forward=1
这行取消注释
sudo nano /etc/sysctl.conf
防呆指引
Ctrl+W 输入 net.ipv4.ip_forward 回车定位所在位置
删除 # 取消注释并检测 net.ipv4.ip_forward=1
Ctrl+X 回车输入 y 回车保存并退出
配置snat连接到外部网络
iptables -t nat -A POSTROUTING -s 172.21.252.1/24 -o wlan0 -j MASQUERADE
其中
172.21.252.1/24
为DHCP的网段,
wlan0
为树莓派的的无线网卡
设置nuc联网
将树莓派与nuc使用网线连接,在树莓派上监听连接获取nuc的ip
ssh连接nuc
ssh ubuntu@172.21.252.137
这个时候nuc还是没办法通过域名访问外部网络的,需要设置nuc的DNS服务器地址
sudo nano /etc/resolv.conf
将
nameserver 192.168.1.1
写入文件,保存并退出现在
ping baidu.com
就能通了
设备网络信息
树莓派
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.21.252.1 netmask 255.255.255.0 broadcast 172.21.252.255
inet6 fe80::dea6:32ff:fe46:33fa prefixlen 64 scopeid 0x20<link>
ether dc:a6:32:46:33:fa txqueuelen 1000 (Ethernet)
RX packets 383 bytes 65536 (65.5 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 524 bytes 57530 (57.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 212 bytes 17610 (17.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 212 bytes 17610 (17.6 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.24 netmask 255.255.255.0 broadcast 192.168.1.255
ether dc:a6:32:46:33:fb txqueuelen 1000 (Ethernet)
RX packets 3670 bytes 583206 (583.2 KB)
RX errors 0 dropped 2 overruns 0 frame 0
TX packets 3837 bytes 796006 (796.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
nuc
enp2s0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 00:90:27:e9:01:b9 txqueuelen 1000 (以太网)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device memory 0x50800000-508fffff
enp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.21.252.137 netmask 255.255.255.0 broadcast 172.21.252.255
inet6 fe80::108b:c57d:1fab:fe4a prefixlen 64 scopeid 0x20<link>
ether 00:90:27:e9:01:ba txqueuelen 1000 (以太网)
RX packets 472 bytes 52274 (52.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 399 bytes 65238 (65.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device memory 0x50500000-505fffff
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0