How to find the hostname of an IP using /etc/hosts if it has the mapping on Linux?

Common DNS queries like ping will first get the IP of a hostname if it exists in /etc/hosts.

How to find the hostname of an IP using /etc/hosts if it has the mapping on Linux?

Multiple tools on Linux can do the “reverse” checking using /etc/hosts file. For example, if we have a line 192.0.2.1 test in the /etc/hosts, we can query the host name from the IP as follows.

Use getent

getent hosts: When one or more arguments are provided, it will pass each key to gethostbyaddr() or gethostbyname2(), depending on whether a call to inet_pton() indicates that the argument is an IPv6 or IPv4 address or not, and display the result.

$ getent hosts test
192.0.2.1       test

Use host

If host is given a dotted-decimal IPv4 address or a colon-delimited IPv6 address, host will by default perform a reverse lookup for that address.

$ host 192.0.2.1
1.2.0.192.in-addr.arpa domain name pointer test.

Similar Posts

  • |

    Synchronizing home directories

    Any good tools to synchronize home directories on Linux boxes? Many have several PC/laptops and consequently many home directories. There is home directory synchronizing problem. unison is a good tool to do this: http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html#rshmeth http://www.watzmann.net/blog/2009/02/my-homedirs-in-unison.html http://www.cis.upenn.edu/~bcpierce/papers/index.shtml#File%20Synchronization Useful script: $ unison -batch -ui text ./ /mnt/homebak/zma/ In text user interface, synchronizing two directories in batch mode…

  • Setting up a VPN over SSH

    SSH tunnel and port forwarding is great and convenient to use. But is it possible to set up a VPN like connection over SSH? If you are on Linux or Mac, you can use sshuttle: https://github.com/apenwarr/sshuttle If you are on Windows, you can use ProxyCap: http://www.proxycap.com/index.html Both are great software. Read more: Setting Up VPN-like…

  • How to edit eps file?

    I have made some eps figures for my paper but now I wanna modify them. How to edit them now? You may try inkscape on Linux: https://inkscape.org/en/ It can edit eps files. Read more: How to make eps files using gnuplot How to Convert JPG Images to EPS An Online JPG to EPS Converter How…

  • How to mark a line as deleted in BBCode?

    How to mark a line as deleted in BBCode? e.g. a line crossing through the text. You can use the [s] tag. The BBCode: [s]strikethrough text[/s] will be converted <s>strikethrough text</s>, <del>strikethrough text</del> or <span style=”text-decoration: line-through;”>strikethrough text</span> Check more at: http://en.wikipedia.org/wiki/BBCode Read more: BBCode Support in WordPress How can I recover deleted photos from…