如何添加dnsmasq并保持systemd解析(18.04)


10

我想使用dnsmasq获得快速的dns解析,并保持默认的systemd解析。

寻找一种优雅的方式来做到这一点


您是否意识到systemd-resolved还会缓存DNS查找?在具有systemd-resolved的系统上,您不需要dnsmasq。您应该阅读systemd-resolved有用吗?
Aeyoun

我知道-但是有一个非常重要的“细节” ...-dnsmasq作为巨大的dns缓存-systemd-resolved的dns缓存非常小且无法调整-巨大的缓存使dns解析速度得到了极大提高dnsmasq的请求已填充。
cmak.fr

systemd-resolved中的默认未配置缓存实际大于dnsmasq。
Aeyoun

实际上,dnsmasq的功能远不止systemd-resolved。见gist.github.com/jult/4eba88bdd34a57cc79d6#gistcomment-1706666gist.github.com/jult/4eba88bdd34a57cc79d6#file-hostsupdater-sh的名字,但很少..
朱利叶斯

@Aeyoun:通过阅读systemd解析的源代码,您可以看到4096的DNS缓存限制是字节,而不是条目。dnsmasq具有更大且可配置的dns缓存大小。
cmak.fr

Answers:


10

我想通过dnsmasq获得快速的dns解析,并保持默认的systemd-resolved / NetworkManager设置不变以备将来使用。是的,巨大的dnsmasq dns缓存可以提高浏览速度。是的,目标是保留默认的功能DNS设置18.04

  1. 安装dnmasq
  2. 配置它(监听地址和DNS服务器)
  3. 将NetWorkManager配置为手动DNS服务器地址
  4. 检查确认

1-使用sudo

apt-get -y install dnsmasq

2-使用sudo

tee -a /etc/dnsmasq.conf << ENDdm
interface=lo
bind-interfaces
listen-address=127.0.0.1
# DNS server from OpenDns. Use yours...
server=208.67.222.222
server=208.67.220.220
ENDdm

systemctl restart dnsmasq
systemctl enable dnsmasq

3-使用USER,配置NetworkManager

# Get NM first active profile name
NetManProfile=$(nmcli -t  connection show --active | cut -f 01 -d ':')
# remove, if exists, current dns servers
nmcli con mod "$NetManProfile" ipv4.dns ""
# set 'manual' dns server
nmcli con mod "$NetManProfile" ipv4.ignore-auto-dns yes
# set dnsmasq as manually set dns server
nmcli con mod "$NetManProfile" ipv4.dns 127.0.0.1
# i also disabled ip6, do what u want
nmcli con mod "$NetManProfile" ipv6.method ignore
# reconnect to take effect
nmcli connection down "$NetManProfile"
nmcli connection up "$NetManProfile"

4-检查确认

  • 默认情况下应在127.0.0.53上进行systemd-resolved侦听
  • dnsmasq侦听/ etc / dnsmasq中设置的127.0.0.1
  • systemd-resolved从NetworkManager获得了127.0.0.1
netstat -antup
Proto Recv-Q Send-Q Adresse locale          Adresse distante        Etat       PID/Program name    
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      1036/dnsmasq        
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      766/systemd-resolve
cat /run/systemd/resolve/resolv.conf 
nameserver 127.0.0.1

1

我试图找到一个合理的解决方案,并发现有不同的方法。

我想在保持所有业务需求得到满足的同时,最多只留在分销布局中。这是我收集并测试的结果,可用于干净的Ubuntu 18.04和KDE Neon风味:

# Install required package and reconfigure service plans (i.e. disablesystemd-resolved, enable dnsmasq
sudo apt-get install dnsmasq
sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved
sudo systemctl enable dnsmasq

# These two lines should work on most environments, but .. :-) - so I kept them commented out for less experienced users
# Just add or change 'dns=dnsmasq' to your NetworkManager.conf to the section [main]
# and yes, the sed expression can be better :-)

#sudo cp /etc/NetworkManager/NetworkManager.conf /etc/NetworkManager/NetworkManager.conf.backup
#sudo bash -c 'cat /etc/NetworkManager/NetworkManager.conf.backup |sed -e "s/^dns=.*//"| sed -e "s/\[main\]/\[main\]\ndns=dnsmasq/" >/etc/NetworkManager/NetworkManager.conf'

# Restart NetworkManager to make the change above applied
sudo systemctl restart NetworkManager

# This removes the systemd resolv.conf link only if it has NetworkManager replacement :-)
ls /var/run/NetworkManager/resolv.conf && sudo rm /etc/resolv.conf

# And add NetworkManager's resolv.conf available for the system resolver
sudo ln -s /var/run/NetworkManager/resolv.conf /etc/resolv.conf

(请注意,上述答案的唯一一般区别是NetworkManager会自动处理dnsmasq DNS服务器分配


可以,但是使用apt-get
--upgrade

老实说,我不知道。这取决于Ubuntu 18.04的维护者决定。如果他/她保持禁用systemd解析的服务,并且不会修改NetworkManager.conf(默认情况下会在发生冲突时以交互方式解析),那么它可以生存直到Ubuntu 20.04有望修复。
Venca B垃圾邮件

0

如您所知,Docker主机 /etc/resolv.conf文件复制到容器中,但删除了任何本地名称服务器。

我对此问题的解决方案是继续使用systemd-resolvdNetworkManager,但添加dnsmasq并将其用于将Docker容器DNS查询“转发” 到systemd-resolvd

分步指南:

  • 使/etc/resolv.conf为“真实”文件 sudo rm /etc/resolv.conf sudo touch /etc/resolv.conf
  • 创建文件/etc/NetworkManager/conf.d/systemd-resolved-for-docker.conf以告诉NetworkManager通知systemd-resolvd,但不要触摸/etc/resolv.conf [main] # NetworkManager will push the DNS configuration to systemd-resolved dns=systemd-resolved # NetworkManager won’t ever write anything to /etc/resolv.conf rc-manager=unmanaged
  • 安装dnsmasq sudo apt-get -y install dnsmasq
  • /etc/dnsmasq.conf中配置dnsmasq,以侦听来自Docker的DNS查询,并使用systemd-resolvd名称服务器 # Use interface docker0 interface=docker0 # Explicitly specify the address to listen on listen-address=172.17.0.1 # Looks like docker0 interface is not available when dnsmasq service starts so it fails. This option makes dynamically created interfaces work in the same way as the default. bind-dynamic # Set systemd-resolved DNS server server=127.0.0.53
  • 编辑/etc/resolv.conf以在Docker网络中 使用systemd-resolvd名称服务器(127.0.0.53)和主机IP(172.17.0.1) # systemd-resolvd name server nameserver 127.0.0.53 # docker host ip nameserver 172.17.0.1
  • 重新启动服务 sudo service network-manager restart sudo service dnsmasq restart sudo service docker restart

有关更多信息,请参阅我的帖子(西班牙语)https://rubensa.wordpress.com/2020/02/07/docker-no-usa-los-mismos-dns-que-el-host/


-1

Ubuntu 18.10

恕我直言,如果您要运行dnsmasq,则应该静态分配您的IP地址,而不是从dhcp获取它。这样,您就可以一起禁用systemd-resolved了。

  1. 须藤apt-get install dnsmasq

  2. sudo systemctl禁用systemd-resolved

  3. sudo systemctl停止systemd-resolved

  4. 手动分配您的IP地址,网关,并将IP地址分配给您的计算机作为DNS。

  5. 配置/etc/dnsmasq.conf(确实是... RTFM-> man dnsmasq.conf)

  6. sudo systemctl启用dnsmasq

  7. 重启
  8. sudo systemctl状态dnsmasq

  9. 将dhcp服务器上的dhcp指向闪亮的新dnsmasq服务器(..if yumpto)


我不会执行您描述的内容。我希望保留默认的systemd-resolved以便将来最终使用NetworkManager。
cmak.fr,
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.