Ubuntu使用dnsmasq自建dns服务

前言

服务上自定义了一些域名,当有用户在局域网中能够通过自定义域名访问到这些服务。

环境

  • Ubuntu 20.04
  • dnsmasq

操作

安装dnsmasq

sudo apt update
sudo apt install dnsmasq

安装好之后,查看一下服务状态

sudo service dnsmasq status

发现服务启动失败

● dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server
     Loaded: loaded (/lib/systemd/system/dnsmasq.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Sat 2022-01-29 09:05:45 UTC; 5s ago
    Process: 149548 ExecStartPre=/usr/sbin/dnsmasq --test (code=exited, status=0/SUCCESS)
    Process: 149549 ExecStart=/etc/init.d/dnsmasq systemd-exec (code=exited, status=2)

Jan 29 09:05:45 ubuntu systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...
Jan 29 09:05:45 ubuntu dnsmasq[149548]: dnsmasq: syntax check OK.
Jan 29 09:05:45 ubuntu dnsmasq[149549]: dnsmasq: failed to create listening socket for port 53: Address already in use
Jan 29 09:05:45 ubuntu dnsmasq[149549]: failed to create listening socket for port 53: Address already in use
Jan 29 09:05:45 ubuntu systemd[1]: dnsmasq.service: Control process exited, code=exited, status=2/INVALIDARGUMENT
Jan 29 09:05:45 ubuntu dnsmasq[149549]: FAILED to start up
Jan 29 09:05:45 ubuntu systemd[1]: dnsmasq.service: Failed with result 'exit-code'.
Jan 29 09:05:45 ubuntu systemd[1]: Failed to start dnsmasq - A lightweight DHCP and caching DNS server.

检查 systemd-resolved 服务状态,发现是启动的

sudo service systemd-resolved status
● systemd-resolved.service - Network Name Resolution
     Loaded: loaded (/lib/systemd/system/systemd-resolved.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-01-29 09:03:56 UTC; 4min 42s ago
       Docs: man:systemd-resolved.service(8)
             https://www.freedesktop.org/wiki/Software/systemd/resolved
             https://www.freedesktop.org/wiki/Software/systemd/writing-network-configuration-managers
             https://www.freedesktop.org/wiki/Software/systemd/writing-resolver-clients
   Main PID: 149486 (systemd-resolve)
     Status: "Processing requests..."
      Tasks: 1 (limit: 5814)
     Memory: 7.1M
     CGroup: /system.slice/systemd-resolved.service
             └─149486 /lib/systemd/systemd-resolved

Jan 29 09:03:55 ubuntu systemd[1]: Starting Network Name Resolution...
Jan 29 09:03:56 ubuntu systemd-resolved[149486]: Positive Trust Anchors:
Jan 29 09:03:56 ubuntu systemd-resolved[149486]: . IN DS 20326 8 2 e06d44b80b8f1d39a95c0b0d7c65d08458e880409bbc683457104237c7f8ec8d
Jan 29 09:03:56 ubuntu systemd-resolved[149486]: Negative trust anchors: 10.in-addr.arpa 16.172.in-addr.arpa 17.172.in-addr.arpa 18.172.in-addr.arpa 19.172.in-addr.arpa 20.172.in-addr.arpa 21.172.in-addr.arpa>
Jan 29 09:03:56 ubuntu systemd-resolved[149486]: Using system hostname 'ubuntu'.
Jan 29 09:03:56 ubuntu systemd[1]: Started Network Name Resolution.

这时候,选择停止 system-resolved 服务

sudo service systemd-resolved stop

再次启动 dnsmasq 服务

sudo service systemd-resolved start

查看 dnsmasq 服务状态

● systemd-resolved.service - Network Name Resolution
     Loaded: loaded (/lib/systemd/system/systemd-resolved.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-01-29 09:13:11 UTC; 3s ago
       Docs: man:systemd-resolved.service(8)
             https://www.freedesktop.org/wiki/Software/systemd/resolved
             https://www.freedesktop.org/wiki/Software/systemd/writing-network-configuration-managers
             https://www.freedesktop.org/wiki/Software/systemd/writing-resolver-clients
   Main PID: 149674 (systemd-resolve)
     Status: "Processing requests..."
      Tasks: 1 (limit: 5814)
     Memory: 4.3M
     CGroup: /system.slice/systemd-resolved.service
             └─149674 /lib/systemd/systemd-resolved

Jan 29 09:13:11 ubuntu systemd[1]: Starting Network Name Resolution...
Jan 29 09:13:11 ubuntu systemd-resolved[149674]: Positive Trust Anchors:
Jan 29 09:13:11 ubuntu systemd-resolved[149674]: . IN DS 20326 8 2 e06d44b80b8f1d39a95c0b0d7c65d08458e880409bbc683457104237c7f8ec8d
Jan 29 09:13:11 ubuntu systemd-resolved[149674]: Negative trust anchors: 10.in-addr.arpa 16.172.in-addr.arpa 17.172.in-addr.arpa 18.172.in-addr.arpa 19.172.in-addr.arpa 20.172.in-addr.arpa 21.172.in-addr.arpa>
Jan 29 09:13:11 ubuntu systemd-resolved[149674]: Using system hostname 'ubuntu'.
Jan 29 09:13:11 ubuntu systemd[1]: Started Network Name Resolution.

配置 dnsmasq

创建文件 diy.dns(这个名字随便起),并写入配置

sudo tee /etc/dnsmasq.d/diy.dns <<- 'EOF'
# 完整的域名才向上游服务器查找,如果仅仅是主机名仅查找hosts文件
domain-needed
# 指定不提供 DHCP 或 TFTP 服务的接口,仅提供 DNS 服务。
no-dhcp-interface=eth0
# 如果反向查找的是私有地址例如192.168.X.X,仅从 hosts 文件查找,不再转发到上游服务器
bogus-priv
# 缓存条数,默认为150条,cache-size=0 禁用缓存
cache-size=2000
# 指定 resolv-file 文件路径,默认/etc/resolv.conf
resolv-file=/etc/resolv.dnsmasq.conf
EOF

创建文件 /etc/resolv.dnsmasq.conf(名字可以自定义,只需要和上面的 resolve-file 配置一致就好),并写入配置

sudo tee /etc/resolv.dnsmasq.conf <<- 'EOF'
nameserver 8.8.8.8
nameserver 114.114.114.114
EOF

配置好之后,重启服务

sudo service dnsmasq restart

检查服务是否可用

/etc/hosts 中自定义一个服务,并重启 dnsmasq

echo "127.0.0.1 test.domain.com" | sudo tee -a /etc/hosts
sudo service dnsmasq restart

使用 dig 命令查看服务是否正常

# 这里的 192.168.137.128 是 dnsmasq 所在服务器的地址
dig @192.168.137.128 test.domain.com

结果如下,成功解析出 test.domain.comip 就是 127.0.0.1

; <<>> DiG 9.16.1-Ubuntu <<>> @192.168.137.128 test.domain.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60222
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;test.domain.com.               IN      A

;; ANSWER SECTION:
test.domain.com.        0       IN      A       127.0.0.1

;; Query time: 0 msec
;; SERVER: 192.168.137.128#53(192.168.137.128)
;; WHEN: Sat Jan 29 09:29:20 UTC 2022
;; MSG SIZE  rcvd: 60

最后

搞起来搞起来

上一篇:Ubuntu 开启自启动脚本以及带图形界面脚本(imshow())


下一篇:(GCJ-2009 Round 2C) Stock Charts (最小路径覆盖)