• Linux dnsmasq 服务


    在日常开发中,有这么一个需求:

    大家在公司内网同一个网段下,一般情况上网会由网关(一般是路由器)的DHCP服务分配IP。公司内网里放了几台服务器,分别配置成静态IP,这些IP是DHCP配置时预留的。服务器上会放置一些文件共享,所以有个Samba服务器。也配置了一些wiki及git等服务。大家可以使用IP访问这些服务。但难记,IP偶尔也会变,总归不如域名方便。但要每个人都改 hosts 又很麻烦,而且变更起来就更麻烦了。

    一直想在内网布置一个域名解析的服务器,但不知道怎么搞定。今天跟一位IT运维请教后,才恍然大悟。是自己一直想复杂了。

    整个配置过程大概有这么几步:

    1. 取内网的一台服务器,部署一个 dnsmasq 服务作域名解析(不使用其DHCP功能)。
    2. 把内网这些服务的 IP 到域名的映射,设置到 dnsmasq 的配置文件里。
    3. 在DHCP服务上,把默认的DNS地址改成内网的这台服务器IP地址。这样在连网会由DHCP把IP和DNS这些设置分配给机器。

    这样就行了。

    使用域名上网的过程是:

    1. 访问一个域名,但本地 /hosts 里找不到。
    2. 找到DNS服务器,到DNS服务器上去找
    3. DNS 服务器接收到域名访问,如果是本地配置好的,则可直接返回结果。如果本地未配置,则到 dnsmasq 配置的默认上一级 DNS 服务器上找。

    dnsmasq 在 ubuntu 上的配置

    /etc/dnsmasq.conf

    修改几个地方:

    listen-address=127.0.0.1, 10.39.249.211
    

    监听这两个IP上的收到DNS请求

    cache-size=2048
    

    设置缓存的大小。有了缓存,被缓存的域名会立即返回IP地址。这样可以加速访问。

    address=/node0.cpv.org/192.168.1.230
    address=/node1.cpv.org/192.168.1.231
    address=/node2.cpv.org/192.168.1.232
    address=/node3.cpv.org/192.168.1.233
    address=/node4.cpv.org/192.168.1.234
    

    设置局域网内的域名IP映射关系。

    修改本机DNS服务地址

    本机的 DNS 服务地址配成 127.0.0.1。/etc/network/interfaces 里加上这一句:

    dns-nameservers 127.0.0.1
    

    修改 dnsmasq 的上一级DNS服务地址

    dnsmasq 默认会到 /etc/resolv.conf 里去找,但这个文件里放的也是本机的DNS服务地址。上面的修改,把本机所有dns请求从 127.0.0.1 转到了 dnsmasq 上。

    另外,dnsmasq 会读 /var/run/dnsmasq/resolv.conf 这个文件的内容作为上一级DNS服务地址。这个文件是怎么生成的呢:resolvconf 启动时,会调用一个 hook 脚本:/etc/resolvconf/update.d/dnsmasq,这个脚本生成 /var/run/dnsmasq/resolv.conf 文件。

    我看了一下 /var/run/dnsmasq/resolv.conf 里面只有一句 nameserver 127.0.0.1。可能是 resolvconf 这边的脚本没有被执行。

    /etc/default/dnsmasq 里面设置 IGNORE_RESOLVCONF=yes。这样 dnsmasq 就不会去找 /var/run/dnsmasq/resolv.conf,而是直接去读 resolv-file 指定的文件。

    /etc/dnsmasq.conf 设置 resolv-file=/etc/resolv.dnsmasq

    最后创建 /etc/resolv.dnsmasq 文件,并在其中指定上一级 DNS 服务器地址。

    后面这个文件是配置 dnsmasq 的上一级DNS服务器地址:

    nameserver 10.39.249.254
    nameserver 114.114.114.114
    

    参考链接

    https://bugs.launchpad.net/ubuntu/+source/dnsmasq/+bug/1090589
    http://www.thekelleys.org.uk/dnsmasq/docs/dnsmasq-man.html
    https://wiki.archlinux.org/index.php/Dnsmasq_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)

  • 相关阅读:
    What is the difference between google tag manager and google analytics?
    GetService<IMessageBoxService>() returned null.
    Using Google Consent Mode to Adjust Tag Behavior Based on Consent
    what are the values in _ga cookie?
    DEP019 System table or view is deprecated
    How to set the Google Analytics cookie only after another consent cookie is set and "true"?
    Tag Manager and gtag.js
    Using the OptanonWrapper Callback Function
    elk7.15.1版本收集nginx日志并用kibana图形化分析日志
    ELK分析Nginx日志和可视化展示
  • 原文地址:https://www.cnblogs.com/sammei/p/Linux-dnsmasq-fu-wu.html
Copyright © 2020-2023  润新知