10

what is the easiest way to get a host's canonical host name from linux's command line?

If it matters, my shell is bash.

CLARIFICATION: I want another host's canonical hostname, either by ip or by non canonical hostname. Not the local host's canonical hostname.

flybywire
  • 597
  • 4
  • 9
  • 20

8 Answers8

11

Use dig(1). For normal lookups use:

dig full.hostname.xxx

and for reverse lookups use:

dig -x 127.0.0.1

Also check the man page for much more cool options.

fim
  • 497
  • 2
  • 5
6

A remote host's node name bears no necessary relationship to any of its network names. You'll have to log in and use the hostname command.

If you're looking for the Fully Qualified Domain Name (FQDN) of a network address, you can use DNS query tools like dig or nslookup, as described by @firm and @Richard Holloway.

mpez0
  • 1,512
  • 9
  • 9
5

To be clear, hostname will just return the short name. Use the -f parameter; hostname -f to get the fully qualified name.

Kvisle
  • 4,193
  • 24
  • 25
RRP
  • 51
  • 1
  • 2
3

You could use also the host command. Like in this example

host xxx.xxx.xxx.xyz

The result will be something like

xyz.xxx.xxx.xxx.in-addr.arpa domain name pointer name.domain.com
w00binda
  • 91
  • 1
  • 4
2

To find the fqdn of a remote host with IP xxx.xxx.xxx.xxx you can use

nslookup xxx.xxx.xxx.xxx

and see the answer contains

name = host.domain.net
Richard Holloway
  • 7,456
  • 2
  • 25
  • 30
2

There is no "reverse lookup" for canonical names. That is: given an A record, there is no way to get a listing of what CNAME records point to it.

0

Here's the command:

hostname

tylerl
  • 15,055
  • 7
  • 51
  • 72
  • I want another host's canonical hostname, either by ip or by non canonical hostname. I should have clarified that. – flybywire Mar 16 '10 at 08:39
0

You can look up the IP address by name, and then look up the name for that IP address. To do that in one command:

$ dig -x `dig +short www.google.com` | grep PTR.
 100.212.58.216.in-addr.arpa. 86309 IN  PTR lhr35s06-in-f4.1e100.net.
 100.212.58.216.in-addr.arpa. 86309 IN  PTR lhr35s06-in-f100.1e100.net.

This doesn't work for all sites.

bdsl
  • 101