默认安装好ubuntu server 22.04LTS后,系统使用systemd-resolved 53端口端口的dns服务为本机提供服务。
Ubuntu的systemd-resolved将默认监听在53号端口,如果我们需要运行自己定义的dns服务器,端口已经在使用会导致端口冲突。所以我们会遇见下面的错误:
"listen tcp 0.0.0.0:53: bind: address already in use".
查看端口情况
root@sunday:~# netstat -lnpt|grep 53
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 2119/systemd-resolv
或者
root@sunday:~# sudo lsof -i :53
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd-r 2119 systemd-resolve 12u IPv4 67939 0t0 UDP localhost:domain
systemd-r 2119 systemd-resolve 13u IPv4 67940 0t0 TCP localhost:domain (LISTEN)
如何停止ubuntu上的systemd-resolved服务使用53
1.修改配置文件
修改/etc/systemd/resolved.conf中DNSStubListener的注释行,它将不再打开dns服务
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文件夹下
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
3.生效配置
systemctl restart systemd-resolved.service
检查53是否使用:
lsof -i :53
批量脚本
sed -i 's/^#\?DNSStubListener=.*/DNSStubListener=no/' /etc/systemd/resolved.conf
ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
systemctl restart systemd-resolved.service