How to redirect VM DNS question to host machine localhost dnsmasq
0
votes
0
answers
251
views
My local network shcema is something like this:
internet (WAN)
|
router host IP (resolv.conf)
| |
+------- eth0 - br0 - tap0 - VM (resolv.conf)
| |
localnet localhost (dnsmasq)
(.home)
My host computer is used mainly for testing virtual machines.
The goal is to let the VM to see the local net only, not to get DNS from the WAN, but get it from
dnsmasq
, which is configured to listen on host lo
interface and to know just hosts from the localnet: pc1.home
pc2.home
www.home
data.home
printer.home
...
I succefully configured the dnsmasq to listen on tcp/udp 127.0.0.1:53
on the host machine, and check it by getting answer form host cli:
# dig www.google.com ... 142.251.36.68 (ANSWER 1)
and
# dig @127.0.0.1 www.google.com ... (ANSWER 0)
# dig @127.0.0.1 www.home ... 192.168.1.2 (ANSWER 1)
I set the ip_forwarding in various ways
# echo "1" > /proc/sys/net/ip4/conf/all/forwarding
# echo "1" > /proc/sys/net/ipv4/conf/tap0/route_localnet
Then I tried to compose the iptables rule:
# iptables -t nat -A PREROUTING -i tap0 -p tcp --dport 53 -j DNAT --to-destination 127.0.0.1:53
# iptables -t nat -A PREROUTING -i tap0 -p udp --dport 53 -j DNAT --to-destination 127.0.0.1:53
followed by check
# iptables -v -t nat -L
pkts bytes target prot opt in out source destination
0 0 DNAT udp -- tap0 any anywhere anywhere udp dpt:domain to:127.0.0.1:53
0 0 DNAT tcp -- tap0 any anywhere anywhere tcp dpt:domain to:127.0.0.1:53
But while running my virtual linux, the DNS is still answered from router nameserver listed in /etc/resolv.conf
.
Well, you can advice me to rewrite the resolv.conf
of virtual machine manulaly, but as I test the installation of many VM it is annoying to do that each time, and it is hardly to say, what to write there, because the dnsmasq listens at localhost of the host machine. Hence the content of VM resolv.conf
like
nameserver 127.0.0.1
could be very confusing and useless.
I tried to capture some packets with Wireshark, but it just showed me that no redirection takes place, but not why.
Asked by schweik
(1440 rep)
Feb 2, 2023, 10:50 PM