Transform the Raspberry Pi into a wireless soft router (2) ----- wireless router mode (independent wireless router)

Raspberry Pi can be used as a wifi wireless router in the network. Allows computers and devices using wireless access to connect to the network.
As shown in the figure below, there is already a network with a router that can access the Internet independently, but this network does not have wireless functions. Now we can use a raspberry pie to set up as an independent wifi wireless router, and add an independent wireless network to this network, so that the laptop can access the Internet through wifi wirelessly.

                                         +- RPi -------+
                                     +---+ 10.10.0.2   |          +- Laptop ----+
                                     |   |     WLAN AP +-)))  (((-+ WLAN Client |
                                     |   | 192.168.4.1 |          | 192.168.4.2 |
                                     |   +-------------+          +-------------+
                 +- Router ----+     |
                 | Firewall    |     |   +- PC#2 ------+
(Internet)---WAN-+ DHCP server +-LAN-+---+ 10.10.0.3   |
                 |   10.10.0.1 |     |   +-------------+
                 +-------------+     |
                                     |   +- PC#1 ------+
                                     +---+ 10.10.0.4   |
                                         +-------------+

The Raspberry Pi 4, Raspberry Pi 3, or Raspberry Pi Zero W all have built-in wireless capabilities (if your Raspberry Pi does not support wireless capabilities, then you can connect your Raspberry Pi to one that supports access point mode USB wireless network card) can be used to create wifi wireless AP.

The method described in this document has been tested on the latest Raspberry Pi OS bullseys + Raspberry Pi zero W. The only difference from this document is that the wired Ethernet network uses the 192.168.3.xxx network segment.

1. Preparation

  1. Administrator privileges on the Raspberry Pi
  2. Preferably, by directly connecting the screen and keyboard to the Raspberry Pi's local access mode (so as not to interrupt the ssh connection due to the change of ip during the setup process)
  3. The Raspberry Pi is connected to Ethernet and boots normally
  4. Upgrade to the latest Raspberry Pi OS, and if you installed a package during this configuration, remember to restart the Raspberry Pi to make sure the installation completed correctly.
  5. The current network configuration is:
  6. IP configuration of the Ethernet network to which the Raspberry Pi is connected:
    The IP network 10.10.0.0/24 is configured on the Ethernet LAN, and the Raspberry Pi will manage the IP network for the 192.168.4.0/24 wireless clients.
  7. Prepare wireless client devices, such as laptops, smart phones, etc., to test whether the wifi wireless AP is configured successfully.

2. Install wireless AP and management software

  1. Install the hostapd access point package:
sudo apt install hostapd
  1. Enable the Wireless Access Point service and set it to start automatically when the Raspberry Pi boots:
sudo systemctl unmask hostapd 
sudo systemctl enable hostapd
  1. In order to provide network management services (DNS, DHCP) to wireless clients, the Raspberry Pi needs to install the software package dnsmasq:
sudo apt install dnsmasq
  1. Finally, install netfilter-persistent and its plugin iptables-persistent. These two programs are used to save firewall rules and restore them when the Raspberry Pi boots:
sudo DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent

The software installation is complete. We'll configure the package later.

3. Set up network routing

In this configuration the Raspberry Pi will run and manage an independent wireless network. It also provides routing functionality between the wireless network and the Ethernet network, providing Internet access to wireless clients. If you do not need the wireless terminal to access the Internet, then you can skip the setting of "Enable Routing and IP Masquerade" below, so that the Raspberry Pi will not have the routing function, then the Raspberry Pi will bring up an independent wireless network.

3.1, Raspberry Pi's wireless network interface IP configuration

The Raspberry Pi runs a DHCP server for the wireless network; the Raspberry Pi's wireless network card wlan0 needs to be configured with a static IP. At this time, the Raspberry Pi is used as a router on this wireless network, and it is generally configured as the first IP address in this IP subnet: 192.168.4.1.

We need to modify the configuration file dhcpcd:

sudo vi /etc/dhcpcd.conf

Add the following at the end of the file:

interface wlan0
    static ip_address=192.168.4.1/24
    nohook wpa_supplicant

3.2. Enable routing and IP masquerading

Configure the Raspberry Pi so that wireless clients can access computers on the main (Ethernet) network and access the Internet (you can skip this chapter if you don't want wireless clients to access both the Ethernet network and the Internet).

To enable routing (allowing traffic to flow from one network to another in the Raspberry Pi), create a file with the following command:

sudo nano /etc/sysctl.d/routed-ap.conf

The content of the file is as follows (enable the forwarding function of IPv4)):

# Enable IPv4 routing
net.ipv4.ip_forward=1

After the routing function is enabled, the host from 192.168.4.0/24 accesses the network of segment 10 and the external network. At this time, we cannot modify the configuration of the main router, so in order to allow the wireless terminal on the 192 network segment to access the external network, the Raspberry Pi needs to enable the IP "masquerade" function, and replace the IP address of the Yuanxian terminal with its own on the Ethernet network. on IP address 10.10.0.2.

  • From the upstream direction, the Raspberry Pi will replace all the data from the wireless terminal with the IP address of its own LAN, so for the main router, it only sees the data of the IP address of the Raspberry Pi.
  • From the perspective of the downlink direction, the Raspberry Pi will replace the external data back to the IP address of each wireless terminal, and send the data to the wireless terminal

We need to enable IP masquerading on the Raspberry Pi, which is the NAT function:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Save and automatically enable this IP masquerading function at system startup through the netfilter-persistent service

sudo netfilter-persistent save

Note that this NAT rule is saved in the directory /etc/iptables/.

3.3. Configure DHCP and DNS services for the wireless network

The dnsmasq installed earlier provides DHCP and DNS services. The default configuration file template is very large, but we only need a small function in it, so here we choose to add configuration from an empty file, which is easier.

Back up the original configuration template file and create an empty configuration file:

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

Add the following to the file and save it:

interface=wlan0 # Listening interface
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
                # Pool of IP addresses served via DHCP
domain=wlan     # Local wireless DNS domain
address=/gw.wlan/192.168.4.1
                # Alias for this router

The Raspberry Pi will choose an IP between 192.168.4.2 and 192.168.4.20 to provide an IP address for the wireless terminal, and the lease time is 24 hours. After the DHCP/DNS service works normally, you can also access the Raspberry Pi through the name gw.wlan on the wireless terminal side.

4. Confirm the wireless configuration

To ensure WiFi radio is not blocked on your Raspberry Pi, execute the following command:

Countries around the world have allocated different wifi radio frequency bands by agreement to ensure that they will not interfere with each other. The Linux operating system allows applications to be configured with a two-letter "WiFi country code" (such as US if the computer is used in the United States) to help users comply with these rules.

In the Raspberry Pi operating system, if the wifi country code is not configured, the wireless network in the 5 GHz band is disabled. Usually the wifi country code has been configured during the installation process, of course, it can also be configured through the raspi-config tool.

5. Configure AP software

Create the hostapd configuration file /etc/hostapd/hostapd.conf, and put various parameters related to the wifi wireless network in this file.

sudo vi /etc/hostapd/hostapd.conf

The content is as follows:

Add the following information to the configuration file. This configuration assumes we are using channel 7, the network name is raspberry_wifi_ap, and the password is raspberry_wifi_password. Note that the name and password should not contain quotation marks, and the password length should be between 8 and 64 characters.

country_code=CN
interface=wlan0
ssid=raspberry_wifi_ap
hw_mode=g
channel=7
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=raspberry_wifi_password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
#rsn_pairwise=CCMP

Please note: interface=wlan0 and bridge=br0: When hostapd starts, hostapd will set the network card wlan0 as a member of the Linux bridge br0 according to this configuration, thus completing the bridging between the Ethernet card eth0 and the wireless network card wlan0.

Note the line country_code=GB: it configures the computer for use in the United Kingdom. In China, it needs to be configured as CN.
If you want to: use the 5 GHz band, you can change the operation mode from hw_mode=g to hw_mode=a. hw_mode can be configured to the following values:

  • a = IEEE 802.11a (5 GHz) (requires hardware support, note that Raspberry Pi above 3B+ supports it)
  • b = IEEE 802.11b (2.4 GHz)
  • g = IEEE 802.11g (2.4 GHz)

Note that when changing hw_mode, you may also need to change channel to the corresponding value.

6. Run wifi wireless AP

Reboot the Raspberry Pi and test if the wireless access point is available.

sudo systemctl reboot

After the Raspberry Pi restarts, use a wireless client (such as a computer, mobile phone, etc.) to search for a wireless network. At this point, you should be able to access the external network with the network name and password specified in the file /etc/hostapd/hostapd.conf.

If SSH is enabled on the Raspberry Pi, you can log in to the Raspberry Pi via ssh [email protected] or ssh [email protected] from a wireless terminal.

If your wireless terminal can log in to the Raspberry Pi and connect to the Internet through the above commands (if you have also set up the routing function), it means that the Raspberry Pi wireless router is set up successfully.

Guess you like

Origin blog.csdn.net/meihualing/article/details/130235374