Transform the Raspberry Pi into a wireless soft router (1) ----- bridge mode (wireless AP access point)

Raspberry Pi can be used as a wifi wireless access point in the network. Allows computers and devices using wireless access to connect to the network.
The premise of setting it as a wifi wireless AP is that there is already a router in your network, and you just want to use this wireless AP to expand the coverage of WIFI or introduce wireless wifi coverage 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 a router. Now we can use a Raspberry Pi to set it into wifi wireless AP mode, so that the laptop can access the Internet through wifi wirelessly.

                                         +- 树莓派------+
                                     +---+ 10.10.0.2   |          +--记本电脑----+
                                     |   |     WLAN AP +-)))  (((-+ WLAN Client |
                                     |   |  Bridge     |          | 10.10.0.5   |
                                     |   +-------------+          +-------------+
                 +- 路由器 -----+     |
                 | Firewall    |     |   +- PC#2 ------+
(外网)----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 test environment for this document: the latest Raspberry Pi OS bullseys + Raspberry Pi 4B .

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. 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

3. Create a Linux bridge

Build a Linux bridge on the Raspberry Pi, so that the Raspberry Pi's Ethernet card and wireless network can communicate within the Raspberry Pi through the Linux bridge.

3.1. Create a bridge device and fill the bridge

Create a configuration file with the following command, which will be a Linux bridge virtual device named br0. The content of the file is as follows:

sudo vi /etc/systemd/network/bridge-br0.netdev
[NetDev]
Name=br0
Kind=bridge

Use the following commands to connect both the Ethernet card and the wireless network card to this Linux bridge virtual device. First add the built-in ethernet card (eth0) as a bridge member by creating the following file:

sudo vi /etc/systemd/network/br0-member-eth0.network

document content:

[Match]
Name=eth0

[Network]
Bridge=br0

Note:
hostapd will add the wlan0 wireless card to the Linux bridge when the service starts. So there is no need to create a file for the wireless network card wlan0 here. This situation only applies to the wireless LAN interface.

Use the systemd-networkd service to automatically create and configure a network bridge on tree startup:

sudo systemctl enable systemd-networkd

3.2. Define the IP configuration of the bridge device

The NICs that are members of the bridge device work at the link layer, so there is no need to assign IP addresses to them. But the bridge device itself needs an IP address, which is used to allow us to remotely access the Raspberry Pi through the network.

dhcpcd is the DHCP client software running on the Raspberry Pi, it will automatically apply for an IP address for each network card connected to the network. So here we need to let dhcpcd not apply for ip addresses for eth0 and wlan0, and let dhcpcd only apply for IP addresses for the Linux bridge device br0.

sudo vi /etc/dhcpcd.conf

Add the following configuration at the beginning of the file:

denyinterfaces wlan0 eth0

Add the following at the end of the file:

interface br0

This way the Linux bridge br0 will assign an IP address via DHCP.

Save the file and complete the local IP configuration.

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
bridge=br0
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.

Guess you like

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