前言
在上一篇《Raspberry pi 设置自动拨号搭建无线路由环境》一文中,笔者利用hostapd和udhcpd程序,创建无线热点,实现地址分配以及DNS服务器的设置。本篇将基于上一篇的环境,对树莓派AP进行改进:使用dnsmasq代替udhcpd实现DNS以及地址租约,并将AP功能封装成为一个服务
上一篇中使用的hostapd以及udhcpd,其方式有一些缺陷:
使用命令行方式启动,并未将启动本身封装成为一个固定的“服务”
三代树莓派的内置网卡以及笔者使用的外置网卡的名称均为wlan*,在系统启动的时候有可能造成编号的错乱
udhcpd服务的配置文件里面,需要手动指定上游DNS的地址,使得调整起来并不方便
由于笔者使用的Kali系统是较新的Kali rolling,和Debian 9,Ubuntu16.04以及CentOS/RHEL 7.x一样,已经使用systemd代替了init,因此,针对第一个缺陷,笔者使用systemd将服务进行封装,方便进行管理。
systemd不仅能够管理系统服务,同时对于网络进行管理。针对第二个缺陷,笔者通过利用systemd-networkd,进行网卡设备的重命名,从而避免和内置网卡名称的混淆
针对第三个问题,udhcpd的静态指定DNS的问题,这里笔者将采用dnsmasq来代替,从而让AP客户端使用树莓派的上游DNS进行名称解析。
注:关于init以及systemd,将会在其他的博文中详细阐述,本文以实际应用为主
网卡名称的修改
在Debian whezzy版本以及CentOS/RHEL6.x当中,我们利用编写udev规则来修改网卡设备的名称,而现在我们能够利用systemd-networkd,通过编写.link文件,达到修改网卡名称的目的
我们需要查询到我们想要的硬件信息,通过如下命令:
linuxidc@kali:~$ sudo udevadm info -a -p /sys/class/net/wlan1
......
......
ATTR{address}=="00:11:22:33:44:55"
DRIVERS=="rt2800usb"
......
......
在/etc/systemd/network目录下面创建一个.link文件,内容如下:
linuxidc@kali:~$ sudo cat /etc/systemd/network/10-ethusb0.link
[Match]
MACAddress=00:11:22:33:44:55
[Link]
Description=USB to Ethernet Adapter
Name=ethusb0
之后重启开启systemd-networkd这个服务(如果已经开启了,可以忽略上述操作),并且重新挂载网卡驱动,用ip命令可以查看到,网卡名称已经改变:
linuxidc@kali:~$ sudo systemctl enable systemd-networkd.service
linuxidc@kali:~$ sudo systemctl start systemd-networkd.service
linuxidc@kali:~$ modprobe -r rt2800usb
linuxidc@kali:~$ modprobe rt2800usb
linuxidc@kali:~$ sudo ip l sh | grep ethusb0
6: ethusb0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
之后对于hostapd的配置文件(hostapd的安装配置过程,请详见前言中提到的上一篇博文),进行修改,修改之后的内容如下:
interface=ethusb0
ssid=raspberry_AP
hw_mode=g
channel=11
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=PASSWORD
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
dnsmasq的安装以及配置
下面介绍如何利用dnsmasq这个轻量级的应用来将树莓派作为DHCP服务端以及本地DNS服务器。首先简单介绍一下dnsmasq。这是一款轻量级的配置DNS代理以及DHCP和TFTP的工具,适用于小型网络,经常用于嵌入式系统,而且Openstack当中针对虚机实例的DHCP分发功能也是由dnsmasq来完成的。
首先,安装dnsmasq
linuxidc@kali:~$ sudo apt-get dnsmasq
查询一下dnsmasq的介绍以及都安装了哪些内容:
linuxidc@kali:~$ dpkg-query -l dnsmasq
Desired=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
linuxidc@kali:~$ 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
linuxidc@kali:~$ 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