Raspberry Pi AP Feature improvements: SYSTEMD service encapsulation and DNSMASQ usage

Source: Internet
Author: User

Objective
In the previous article, "Raspberry pi set automatic dialing, build no-line by environment" , the author uses HOSTAPD and UDHCPD programs to create wireless hotspots to achieve address assignment and DNS server settings. This article will make improvements to the Raspberry Pi AP based on the previous environment: use DNSMASQ instead of UDHCPD to implement DNS and address leases, and encapsulate AP functionality as a service

The HOSTAPD and UDHCPD used in the previous article have some drawbacks in the way:

    1. Started by using the command line, does not encapsulate the boot itself into a fixed "service"

    2. The built-in network card of the three-generation Raspberry Pi and the name of the external network card used by the author are wlan*, which may cause the number confusion when the system is started.

    3. UDHCPD service configuration file, you need to manually specify the upstream DNS address, making adjustment is not convenient

As the author uses the Kali system is newer Kali rolling, and Debian 9,Ubuntu16.04 as well as Centos/rhel 7.x , SYSTEMD has been used instead of INIT, so for the first flaw, the author uses systemd to encapsulate the service for ease of management.

SYSTEMD is not only able to manage system services, but also to manage the network. For the second flaw, the author makes use of SYSTEMD-NETWORKD to rename the network card device to avoid confusion with the built-in NIC name.

For the third problem, UDHCPD statically specifies the DNS problem, here the author will use DNSMASQ instead, so that the AP client uses the Raspberry Pi upstream DNS for name resolution.

Note: With regard to Init and systemd, it will be elaborated in other blog post, this paper is based on practical application.

    • Modification of the NIC name
      In the Debian whezzy version as well as in centos/rhel6.x , we used to write Udev rules to modify the name of the NIC device, and now we can take advantage of SYSTEMD-NETWORKD, The purpose of modifying the NIC name is achieved by writing a. link file

We need to query the hardware information we want, by the following command:

[Email protected]:~$ sudo udevadm info-a-p/sys/class/net/wlan1 .....] attr{address}== "00:11:22:33:44:55" drivers== "Rt2800usb" ......

Under the/etc/systemd/network directory, create a. link file that reads as follows:

[email protected]:~$ sudo cat/etc/systemd/network/10-ethusb0.link[match]macaddress=00:11:22:33:44:55[link] Description=usb to Ethernet adaptername=ethusb0

Then restart the SYSTEMD-NETWORKD service (if it is already turned on, you can ignore the above), and re-mount the network card driver, with the IP command can see that the NIC name has changed:

[Email protected]:~$ sudo systemctl enable Systemd-networkd.service[email protected]:~$ sudo systemctl start Systemd-networkd.service[email protected]:~$ modprobe-r rt2800usb[email protected]:~$ modprobe rt2800usb[email protected]:~$ sudo ip l SH |  grep ethusb06:ethusb0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc MQ State up mode default group default Qlen 1000

After the configuration file for HOSTAPD (HOSTAPD installation configuration process, please refer to the preface mentioned in the previous post), to make changes, the following changes:

Interface=ethusb0ssid=raspberry_aphw_mode=gchannel=11macaddr_acl=0auth_algs=1ignore_broadcast_ssid=0wpa=2wpa_ passphrase=passwordwpa_key_mgmt=wpa-pskwpa_pairwise=tkiprsn_pairwise=ccmp
    • Installation and configuration of the DNSMASQ
      Here's how to use DNSMASQ, a lightweight application that uses the Raspberry Pi as the DHCP server and the local DNS servers. Let's start with a brief introduction to DNSMASQ. This is a lightweight configuration of DNS proxies and DHCP and TFTP tools for small networks, often used in embedded systems, and the DHCP distribution function for virtual machine instances in OpenStack is also done by DNSMASQ.

First, install DNSMASQ

[Email protected]:~$ sudo apt-get dnsmasq

Query the introduction of DNSMASQ and what is installed:

[email protected]:~$ dpkg-query -l dnsmasqdesired=unknown/install/remove/purge/hold|  Status=not/inst/conf-files/unpacked/half-conf/half-inst/trig-await/trig-pend|/ err?= (None)/ reinst-required  (Status,err: uppercase=bad) | | / name version architecture description+++-======================-================-======= =========-==================================================ii dnsmasq 2.76-5 all small  caching dns proxy and dhcp/tftp server[email protected]:~$ dpkg-query  -l dnsmasq/./etc/etc/default/etc/default/dnsmasq/etc/dnsmasq.conf/etc/dnsmasq.d/etc/dnsmasq.d/ readme/etc/init.d/etc/init.d/dnsmasq/etc/insserv.conf.d/etc/insserv.conf.d/dnsmasq/etc/resolvconf/etc/ resolvconf/update.d/etc/resolvconf/update.d/dnsmasq/lib/lib/systemd/lib/systemd/system/lib/systemd/system/ dnsmasq.service/usr/usr/lib/usr/lib/resolvconf/usr/lib/resolvconf/dpkg-event.d/usr/lib/resolvconf/dPkg-event.d/dnsmasq/usr/share/usr/share/dnsmasq/usr/share/dnsmasq/installed-marker/usr/share/doc/usr/share/doc /dnsmasq[email protected]:~$ sudo dpkg-query -l dnsmasq-base/./etc/etc/dbus-1/etc/ Dbus-1/system.d/etc/dbus-1/system.d/dnsmasq.conf/usr/usr/sbin/usr/sbin/dnsmasq/usr/share/usr/share/ dnsmasq-base/usr/share/dnsmasq-base/trust-anchors.conf/usr/share/doc/usr/share/doc/dnsmasq-base/usr/share/doc/ Dnsmasq-base/dbus-interface.gz/usr/share/doc/dnsmasq-base/faq.gz/usr/share/doc/dnsmasq-base/readme. Debian/usr/share/doc/dnsmasq-base/changelog. Debian.gz/usr/share/doc/dnsmasq-base/changelog.archive.gz/usr/share/doc/dnsmasq-base/changelog.gz/usr/share/ Doc/dnsmasq-base/copyright/usr/share/doc/dnsmasq-base/doc.html/usr/share/doc/dnsmasq-base/examples/usr/share/ doc/dnsmasq-base/examples/dnsmasq.conf.example/usr/share/doc/dnsmasq-base/setup.html/usr/share/locale/usr/ share/locale/de/usr/share/locale/de/lc_messages/usr/share/locale/de/lc_messages/dnsmasq.mo/usr/share/locale/Es/usr/share/locale/es/lc_messages/usr/share/locale/es/lc_messages/dnsmasq.mo/usr/share/locale/fi/usr/share/ Locale/fi/lc_messages/usr/share/locale/fi/lc_messages/dnsmasq.mo/usr/share/locale/fr/usr/share/locale/fr/lc_ messages/usr/share/locale/fr/lc_messages/dnsmasq.mo/usr/share/locale/id/usr/share/locale/id/lc_messages/usr/ Share/locale/id/lc_messages/dnsmasq.mo/usr/share/locale/it/usr/share/locale/it/lc_messages/usr/share/locale/it /lc_messages/dnsmasq.mo/usr/share/locale/no/usr/share/locale/no/lc_messages/usr/share/locale/no/lc_messages/ dnsmasq.mo/usr/share/locale/pl/usr/share/locale/pl/lc_messages/usr/share/locale/pl/lc_messages/dnsmasq.mo/usr/ Share/locale/pt_br/usr/share/locale/pt_br/lc_messages/usr/share/locale/pt_br/lc_messages/dnsmasq.mo/usr/share/ Locale/ro/usr/share/locale/ro/lc_messages/usr/share/locale/ro/lc_messages/dnsmasq.mo/usr/share/man/usr/share/ man/es/usr/share/man/es/man8/usr/share/man/es/man8/dnsmasq.8.gz/usr/share/man/fr/usr/share/man/fr/man8/usr/ Share/man/fr/man8/dnsmAsq.8.gz/usr/share/man/man8/usr/share/man/man8/dnsmasq.8.gz/var/var/lib/var/lib/misc 

As can be seen from the above results, the main program is/USR/SBIN/DNSMASQ, the default configuration file is/etc/dnsmasq.conf, and there are DNSMASQ services under SYSTEMD. However, in this article, in order to encapsulate DNSMASQ and HOSTAPD as a unified new service, the DNSMASQ service that is installed by default in the package and the default configuration file are deprecated. Edit the new profile as shown below, in the following configuration, use Dhcp-options to pass the DNS server to the client for the Raspberry Pi itself 20.20.20.1, and the Raspberry Pi uses the/etc/resolv.conf file as the upstream DNS server for name resolution, and this/ The etc/resolv.conf file is pon dsl-provider automatically acquired when dialing.

[Email protected]:~$ cat/etc/dnsmasq_ap.conf#dhcplisten-address=20.20.20.1,127.0.0.1dhcp-range= 20.20.20.2,20.20.20.20,12hdhcp-option=3,20.20.20.1user=dnsmasq#dnsno-hostscache-size=500resolv-file=/etc/ resolv.conf
    • Service Package
      Once the above process is ready, the following is a unified package for the HOSTAPD,DNSMASQ.

Create the/lib/systemd/system/ap.service file, as shown below. Where the unit section is described as well as the configuration of the service dependency, the Service section does the configuration to start the services, including opening the link, configuring the IP address of the network card, turning on the HOSTAPD service, and turning on the DNSMASQ service. The Install configuration segment specifies that the service uses Multi-user.target, which corresponds to multi-user state.

[Unit] Description=apwants=network.targetbefore=network.targetbindsto=sys-subsystem-net-devices-ethusb0.deviceafter= SYS-SUBSYSTEM-NET-DEVICES-ETHUSB0.DEVICE[SERVICE]TYPE=ONESHOTREMAINAFTEREXIT=YESEXECSTART=/SBIN/IP Link Set Dev ethusb0 UPEXECSTART=/SBIN/IP addr Add 20.20.20.1/24 broadcast 20.20.20.255 Dev ethusb0execstart=/usr/sbin/hostapd-b/ Etc/hostapd/hostapd.confexecstart=/usr/sbin/dnsmasq-c/etc/dnsmasq_ap.confexecstop=/sbin/ip addr Flush Dev ETHUSB0EXECSTOP=/SBIN/IP Link Set Dev ethusb0 down[install]wantedby=multi-user.target

Activate the service with the following command and set it to boot automatically. Observe the IP address, HOSTAPD service, whether the DNSMASQ service has been started:

[email protected]:~$ sudo systemctl enable ap.service[email protected]:~$  Sudo systemctl start ap.service[email protected]:~$ ps -ef | grep  -E  ". *hostapd|. *DNSMASQ " | grep -v greproot 1337 1 0 19:43 ? 00:00:12  /usr/sbin/hostapd -b /etc/hostapd/hostapd.confdnsmasq 1340 1 0 19:43 ?  00:00:00 /usr/sbin/dnsmasq -C /etc/dnsmasq_AP.conf[email protected]:~$ ip  a sh dev ethusb06: ethusb0: <broadcast,multicast,up,lower_up> mtu  1500 qdisc mq state up group default qlen 1000link/ether c8:3a:35 :d 3:1b:4e brd ff:ff:ff:ff:ff:ffinet 20.20.20.1/24 brd 20.20.20.255 scope  global ethusb0valid_lft forever preferred_lft foreverinet6 fe80::ca3a:35ff:fed3:1b4e/64 scope linkvalid_lft forever preferred_lft forever 

The

Attempts to connect Windows as a client Raspberry_ap,cygwin output information as follows:

Wireless LAN Adapter   Wireless network connection:  connection-specific  DNS  suffix  . . . . . . . :   description.  . . . . . . . . . . . . . .  : Realtek RTL8723AE Wireless LAN 802.11n PCI-E NIC  Physical Address.  .  . . . . . . . . . . . : ff-ff-ff-ff-ff-ff  DHCP  enabled  . . . . . . . . . . . :  Yes   Automatic configuration enabled . . . . . . . . . . :  is   local link   ipv6  address.  . . . . . . . : fe80::c011:2b50:411b:56b7%38 (preferred)  IPv4  address  . . . . . . . . . . . . :  20.20.20.8 (preferred)   subnet mask  . . . . . . . . . . .  . : 255.255.255.0  time to obtain lease  . . . . . . . . . : 2017 May 14   19:46:52  Lease expires in  . . . . . . . . . : 2017 May 15  7:46:52  default gateway.  . . . . . . . . . . . .  : 20.20.20.1 DHCP  server  . . . . . . . . .  . . : 20.20.20.1 dhcpv6 iaid . . . . . . .  . . . . : 810821406 DHCPv6  client  duid . . . .  . . . : 00-01-00-01-1C-58-D9-FD-44-8A-5B-ED-33-CF DNS  server  . .  . . . . . . . . . : 20.20.20.1 TCPIP  on the  NetBIOS . . . . . . . :  enabled

Try to ping the great Baidu :

[jiangche00.jiangche00-pc] Ping www.baidu.com is pinging www.a.shifen.com [111.206.223.206] with 32 bytes of data: from 111.206.223.206 Reply: Byte =32 time =7ms ttl=56 reply from 111.206.223.206: Byte =32 time =13ms ttl=56 reply from 111.206.223.206: Byte =32 time =12ms ttl=56 from 111. 206.223.206 reply: Byte =32 time =9ms ttl=56 111.206.223.206 Ping Statistics: packet: Sent = 4, received = 4, lost = 0 (0% missing), estimated time of round trip (in milliseconds): Shortest = 7ms, longest = 13ms, average = 10ms

At this point, the Raspberry Pi AP transformation is complete!

This article is from the "Technical Achievement Dream" blog, please be sure to keep this source http://jiangche00.blog.51cto.com/4377920/1925623

Raspberry Pi AP Feature improvements: SYSTEMD service encapsulation and DNSMASQ usage

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.