-1

It is very common to point domains or subdomains to some IPs.

However, I would like to do the opposite:

I would like to point the IP 42.42.42.42 to a subdomain, E.G to test.example.com so when I visit 42.42.42.42 it shows me the content of test.example.com.

I tried editing /etc/hosts but it doesn't works, it seems that you can't point an IP to a subdomain.

Any idea ? All my google searches lead me to tutorials explaining how to redirect a domain to an IP... But I just need the reverse

update: I don't own the IP and I don't own the domain name

raph77777
  • 1
  • 1
  • The actual reverse would be Reverse DNS, which uses DNS to assign a domain name associated to an IP address, but this has nothing to do with redirection, you are likely looking to perform HTTP redirects. – Torin Mar 13 '18 at 11:16
  • I edited the question, I spelled it wrong and didn't mean to say "redirect" – raph77777 Mar 13 '18 at 12:50
  • Provide the true names and IP involved without useless obfuscation, especially since you do not follow RFC2606 for the IPs to use. – Patrick Mevzek Mar 15 '18 at 03:55

2 Answers2

1

You don't need DNS for that.

Just add the IP as an alias in the test.example.com server block in the configuration of your httpd.

Example for Apache:

<VirtualHost *:80>
    DocumentRoot "/www/example1"
    ServerName test.example.com
    ServerAlias 42.42.42.42

    # Other directives here
</VirtualHost>
Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
  • Thanks but I don't control example.com, is there another way ? – raph77777 Mar 13 '18 at 12:51
  • This does not really do what the original poster is asking. This alone won't make the real test.example.com's content seem to show up in IP address 42.42.42.42. You would need to use a HTTP redirect or (reverse) proxy functionality for that. And as the original poster does not control 42.42.42.42 nor test.example.com, this option is not applicable. – telcoM Mar 13 '18 at 12:56
  • If the OP controls neither the IP nor the domain we can close the question as off topic. – Gerald Schneider Mar 13 '18 at 12:58
0

Pointing a [sub]domain to an IP address is easy, because resolving a hostname to an IP address is part of the normal procedure for making a HTTP connection.

Pointing an IP address to another domain name would really require introducing another round of DNS resolution into the process. That could be done at the HTTP protocol level: that's what HTTP redirect is... but that would require controlling the destination system at the 42.42.42.42 IP address. Any lower protocol layers don't really have the capability to introduce another round of DNS resolution. But if you look up the IP address of test.example.com in advance, substituting its IP address in place of 42.42.42.42 is possible.

If you want to redirect an IP address, you'll pretty much need to control either the client system, its network, or the system holding the IP address you wish to redirect. Since you said you don't control the target IP address, you'll need to do it client-side.

If you control the client system, you could use an iptables DNAT rule(s) in the OUTPUT rule chain to redirect traffic from 42.42.42.42 to the target FQDN. If you do that, the name resolution for the target FQDN only happens once, when you configure the rule: so the redirection is really substituting one IP address for another. The rule on the client would be like this:

iptables -t nat -A OUTPUT -p tcp -d 42.42.42.42 --dport 80 -j DNAT --to-destination test.example.com:80

If you control the client's network, then you also have the option of doing the redirection in a regular WWW proxy (if the client is configured to use a proxy you can control), or a transparent proxy (if there is no explicit proxy configuration in the client).

telcoM
  • 4,448
  • 15
  • 25