Skip to main content
2 of 3
added 69 characters in body; added 98 characters in body; added 50 characters in body
bishop
  • 3.2k
  • 2
  • 19
  • 31

Size limits only apply when allocating static buffers. gethostbyname(3), which parses the entries in /etc/hosts, does not allocate static buffers - and never has. The original 1983 release of BSD 4.3 algorithm shows an open-file, while parse line, close-file pattern:

sethostent(0);
while (p = gethostent()) {
    if (strcmp(p->h_name, name) == 0)
        break;
    for (cp = p->h_aliases; *cp != 0; cp++)
        if (strcmp(*cp, name) == 0)
            goto found;
}
found:
endhostent();

Modern implementations retain this heritage in all essentials.

Anyway, internally, the *hostent family of functions store a file pointer to the current line in the file. sethostent opens the file and sets the file pointer position. gethostent gets data and advances the pointer. endhostent closes the file pointer. The GNU C Library offers a thorough reference on these functions.

As you might guess from the implementation, entries occuring earlier in the file resolve faster. If your hosts file is huge, this comes into play.

So, no matter how big the file is, the OS will consume it. Eventually, though, you'll hit filesystem limits (per Jeff Shaller's answer). You also have maximum line size limits (per Kusalananda's answer). But, in the end, you can make it as big as you want. But please, don't.

bishop
  • 3.2k
  • 2
  • 19
  • 31