Location>code7788 >text

Raspberry Pi power on self-starting hotspot (using wired eth0 for internet access and wlan0 for hotspot)

Popularity:961 ℃/2024-11-08 17:26:01

I. Background content

Actually, I just took an order from someone who needed me to help configure a Raspberry Pi boot hotspot. This way to make a record, the way raspberry pi 4B, 3B can be used.

II. Practical exercises

1. Use the network cable to connect the router and Raspberry Pi

There are three network interfaces on the Raspberry Pi, which are:

  • eth0: wired network interface (Ethernet interface)
  • wlan0: wireless network interface (WiFi interface)
  • lo: local loopback interface (for local communication, localhost: 127.0.0.1)

Since we need to configure hotspot, we need to configure wlan0, so its WiFi function needs to be turned off, and here we use eth0 to connect to the network, which has been configured by default to obtain an IP address automatically through DHCP. After connecting to the network, pass the

ifconfig

Check if eth0 has a fixed IP address for determining whether the network is connected.
image

2、Close the network connection of wlan0

Terminal Input:

ip route

It can be seen:
image
Explain that my device is connected to the network through eth0 and wlan0, the first point has been mentioned the need to use wlan0 to open the hotspot, so here you need to disconnect the wifi connection function of wlan0 and then configure.
Terminal Input:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Delete everything in there, then save and exit to restart the service, one click and three links~~~

3. Raspberry Pi for source

Since you will need to use apt install to install Linux packages, please make sure that your Raspberry Pi's apt source is OK. I won't expand too much here, but will add some other links later.

4、hostapd

hostapd allows you to configure the device's wireless network interface to hotspot mode, making it a soft AP that accepts connections from other devices.

(1) Installation and stopping of services

Terminal to enter commands for installation:

sudo apt install hostapd

Stop the hostapd service:

sudo systemctl stop hostapd

(2) Configuration of hotspot parameters

Terminal type: (if you don't use this file, just create a new one in this path)

sudo nano /etc/hostapd/

Fill in:

interface=wlan0
driver=nl80211
ssid=???
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=???
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

ssid is the hotspot name; wpa_passphrase is the hotspot password, modified as needed.
After filling out the form, it looks like this.
image

(3) Specify the path to the hotspot configuration file for hostapd

Terminal Input:

sudo nano /etc/default/hostapd

Remove the comments from DAEMON_CONF and configure it to /etc/hostapd/ as shown. It means to tell hostapd to read the configuration parameters from /etc/hostapd/. The details are shown in the figure:
image
Finally, restart the hostapd service by typing in the terminal:

sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl start hostapd

Wait a bit and you can see the generated hotspot signal. However, the hotspot cannot connect at this time because this hotspot signal is not connected to the network and cannot assign an IP to the client.
image

5、dhcpcd

dhcpcd is a package used to perform IP address related operations, here we use this package for hotspot IP address fixing.
Terminal to enter commands for installation:

(1) Install dhcpcd

sudo apt install dhcpcd

(2) Edit configuration file

Edit the dhcpcd configuration file. terminal entry:

sudo nano /etc/

Delete the entire contents and enter:

interface wlan0
    static ip_address=192.168.4.1/24
    nohook wpa_supplicant

Here set the static ip_address best not and your surrounding wireless network in the same network segment, such as your home wireless network network segment is 192.168., then here the static IP of the third bit is set to other good!

After saving, restart the dhcpcd service and type in the terminal:

sudo systemctl restart dhcpcd

(3) Inspection

After that, check the IP address of wlan0 by typing in the terminal

ifconfig

You can see that the IP address is fixed, as shown in the figure:
image

6、dnsmasq

The dnsmasq package is used to automatically assign IP addresses to tropical-connected devices.

(1) Install dnsmasq

Terminal Input:

sudo apt install dnsmasq

Discontinue its services:

sudo systemctl stop dnsmasq

(2) Configuration parameters

Terminal Input:

sudo nano /etc/

Delete it in its entirety and fill in:

interface=wlan0
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h

Meaning: The dhcp service allocates IP space from 192.168.4.2 to 192.168.4.20 to clients with a 24-hour lease.
As pictured:
image
After that restart the service. Terminal input:

sudo systemctl reload dnsmasq

If an error is reported, execute the following command:

sudo systemctl unmask dnsmasq
sudo systemctl enable dnsmasq
sudo systemctl start dnsmasq

(3) Try to connect to the hotspot

After the service is restarted, the hotspot can connect. This is because the Raspberry Pi assigns IPs to the devices connected to the hotspot. but at this point there is no Internet access.
image

7. Enable IP forwarding

Terminal Input:

sudo nano /etc/

Find and uncomment the following lines:

net.ipv4.ip_forward=1

As pictured:
image

8. Configure the Raspberry Pi firewall

Terminal Input:

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

Save the firewall rule afterward:

sudo sh -c "iptables-save > /etc/iptables."

After that have the device reload this firewall rule every time it reboots:

sudo nano /etc/

Add iptables-restore < /etc/iptables. to the last line before exit 0, as shown:
image

9, restart the device, enjoy yourself~!

Restart all services:

sudo systemctl unmask hostapd
sudo systemctl unmask dnsmasq
sudo systemctl enable hostapd
sudo systemctl enable dnsmasq
sudo systemctl start hostapd
sudo systemctl start dnsmasq

Reboot the device:

sudo reboot