我想使用dnsmasq获得快速的dns解析,并保持默认的systemd解析。
寻找一种优雅的方式来做到这一点
我想使用dnsmasq获得快速的dns解析,并保持默认的systemd解析。
寻找一种优雅的方式来做到这一点
Answers:
我想通过dnsmasq获得快速的dns解析,并保持默认的systemd-resolved / NetworkManager设置不变以备将来使用。是的,巨大的dnsmasq dns缓存可以提高浏览速度。是的,目标是保留默认的功能DNS设置18.04
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-检查确认
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
我试图找到一个合理的解决方案,并发现有不同的方法。
我想在保持所有业务需求得到满足的同时,最多只留在分销布局中。这是我收集并测试的结果,可用于干净的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服务器分配
如您所知,Docker将主机 /etc/resolv.conf文件复制到容器中,但删除了任何本地名称服务器。
我对此问题的解决方案是继续使用systemd-resolvd和NetworkManager,但添加dnsmasq并将其用于将Docker容器DNS查询“转发” 到systemd-resolvd。
分步指南:
sudo rm /etc/resolv.conf
sudo touch /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
sudo apt-get -y install dnsmasq
# 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
# 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/
Ubuntu 18.10
恕我直言,如果您要运行dnsmasq,则应该静态分配您的IP地址,而不是从dhcp获取它。这样,您就可以一起禁用systemd-resolved了。
须藤apt-get install dnsmasq
sudo systemctl禁用systemd-resolved
sudo systemctl停止systemd-resolved
手动分配您的IP地址,网关,并将IP地址分配给您的计算机作为DNS。
配置/etc/dnsmasq.conf(确实是... RTFM-> man dnsmasq.conf)
sudo systemctl启用dnsmasq
sudo systemctl状态dnsmasq
将dhcp服务器上的dhcp指向闪亮的新dnsmasq服务器(..if yumpto)