默认安装好 ubuntu server 22.04LTS 后,系统使用 systemd-resolved 53 端口端口的 dns 服务为本机提供服务。
Ubuntu 的 systemd-resolved 将默认监听在 53 号端口,如果我们需要运行自己定义的 dns 服务器,端口已经在使用会导致端口冲突。所以我们会遇见下面的错误:
1
  | 
"listen tcp 0.0.0.0:53: bind: address already in use".
  | 
 
查看端口情况
1
  | 
root@sunday:~# netstat -lnpt|grep 53Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program nametcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      2119/systemd-resolv
  | 
 
或者
1
  | 
root@sunday:~# sudo lsof -i :53COMMAND    PID            USER   FD   TYPE DEVICE SIZE/OFF NODE NAMEsystemd-r 2119 systemd-resolve   12u  IPv4  67939      0t0  UDP localhost:domainsystemd-r 2119 systemd-resolve   13u  IPv4  67940      0t0  TCP localhost:domain (LISTEN)
  | 
 
如何停止 ubuntu 上的 systemd-resolved 服务使用 53
1.修改配置文件
修改/etc/systemd/resolved.conf 中 DNSStubListener 的注释行,它将不再打开 dns 服务
1
  | 
root@sunday:~# cat /etc/systemd/resolved.conf#  This file is part of systemd.##  systemd is free software; you can redistribute it and/or modify it#  under the terms of the GNU Lesser General Public License as published by#  the Free Software Foundation; either version 2.1 of the License, or#  (at your option) any later version.## Entries in this file show the compile time defaults.# You can change settings by editing this file.# Defaults can be restored by simply deleting this file.## See resolved.conf(5) for details[Resolve]#DNS=#FallbackDNS=#Domains=#LLMNR=no#MulticastDNS=no#DNSSEC=no#DNSOverTLS=no#Cache=no-negative#DNSStubListener=yes  将这行的注释拿掉,改为no保存,如下DNSStubListener=no  #ReadEtcHosts=yes
  | 
 
2.创建链接
将下面的文件创建一个软链接到 etc 文件夹下
1
  | 
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
  | 
 
3.生效配置
1
  | 
systemctl restart systemd-resolved.service
  | 
 
检查 53 是否使用:
批量脚本
1
  | 
sed -i 's/^#\?DNSStubListener=.*/DNSStubListener=no/' /etc/systemd/resolved.confln -sf /run/systemd/resolve/resolv.conf /etc/resolv.confsystemctl restart systemd-resolved.service
  |