-1

I have a Linux box and I know I can get the list of IP addresses assigned to that box using following command /sbin/ifconfig -a but how to I get the list of hostnames / DNS for that Linux box using single command?

Why I asking this is because using /sbin/ifconfig -a I see that there are ~6 ethernets connected to it and in those 4 are active. Now, all those have different IP addresses, so I have 4 different IP addresses for my Linux box (please correct me if I am wrong here ??). Now, my Weblogic application server has defined listen address as, lets say abc.fb.us and xyz.fb.us etc. Now when I ping each hostname (should I call abc.fb.us as hostname or DNS or alias ??) then I get some IP address which is listed when I do ifconfig.

I know I can do nslookup 172.22.33.11 and I will get abc.fb.us, assuming that's the IP address of abc.fb.us.

But I want to get a list of hostnames using single command, much like the way I got list of IP address.

I have checked my /etc/hosts and it doesn't have abc.fb.us, xyz.fb.us etc. so clearly there is some other way these names are configured and how do I get all the hostnames?

3 Answers 3

0

This one liner will work:

ifconfig | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}' | xargs -L1 host

7
  • This only works if the PTR records are set up correctly surely and won't catch all forward references.
    – user9517
    Jun 23, 2016 at 22:13
  • Really good one, but before I could accept this as answer, few things - could you please confirm/answer the points I have highlighted in bold? Jun 23, 2016 at 22:17
  • @Iain Do you have better solution or answer? I don't know who downvoted his answer and why, I upvoted because it was a useful answer. Jun 23, 2016 at 22:21
  • I'd like to add some clarity to the answer I gave - yes, Iain is correct that this answer relies on PTR records to exist for the given IP's. In the event that they do not exist, there's nothing (beyond Iain's suggestion, which is very, very intensive) that can reverse IP's into hostnames. That's a limitation of DNS. In simple use cases (and I'd argue, many use cases), the above command will work well to reverse the DNS entries. As long as you know the limitations of DNS, host is a respected method of determining an hostname for an IP and vice versa. It's as close as they will get. Jun 24, 2016 at 1:10
  • To answer your bolded points - 1) It's definitely possible to have 4 IP's on your Linux machine. 2) Assuming the PTR record is assigned correctly, then yes, when you ping abc.com it will send an ICMP (ping) request to the given IP. Jun 24, 2016 at 1:12
2

You can't get this information at all, at least in a reliable and complete way. The system doesn't need to be aware of what DNS names for what domain are pointing to it.

If PTR records are defined, you can get these like Brennen described, but everything else is unreliable.

As an example, nothing prevents me from setting up DNS entries for any IP address and try to connect to the address using that DNS entry. For most protocols, you will be completely unaware that this has happened (HTTP is a major exception, as it will transfer the hostname in the HOST header).

2
  • Thank you for your inputs. FYI - this is not a public website which is on public DNS servers, this is a private network .. Now w.r.t. private networks - when ping "abc.fb.us" and I get some IP address, what could I call "abc.fb.us" - hostname or DNS or alias ?? Can you guide me how Linux team of our server has associated "abc.fb.us" with a IP address? I thought they would have added an entry in /etc/hosts files but that's not the case .. Jun 23, 2016 at 22:31
  • FYI - I had got results using @Brennen answer, so does it mean that my box has PTR records or should I say my network has PTR records for my box? Jun 23, 2016 at 22:31
2

You can't do what you want to do. There is no single command that will give you the information you want.

Additionally, there can be multiple forward (A) records for a a single IP address as well as CNAME records all of which are hostnames for the system.

You could try dumping your DNS config and searching for all records that have the same IP addrress, then search to see if any CNAME records refer to the A record names. This isn't simple and certainly isn't a single command.

5
  • Thank you for your inputs, I am novice in this area so may be you have to elaborate on "dumping your DNS config", how can I do that? FYI - this is not a public website which is on public DNS servers, this is a private network .. Jun 23, 2016 at 22:24
  • W.r.t. private networks - when ping "abc.fb.us" and I get some IP address, what could I call "abc.fb.us" - hostname or DNS or alias ?? Can you guide me how Linux team of our server has associated "abc.fb.us" with a IP address? I thought they would have added an entry in /etc/hosts files but that's not the case .. Jun 23, 2016 at 22:28
  • FYI - I had got results using @Brennen answer, so does it mean that my box has PTR records or should I say my network has PTR records for my box? Jun 23, 2016 at 22:30
  • @hagrawal learn about the DNS be educated.
    – user9517
    Jun 23, 2016 at 22:57
  • elaborate on "dumping your DNS config", how can I do that? ... That is implementation dependent and you don't say how you implement DNS but that isn't helpful in answering your question which is fully answered by first sentence.
    – user9517
    Jun 23, 2016 at 22:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .