How to access service which is listening on the private IP range within Vagrant from the host?

by Pritzl   Last Updated July 21, 2019 08:00 AM - source

I have a Vagrant VM which is running Ubuntu 18.04/Bionic. In this VM, there is a DNS server (systemd resolve in this case) running. This can be seen here:

[email protected]:~$ netstat -peanut | grep :53
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
udp        0      0 127.0.0.53:53           0.0.0.0:*                           101        15442      -                   

As you see, the DNS server is bound on 127.0.0.53:53 UDP. My goal is to use this DNS server (for certain hosts, this is quite easy to configure in dnsmasq) on the host.

Here, 127.0.0.53 is within the private IP range (being 127.0.0.0/8), so the following Vagrant portforwarding rule will not work:

config.vm.network "forwarded_port", guest: 53, host: 8125, protocol: "udp"

I know the default approach to fix this is by binding the DNS service to 0.0.0.0, instead of 127.0.0.53, but I am not (yet) able to find in the documentation how to do that. Maybe this is a bad idea as well?

So, for the scope of this question, I would like to assume that this is not possible and start from the pre-condition that the service is listening on the private IP range.

My question then is: which approaches can we apply to make the forwarding happen?

I found one approach, which I'll explain below, but I am wondering if there are more. Maybe some iptables trickery?

socat

With socat, we can create a local tunnel from 8125 to 53 in the guest:

socat -T10 UDP4-LISTEN:8125,fork,reuseaddr UDP4:127.0.0.53:53

Then, in Vagrantfile, we can do:

config.vm.network "forwarded_port", guest: 8125, host: 8125, protocol: "udp"

This works, but introduces a (probably small) performance overhead.



Related Questions





socat udp packet forwarding with source address

Updated June 10, 2015 15:00 PM