编程笔记

lifelong learning & practice makes perfect

coredns|局域网域名解析

安装

使用docekr安装,默认为linux/x86_64

1
2
3
4
5
6
7
8
9
10
11
services:
coredns:
image: coredns/coredns:latest
ports:
- 53:53/udp
restart: always
command: -conf /root/Corefile
volumes:
- ./config:/root
hostname: coredns
container_name: coredns

配置文件 Corefile

1
2
3
4
5
.:53 {
forward . 223.5.5.5 223.6.6.6
log
errors
}

如图

在启动CoreDNS之前,您需要确保系统中的53端口没有被其他服务占用。您提供的命令sudo lsof -i :53用于查看有无进程正在使用53端口。

53端口占用

如果发现systemd-resolve服务正在使用这个端口,您可以使用以下命令来停止并禁用该服务

1
2
3
4
sudo lsof -i :53
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd-r 4016798 systemd-resolve 13u IPv4 9156404 0t0 UDP localhost:domain
systemd-r 4016798 systemd-resolve 14u IPv4 9156405 0t0 TCP localhost:domain (LISTEN)

停止,禁用systemd-resolved服务

1
2
sudo systemctl stop systemd-resolved
sudo systemctl disable systemd-resolved

停止后,在fish shell里发现报错

1
2
3
4
5
6
7
8
9
10
11
12
sudo lsof -i :53
sudo: 无法解析主机:server: 域名解析出现暂时性错误
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 4020073 root 3u IPv4 9169243 0t0 UDP 172.23.0.1:36982->172.23.0.2:domain
docker-pr 4020073 root 4u IPv4 9159545 0t0 UDP *:domain
docker-pr 4020073 root 8u IPv4 9169244 0t0 UDP 172.23.0.1:49782->172.23.0.2:domain
docker-pr 4020073 root 9u IPv4 9159669 0t0 UDP 172.23.0.1:40376->172.23.0.2:domain
docker-pr 4020073 root 10u IPv4 9175075 0t0 UDP 172.23.0.1:41665->172.23.0.2:domain
docker-pr 4020073 root 11u IPv4 9179143 0t0 UDP 172.23.0.1:35846->172.23.0.2:domain
docker-pr 4020080 root 4u IPv6 9175047 0t0 UDP *:domain
sshd 4020645 root 5u IPv4 9165329 0t0 UDP localhost:38262->127.0.0.53:domain
sshd 4020728 root 5u IPv4 9146662 0t0 UDP localhost:54842->127.0.0.53:domain

无法解析主机

  1. 检查DNS服务器是否正确配置。您可以通过运行cat /etc/resolv.conf来查看DNS服务器的配置。

cat /etc/resolv.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0 trust-ad
search .

nameserver 127.0.0.53 改成 127.0.0.1

  1. /etc/hosts增加配置
1
127.0.0.1 server

参考

欢迎关注我的其它发布渠道