Devices without network card use Raspberry Pi wifi to connect to the Internet | DHCP

Original link: Devices without network card use Raspberry Pi wifi to network | DHCP | No Debug (fater.top)

Use a wired network card and a wireless network card of the Raspberry Pi to convert wifi into a wired signal, so that devices without a network card can directly connect to the Raspberry Pi for Internet access

cause

There is a beggar’s version of the NUC motherboard at hand, no wifi, no bluetooth, only two wired network ports, but the room is too far away from the main router, and it is very inconvenient to pull the network cable to connect. In line with the principle of using what you can, I happened to bring the Raspberry Pi with me when I went home this time, so I squeezed the two network cards of the Raspberry Pi to let this NUC motherboard go online. In addition, the Raspberry Pi and NUC motherboards are both without screens. When necessary, I use a video capture card to display on the screen of my laptop. Most operations can be completed through ssh.

Equipment/environment introduction

Main router: radio and television network, basically no room for operation

Bypass router (hereinafter referred to as Raspberry Pi): Raspberry Pi 4B 4G, arrch, Ubuntu20.04, a wired network port (eth0), a wireless network card (wlan0)

Devices that need to be connected to the Internet (hereinafter referred to as nuc): industrial control motherboard, amd, Ubuntu20.04, dual limited network ports, no wireless network card

PC: Notebook, Windows 11, no network port

Main routing gateway: 192.168.1.1

Bypass router ip: 192.168.1.24

Plan configuration service: DHCP range 172.21.252.2-172.21.252.254; gateway address is 172.21.252.1

Schematic diagram:

configuration process

Preparation

Make sure the Raspberry Pi is connected to wifi, use PC to connect to SSH

ssh ubuntu@192.168.1.24

Configure Raspberry Pi eth0 interface

Open eth0 and specify ip as 172.21.252.1

ifconfig eth0 172.21.252.1 netmask 255.255.255.0 up

Configure DHCP service

Use dnsmasq to provide DHCP service for eth0 network card

install dnsmasq

sudo apt install dnsmasq

Add /etc/dnsmasq.conf configuration

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

Explain the configuration parameters

  • interface=eth0 is to set the dhcp service interface as the wired network card on the Raspberry Pi

  • listen-address=172.21.252.1 is to set the listening address to the ip address of the limited network card

  • dhcp-range=172.21.252.2,172.21.252.254,12h is to set the ip range assigned by DHCP, from 172.21.252.2 to 172.21.252.254, and the lease time limit is 12h

  • Note that if you do not set `

port=0` will open the dns service, but we don’t need it. Set it to 0 to indicate that the dns service is closed. If it is not set, the following error may appear, which means that port 53 has been occupied. The solution can be to close the dns service or set the Set it to another port, such as 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.

Restart the dnsmasq service

sudo systemctl restart dnsmasq

After running correctly, execute sudo systemctl status dnsmasq output should be roughly as follows:

● 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)

Enable SNAT

The ip_forward of Ubuntu 20.04 is disabled by default and needs to be enabled manually. Edit the /etc/sysctl.conf file to find the line net.ipv4.ip_forward=1 and uncomment it

sudo nano /etc/sysctl.conf

Configure snat to connect to the external network

iptables -t nat -A POSTROUTING -s 172.21.252.1/24 -o wlan0 -j MASQUERADE

Among them, 172.21.252.1/24 is the network segment of DHCP, and wlan0 is the wireless network card of Raspberry Pi

Set up nuc networking

Connect the raspberry pie to the nuc with a network cable, monitor the connection on the raspberry pie to get the ip of the nuc

ssh connection nuc

ssh ubuntu@172.21.252.137

At this time, the nuc still has no way to access the external network through the domain name, and the DNS server address of the nuc needs to be set.

sudo nano /etc/resolv.conf

write nameserver 192.168.1.1 to file, save and exit now ping baidu.com can pass

Device network information

raspberry pie

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  (ethernet)
        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  (ethernet)
        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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (local loopback)
        RX packets 1998  bytes 145444 (145.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1998  bytes 145444 (145.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Tags: Ubuntu ssh

Posted by ma9ic on Tue, 17 Jan 2023 13:31:54 +1030