I wrote a simple program where you enter ip, and it should issue a host.

import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Scanner; public class Index { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.print("Введите интерисующий ip:"); String ip = in.nextLine(); InetAddress addr = null; try { addr = InetAddress.getByName(ip); } catch (UnknownHostException e) { e.printStackTrace(); } System.out.println(addr.getCanonicalHostName()); } } 

If I enter the server ip on which I run this program, I get the host name. If I try to find out the host of another server on the same network, I just get ip.


Therefore, question 2:

  • From .getCanonicalHostName () learns the host name.
  • Where do I need to register the host name on the linux server so that the code works as it should, maybe you need to specify it somewhere on the DNS server? (In what places is it possible to do this?)

    1 answer 1

    PTR records in DNS are responsible for converting ip to names. All such records are declared in "reverse zones", which are subdomains of the in-addr.arpa domain. If ip addresses are internal, you need to raise the "reverse zone" in your DNS yourself. for example, for the subnet 192.168.0.0/24, the zone should be called 0.168.192.in-addr.arpa . If the addresses are real, then the reverse zones available on the Internet can be lifted by the owners of at least class C subnets (256 ip addresses).

    PTR records in DNS look like ip turned upside down before:

     0.168.192.in-addr.arpa. IN SOA ns.sample.com. dns-admin.sample.com. ( 1999040701 ;Serial number 10800 ;Refresh 3600 ;Retry 604800 ;Expire 86400) 0.168.192.in-addr.arpa. IN NS ns.sample.com. 1.0.168.192 IN PTR hostname.sample.com. 

    In addition, you can try to register the usual "direct" entries in / etc / hosts

    • Thanks for the answer. One more remark on the issue. There are 2 cars. Sevrer and proxy. ip proxy is registered in dns, therefore I can ping it calmly from the server, both via ip and through the host name, BUT! The .getCanonicalHostName () method for some reason cannot return the host name if I run this application on the server. - Artem Mezhelovsky
    • @ ArtemMezhelovsky ip proxy is registered in the DNS in what form? If it is only in the form of a direct record A , then it can only determine the IP by the host name, but not vice versa. If the name is determined, perhaps it is determined by the hosts file on the machine itself. And on the server it seems that there is no record in the hosts and the only way is to raise the reverse zone and PTR record in it. And if there are correct records in the DNS, then the machine that receives the addresses should of course use exactly that DNS server - Mike
    • @ mike are both A record and PTR record, but still getCanonicalHostName () doesn't want to give the host name - Artem Mezhelovsky
    • @ ArtemMezhelovsky On that machine where it doesn’t work, try dig PTR 2.64.0.10.in-addr.arpa (ip just the required one), if dig is not there, then you can run nslookup, give the command set q=PTR and drive the same in-addr address. they must return the DNS name. - Mike
    • Understood. What was the problem: 1. There was no PTR record. 2. It was necessary to clean the cache on the internal dns server - Artem Mezhelovsky