Build wireless routing with Raspberry Pi

Source: Internet
Author: User
Tags arch linux

Tip : This article is for Archlinuxarm, and other Linux distributions should be replaced with the corresponding commands according to the actual situation.

Step 1, check if the operating system has detected a wireless card
Run the following command on the terminal:

    1. Lsusb
Copy Code

If you can see your wireless network card (I use the Tenda w311mi, Master Ralink RT5370), it means that the wireless network card has been detected by the operating system, for example, I run LSUSB results are:

    1. Bus 001 Device 002:id 0424:9512 Standard Microsystems Corp. LAN9500 Ethernet 10/100 Adapter
    2. Bus 001 Device 001:id 1d6b:0002 Linux Foundation 2.0 root Hub
    3. Bus 001 Device 003:id 0424:ec00 Standard Microsystems Corp.
    4. Bus 001 Device 004:id 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter
Copy Code

The last line is my wireless card (RT5370 chip).
If the wireless card is not detected by the operating system, you do not have to continue with the steps below.

Step 2, check whether the wireless card supports AP mode
Although most of the network cards that are produced now support AP mode, they do not support all. To complete this step, first install a system tool IW:

    1. Pacman-s IW
Copy Code

(The commands for installing packages from different distributions are not the same, such as Raspbian with Apt-get, Fedora Remix with Yum, PARM Puppy with Petget, Gentoo emerge, SliTaz tazpkg, Slackware AR M with Upgradepkg, OpenSUSE with Zypper ... )
After installation, run:

    1. IW List
Copy Code

Finding the supported interface nodes part of the run results, I ran the following results:

    1. Supported interface modes:
    2. * IBSS
    3. * Managed
    4. * AP
    5. * Ap/vlan
    6. * WDS
    7. * Monitor
    8. * Mesh Point
Copy Code

If you include an AP, it means that your wireless network card supports AP mode. If this step does not pass, you do not have to continue with the steps below.
Tip : If the IW list is running too long for a flash, you can use the more command to display pagination:

    1. IW List | More
Copy Code

If the command results in more than one page, it will stay on the first page. Press the space to turn to the next page, and press ENTER to flip down one line.

Step 3, confirm the Wireless LAN IP segment and set the IP address of the wireless network card
Since the established wireless network belongs to the local area network, it is best to use the LAN IP segment (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
Assume that you use 192.168.34.0/24, or 192.168.34.0-192.168.34.255. Where the IP of the wireless AP is set to 192.168.34.1, and the IP field that the client can use is 192.168.34.100-192.168.34.150. Here is just an example, when operating according to the actual situation is good.
When you're sure, set the IP address of the wireless AP:

    1. IP link set Dev wlan0 up
    2. IP addr Add 192.168.34.1/24 broadcast 192.168.34.255 Dev Wlan0
Copy Code

The wlan0 in the above command is the device name of the wireless card, and you can see the actual name through the ifconfig command.
Once set up, you can view the IP you just set up by ifconfig:

    1. Wlan0:flags=4163<up,broadcast,running,multicast> MTU 1500
    2. inet 192.168.34.1 netmask 255.255.255.0 broadcast 192.168.34.255
    3. Inet6 fe80::ca3a:35ff:feca:7276 Prefixlen ScopeID 0x20<link>
    4. Ether 4c:aa:16:9d:ba:e8 Txqueuelen (Ethernet)
    5. RX Packets 0 Bytes 0 (0.0 B)
    6. RX errors 0 dropped 0 overruns 0 frame 0
    7. TX Packets 0 Bytes 0 (0.0 B)
    8. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Copy Code

Step 4, set up and start the wireless AP service
Use HOSTAPD to establish wireless AP service under Linux, if not installed, you will need to install it through the Package installation tool First:

    1. Pacman-s HOSTAPD
Copy Code

Next is configuring HOSTAPD, using a text compiler (such as Nano) to open the HOSTAPD configuration file

    1. Nano/etc/hostapd/hostapd.conf
Copy Code

When installing HOSTAPD, a default hostapd.conf file is given, it is recommended to back up the original hostapd.conf, and then modify or rewrite it, because there is more complete annotation information, can be used for troubleshooting or future modification of reference materials.
The following is a simple configuration scenario:

  1. Interface=wlan0 # The name of the wireless card used
  2. driver=nl80211 # The driver name of the wireless AP, the default is generally only nl80211, other drivers may need to self-compile and load
  3. Ssid=my_ap # The SSID of the WLAN, which is the name seen on the client
  4. HW_MODE=G # Wireless Card working mode, here Select 802.11g
  5. CHANNEL=7 # Communication channel of wireless card
  6. wpa=2 # using WPA2 encryption
  7. Wpa_passphrase=password # AP Authentication Password
  8. Wpa_key_mgmt=wpa-psk
  9. wpa_pairwise=ccmp
  10. rsn_pairwise=ccmp
Copy Code

The meaning of the main configuration has been given in the subsequent comments. Items that are not configured, HOSTAPD automatically take their default values. If you need to configure other items, you can refer to the instructions for other configuration items in the hostapd.conf file that you have backed up. You can also see whether the wireless network card you are using supports the relevant configuration parameters through the results of the IW list run.
When you finish editing the profile, press [Ctrl] + [O], press [y] to save, and then press [CTRL] + [x] to exit the nano.
Next, start HOSTAPD with the following command:

    1. Systemctl Start HOSTAPD
Copy Code

If the boot succeeds and the configuration is correct, you can scan to the AP in the client.

Step 5, set up and start the DHCP service
The DHCP service is used to assign dynamic IPs to clients. To install before use:

    1. Pacman-s DHCP
Copy Code

When installed, it is necessary to configure it. First edit the file

    1. /etc/conf.d/dhcp
Copy Code

, add a wireless card device name that uses the DHCP service in the Dhcp4_args variable in the QI, such as:

    1. dhcp4_args= "-Q wlan0"
Copy Code

After that, edit the file

    1. /etc/dhcpd.conf
Copy Code

The local Area network layout information that is determined in the 3rd step is filled in:

    1. Subnet 192.168.34.0 netmask 255.255.255.0 {
    2. Range 192.168.34.100 10.0.0.150;
    3. Option routers 192.168.34.1;
    4. Option Domain-name-servers 8.8.8.8, 8.8.4.4;
    5. Option broadcast-address 192.168.34.255;
    6. }
Copy Code

The above option Domain-name-servers is followed by the DNS specified by the client, which uses the public DNS provided by Google, and can be modified if all of your networks provide additional DNS.
Start the DHCP service by using the following command:

    1. Systemctl Start Dhcpd4
Copy Code

If the client is able to connect and obtain the IP, the DHCP boot succeeds.

Step 6, add iptables forwarding rules
Although the wireless APs have been established, the client can also successfully connect to the wireless APs, but at this time can not access the Internet, because the wireless AP is not responsible for data exchange, that is, sent to the wireless AP through the client packets are not sent to its target host, naturally will not have any response.
The completion of the packet forwarding in Linux is iptables, which is the core of the completion of the routing function. Before adding iptables forwarding rules, you must first turn on the IP forwarding function of the kernel:

    1. echo "1" >/proc/sys/net/ipv4/ip_forward
Copy Code

After the addition of the conversion rule, here to use the SNAT target, if the RPi's external network is wired network, the corresponding cable network card IP is 121.121.121.121, then the command to add data forwarding rules:

    1. Iptables-t nat-a postrouting-o eth0-j SNAT--to-source 121.121.121.121
Copy Code

If the IP of the external network is assigned dynamically, you can use the

    1. Iptables-t nat-a postrouting-o Eth0-j Masquerade
Copy Code

This avoids the need to manually modify the corresponding IP address each time you connect to an external network.
At this point, the client can access the Internet over a wireless AP.

Step 7, let the RPi start the corresponding service automatically, adapt to unattended application scenarios
Although no line can be used normally, as long as the RPi restarts many services and configuration information will be restored as is. To avoid having to reset the relevant parameters and start related services each time you start the system, you can configure the system to perform these repetitive tasks automatically at startup by using the following steps.
(1) Automatically set the IP address of the wireless network card
Add a new file, save As

    1. /etc/systemd/system/wifi.service
Copy Code

Write the following in the file, and note that the appropriate content is modified according to the actual situation:

    1. [Unit]
    2. Description=wireless Static IP Connectivity
    3. Wants=network.target
    4. Before=network.target
    5. [Service]
    6. Type=oneshot
    7. Remainafterexit=yes
    8. Environmentfile=/etc/conf.d/wifi
    9. EXECSTART=/SBIN/IP Link Set Dev wlan0 up
    10. EXECSTART=/SBIN/IP addr Add 192.168.34.1/24 broadcast 192.168.34.255 Dev Wlan0
    11. EXECSTOP=/SBIN/IP addr Flush Dev Wlan0
    12. EXECSTOP=/SBIN/IP Link Set Dev wlan0
    13. [Install]
    14. Wantedby=multi-user.target
Copy Code

The meaning of the configuration content can be viewed on the Arch Linux wiki.
To run the script automatically when the system starts, you only need to run the following command:

    1. Systemctl Enable WiFi
Copy Code

(2) automatically start HOSTAPD and DHCP services

    1. Systemctl Enable HOSTAPD
    2. Systemctl Enable DHCPD4
Copy Code

(3) Automatically set iptables forwarding rules
After you have added the forwarding rules, save the forwarding rules to the

    1. /etc/iptables/iptables.rules
Copy Code

In this file, the rules are added automatically every time iptables is started.
Then let Iptables run automatically when the system starts:

    1. Systemctl Enable Iptables
Copy Code

At this point, the non-line on the RPi by the construction completed.


PS:
1, about restricting the method of client access, you can view the HOSTAPD configuration of the macaddr_acl parameters of the relevant description;
2. For a method of setting up a network firewall, you can view the iptables documentation.

Build wireless routing with Raspberry Pi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.