reason
After starting raspberry pie, you must want to toss all kinds of things on raspberry pie like me. Among them, you must have the idea of using the public IP of broadband distribution.
The most direct way to use public IP is to use raspberry pie as a router, but raspberry pie 4B has only one gigabit network port, so this is the theme of this article "raspberry pie single arm routing".
The basic functions of the router are: connecting to the external network (dial-up Internet access), enabling DHCP function and providing WIFI hotspot. This paper also mainly solves these three problems.
Equipment list
Raspberry pie 4B + Debian10 x1
Gigabit switch x1
Wireless router x1
preparation
- Install Debian10 system for raspberry pie. For the installation tutorial, please refer to another article: Raspberry Pie 4 installation Debian10
- First connect the raspberry pie to the original router to ensure ssh login to Debian10 system;
- After logging into the raspberry pie system, turn on ipv4 forwarding:
$ sudo vi /etc/sysctl.conf # Delete the previous comment net.ipv4.ip_forward=1 $ sudo sysctl -p
Provide WIFI hotspot
Before setting up single arm routing, be sure to turn on WIFI hotspot, because raspberry pie has only one network interface. After connecting to the external network, you can only use WIFI connection to enter the system.
I tried to create hotspots in two ways: hostapd and network manager. Compared with network manager, the configuration of network manager is simpler and more stable, so I don't recommend you to toss about hostapd.
The following steps are for network manager installation and configuration:
# Install network manage $ sudo apt install network-manager # Add WIFI hotspot configuration $ sudo nmcli c add type wifi con-name wlan0 ifname wlan0 wifi.mode ap ssid <WIFI name> # Turn on 5G channel (if it is set to 2G, it can't be found sometimes, please don't modify it) $ sudo nmcli c mod wlan0 wifi.band a # Set WIFI password (set to WPA2, some devices cannot be connected, please do not modify) $ sudo nmcli c mod wlan0 wifi-sec.key-mgmt wpa-psk wifi-sec.psk <WIFI password> # Set fixed IP $ sudo nmcli c mod wlan0 ipv4.method manual ipv4.addresses 10.5.7.1/24 # Restart service $ sudo systemctl restart NetworkManager.service
I don't know whether there is a problem with the WIFI module of raspberry pie or the system. After trying a variety of settings, I found that WIFI hotspots can only enable 5G and WPA encryption.
Please tell me why you have to make the next step if you have trouble.
After restarting the network manager, you can try to connect with your mobile phone. If the connection is successful, go to the next step.
Enable DHCP function
After the WIFI connection is successful, the icon should be in the state of exclamation mark. This is because there is no IP assigned to the mobile phone, so next, turn on the DHCP function:
# Install dnsmasq $ sudo apt install dnsmasq # Edit the configuration and add it at the end of the configuration $ sudo vi /etc/dnsmasq.conf interface=wlan0 listen-address=10.5.7.1 dhcp-range=10.5.7.100,10.5.7.200,48h # Restart dnsmasq service $ sudo systemctl restart dnsmasq.service
After restarting dnsmasq service, disconnect WIFI from the mobile phone and reconnect. At this time, you should be able to surf the Internet normally.
Connect to the Internet
After WIFI is turned on, you can share the network with your laptop or mobile phone and log in to the raspberry system.
If you don't have a laptop, you can use your mobile phone to connect and share the network with your computer. The specific steps are as follows:
- Disconnect the computer network;
- Connect the mobile phone to WIFI and connect to the computer with USB;
- Enter mobile phone settings = > mobile network = > mobile network sharing = > USB shared network;
- Try ssh login to raspberry pie on the computer. The connection address is the fixed IP set by the front WIFI: 10.5.7.1;
Now we use mobile phone to share the network, log in to the raspberry pie system, connect the optical cat to the network cable on the old router and plug it directly into the network port of raspberry pie.
My broadband is DHCP, so I can directly connect to the Internet without any settings.
You can use the ip addr command to check whether the IP address of the external network is assigned. If not, please refer to another article: Raspberry Pie 4+Debian10 as a router, unable to dial up the Internet
If your broadband needs dial-up Internet access, it is recommended to dial up with network management. Please refer to other materials for detailed configuration, because I don't have this condition to try.
Next, you need to add a virtual network card and set a fixed IP to act as a gateway for wired devices connected to raspberry Pie:
-
Add virtual network card:
# Add at the end of the configuration $ sudo vi /etc/network/interfaces.d/eth0 auto eth0:0 allow-hotplug eth0:0 iface eth0:0 inet static address 10.5.6.1 netmask 255.255.255.0 # Restart the network $ sudo systemctl restart networking.service
After restarting the network, use ip addr to check that eth0 will have two IP addresses: one is the address of the external network and the other is the address of the internal network 10.5.6.1.
-
Enable DHCP function:
# Add at the end of the configuration $ sudo vi /etc/dnsmasq.conf interface=eth0:0 listen-address=127.0.0.1,10.5.6.1 dhcp-range=10.5.6.100,10.5.6.200,48h # Restart dnsmasq service $ sudo systemctl restart dnsmasq.service
Turn on 2G WIFI function
As mentioned earlier, raspberry pie cannot provide 2G WIFI connection, so the old router can be used as 2G access point here.
If your router supports wired bridging, you can bridge to raspberry pie; If bridging is not supported, you can connect to the raspberry pie and set it as a secondary route, but you can't access the raspberry pie and other devices under the raspberry pie.
Because different router configurations may be different, please consult other materials by yourself, which will not be described here.
This step is not necessary and can be skipped.
Final network topology
Finally, connect the raspberry pie, optical cat, wireless router and other wired devices to the switch and have a good time.