clami219
clami219

Reputation: 3038

How to configure DNSmasq to resolve an address always giving the current public ip on a specific network

I have an http server (Apache) configured and running on a machine in my LAN. I want a website running on it to be accessible from all the computers connected to the LAN through a specific URL (e.g. www.mywebsite.mylan).

I'm using DNSmasq as dns server (but I'm totally new to it). The server machine runs Os X 10.9.

I saw that in order to have it resolve a specific address, a line like the following is needed in DNSmasq configuration:

address=/www.mywebsite.mylan/127.0.0.1

I want to know if it is possible to have something as the following:

address=/www.mywebsite.mylan/self_en0

to resolve the url using the local machine's public ip on a specific network (en0), since the dns server and the http server are running on the same machine and the ip address is dynamically assigned by the DHCP server of the router.

Is there a viable way to get it or the only way to have it work is to use a static ip for the server machine?

Upvotes: 2

Views: 5460

Answers (1)

Deleted User
Deleted User

Reputation: 2541

  • nameservers don't resolve "URLs". They resolve hostnames. a URL is a webserver concept, which may include a hostname. Some parts of a URL a nameserver can't deal with (URIs, directory/file specifications, port numbers, the characters to seperate those URL components)
  • an A record (which is what you're simulating with the dnsmasq address= config directive) must have an ip address as value, not an interface name.
  • whether nameserver and webserver are running on same machine, or on different machines, is irrelevant.
  • using the public address of the webserver may or may not work. it depends on several factors: routing, firewall, server configuration (interface binding)

Now for your solution: If, as you say, the address of your webserver is assigned dynamically, maybe even by the same dnsmasq you're using as nameserver, dnsmasq can create a resolvable name when the DHCP lease was requested, and associates the assigned ip address with it. What name is added is under your control: the DHCP client on webserver can tell DHCP server (dnsmasq) what name it likes to have associated with the assigned ip address. You can alternatively fix a name in your dnsmasq configuration for that particular client. Now that there's a name, which can be resolved, you have the option, again through dnsmasq configuration, to introduce an alias for it (I think dnsmasq config actually calls this "alias" - it simulates a CNAME record). But it may be preferable to avoid this, by having the resolvable name set to the desired name right away.

If the DHCP server on your LAN is not dnsmasq which you're also using as nameserver, things get a bit more complicated: In the above scenario, you benefit from synergetic effects from DHCP server and nameserver being components of the same program, and therefore exchanging information internally already. If that's not the case, you'd have to setup your DHCP server such that it informs the nameserver, if a lease was requested or released. DHCP servers usually provide hooks for such purposes, allowing you run a script in case of such an event, which in turn performs actions to update the zone your hosts are members of.

But you could simply consider to tell dnsmasq (its DHCP server component) to assign a static ip address to your webserver, and add that address together with hostname and aliases to the hosts file on the machine dnsmasq runs - its nameserver component can include hostnames from the hosts file, and resolve them too.

Upvotes: 2

Related Questions