3

I am closely working on /etc/hosts file in Linux. I have designed some operations for /etc/hosts file like add, modify and delete.

I have read little about /etc/hosts and got to know that it contains <IP> <FQDNS separated by space>.

My question is: can lines in /etc/hosts have only IP address entry like below:

:1            localhost
192.168.25.21 host1.support.com
192.168.25.22 host2.support.com
192.168.25.23
192.168.25.24
192.168.25.25 host5.support.com
2
  • 1
    What would you expect or hope a line with just an IP address to do? – marcelm Oct 12 '20 at 15:30
  • echoing @marcelm -- I'm curious why you want this, too. – user339730 Oct 16 '20 at 5:39
4

It's not really possible to give a yes or no answer to this.

The hosts file is not read by linux (the kernel) at all. Instead each program that uses DNS resolution reads this file for itself.

I've previously bumped into problems with software behaving differently around hostname resolution which turned out to be uClibc doing different things to glibc. These have their own code to read such files and they don’t behave exactly the same.

Even if you can be sure you know what the major libraries do, you'll often find exceptions. For example there are numerous references around the internet suggesting google chrome does not read or necessarily honor the hosts file. Where there is one exception, there are usually more.

My advice to you would be to avoid IP only entries unless you know exactly how every program on your system will behave.

2

An entry in /etc/hosts that consists of only an IPv4 or IPv6 address is technically invalid (and meaningless), but how the different resolver routines handle that situation is implementation-specific.

You should also be aware that /etc/hosts may also include comment lines with any text after a # being regarded as a comment, and aliases (nicknames). One or more spaces and/or tabs are used to separate the individual fields on a line - not just a single space.

For example:

# comment
192.168.1.100  foobar.mydomain.org  foo bar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.