如何添加 dnsmasq 并保持 systemd-resolved(18.04 到 20.04)

cma*_*.fr 14 network-manager dns dnsmasq systemd-resolved

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

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

cma*_*.fr 12

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

  1. 安装 dnmasq
  2. 配置它(监听地址和dns服务器)
  3. 为手动 dns 服务器地址配置 NetWorkManager
  4. 检查验证

1 - 使用 sudo

apt-get -y install dnsmasq
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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"
Run Code Online (Sandbox Code Playgroud)

4 - 检查验证

  • systemd-resolved 在 127.0.0.53 上默认监听
  • 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
Run Code Online (Sandbox Code Playgroud)