17

What's the best way to confirm that your /etc/hosts file is mapping a hostname to the correct IP address?

Using a tool like dig queries an external DNS directly, bypassing the hosts file.

2
  • hosts files do not map domains....them map specific hostnames to IP addresses.
    – mdpc
    Commented Jun 2, 2014 at 18:10
  • @mdpc I edited it to reflect that. Basically the same question though, I was just looking for a way to verify the hosts file entry is working.
    – Jay
    Commented Jun 2, 2014 at 18:17

3 Answers 3

21

I tried this out and it seems to work as expected:

echo "1.2.3.4 facebook.com" >> /etc/hosts

Then I ran:

$ getent ahosts facebook.com
1.2.3.4         STREAM facebook.com
1.2.3.4         DGRAM  
1.2.3.4         RA
1
  • 1
    looks good, thanks! I also found that ping works: ping facebook.com outputs: PING facebook.com (1.2.3.4) 56(84) bytes of data.
    – Jay
    Commented Jun 2, 2014 at 20:19
5

I found gethostip which accepts either hostname or IP and also has options suitable to print out exactly what you need.

$ gethostip -d facebook
1.2.3.4
1
  • On Debian gethostip is not installed by default. Use sudo apt install syslinux-utils. Commented May 13, 2021 at 9:25
3

You can use the 'ping' command to check the values added in /etc/hosts are working fine or not.

echo "1.1.1.1   abc.com" >> /etc/hosts
ping abc.com

You must log in to answer this question.

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