NetworkManager Conflict with DNS Caching?

1 comment

NetworkManager is a program for providing detection and configuration for systems to automatically connect to network. Your network can easily be shared to others using it. But If you want to implement DNS caching on your machine, you will find it annoying. I used to face conflict between NetworkManager/ConnectionSharing and DNS Caching, but now I find a workaround.


My DNS Caching Configure

/etc/NetworkManager/NetworkManager.conf
[main]
dhcp=dhclient
dns=dnsmasq

It works, but you need root privilege to start networksharing, like this: sudo nmcli con up xxxx. I have also tried using systemd/dnsmasq, but it doesn’t work at all.


When you start a shared network, NetworkManager will start dnsmasq listening on port 53.
Type pgrep -a dnsmasq, you will get:

/usr/bin/dnsmasq --conf-file --no-hosts --keep-in-foreground --bind-interfaces --except-interface=lo --clear-on-reload --strict-order --listen-address=10.42.0.1 --dhcp-range=10.42.0.10,10.42.0.254,60m --dhcp-option=option:router,10.42.0.1 --dhcp-lease-max=50 --pid-file=/var/run/nm-dnsmasq-xxxx.pid`

At first I tried disable the dnsmasq’s dns function, but finally I found these arguements in the source code of NetworkManager.

/* dnsmasq may read from it's default config file location, which if that
 * location is a valid config file, it will combine with the options here
 * and cause undesirable side-effects.  Like sending bogus IP addresses
 * as the gateway or whatever.  So tell dnsmasq not to use any config file
 * at all.
 */
nm_cmd_line_add_string (cmd, "--conf-file");
nm_cmd_line_add_string (cmd, "--no-hosts");
nm_cmd_line_add_string (cmd, "--keep-in-foreground");
nm_cmd_line_add_string (cmd, "--bind-interfaces");
nm_cmd_line_add_string (cmd, "--except-interface=lo");
nm_cmd_line_add_string (cmd, "--clear-on-reload");

You can recompile NetworkManager to solve the problem. But as the comment implies, you can add some arguements in dnsmasq’s default config file and hope it works.

Update: It doesn't work!!


I chose an another solution. If the arguements above couldn’t be changed, I could change the config files of DNS caching.
In /etc/NetworkManager/dnsmasq.d/, you should have:

listen-address=127.0.0.1 # no other listen addresses
no-dhcp-interface=lo
no-dhcp-interface=......
no-dhcp-interface=......

For IPv6:

listen-address=::1

Result:

pgrep -a dnsmasq
2393 /usr/bin/dnsmasq --no-resolv --keep-in-foreground --no-hosts --bind-interfaces --pid-file=/var/run/NetworkManager/dnsmasq.pid --listen-address=127.0.0.1 --conf-file=/var/run/NetworkManager/dnsmasq.conf --cache-size=400 --proxy-dnssec --conf-dir=/etc/NetworkManager/dnsmasq.d
2512 /usr/bin/dnsmasq --conf-file --no-hosts --keep-in-foreground --bind-interfaces --except-interface=lo --clear-on-reload --strict-order --listen-address=10.42.0.1 --dhcp-range=10.42.0.10,10.42.0.254,60m --dhcp-option=option:router,10.42.0.1 --dhcp-lease-max=50 --pid-file=/var/run/nm-dnsmasq-xxxx.pid

It seems there is no conflict in NetworkManager ... The wrong configure causes this case.

1 comment:

  1. The *.conf in the configured config directory "--conf-dir=/etc/NetworkManager/dnsmasq.d" is used, and overrides what is shown as the command flags in the processlist. The dnsmasq application uses the command line flags as defaults, and the config file overrides those values. I tested to confirm this by setting the cache-size in /etc/NetworkManager/dnsmasq.d/overrides.conf, ran "systemctl restart NetworkManager.service", and ran "pkill -USR1 -x dnsmasq", then "journalctl -u NetworkManager.service" to show the logs, which reflected the new cache-size value I set in the configuration. I agree, it is a little deceiving for the old defaults to still show in the process list.

    ReplyDelete