I've somehow deleted /etc/hosts on my testing system, which is Debian Sid. Now I want to install the default /etc/hosts. I tried dpkg -S /etc/hosts to find out which package contains /etc/hosts, but none was found. Where can I download it from ?

  • Just do a quick local Sid install in Virtualbox and find out for yourself? – EEAA Jul 15 '12 at 23:01

The /etc/hosts file is written via debian-installer, it does not exist as a packaged file.

The following is my /etc/hosts from a default install:       localhost       hostname.fqdn.example.com    hostname

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

For more details of the syntax see Debian Reference section The hostname resolution.


Since I feel this answer has garnered more upvotes than I had expected, I did a little finger-work for you, in return. :)

The actual package used by debian-installer, which contains the /etc/hosts logic, is named net-cfg. More specifically, two files, netcfg.h and netcfg-common.c handle the logic of building the /etc/hosts file.

netcfg.h has #defines for both the file itself, and the IPv6 entries:

#define HOSTS_FILE      "/etc/hosts"
#define IPV6_HOSTS \
"# The following lines are desirable for IPv6 capable hosts\n" \
"::1     ip6-localhost ip6-loopback\n" \
"fe00::0 ip6-localnet\n" \
"ff00::0 ip6-mcastprefix\n" \
"ff02::1 ip6-allnodes\n" \
"ff02::2 ip6-allrouters\n"

netcfg-common.c contains the dirty work, populating the info in /etc/hosts:

if ((fp = file_open(HOSTS_FILE, "w"))) {
    char ptr1[INET_ADDRSTRLEN];

    fprintf(fp, "\tlocalhost");

    if (ipaddress.s_addr) {
        inet_ntop (AF_INET, &ipaddress, ptr1, sizeof(ptr1));
        if (domain_nodot && !empty_str(domain_nodot))
            fprintf(fp, "\n%s\t%s.%s\t%s\n", ptr1, hostname, domain_nodot, hostname);
            fprintf(fp, "\n%s\t%s\n", ptr1, hostname);
    } else {
#if defined(__linux__) || defined(__GNU__)
        if (domain_nodot && !empty_str(domain_nodot))
            fprintf(fp, "\n127.0.1.1\t%s.%s\t%s\n", hostname, domain_nodot, hostname);
            fprintf(fp, "\n127.0.1.1\t%s\n", hostname);
        fprintf(fp, "\t%s\n", hostname);

    fprintf(fp, "\n" IPV6_HOSTS);

  • Here are a couple of links to back it up. Do you feel like looking into where debian-installer invokes netcfg by any chance? :) – x-yuri Feb 12 '19 at 16:50

Unchecked on Debian, but it should be

::1                     localhost localhost.my.domain               localhost localhost.my.domain

(if you do not use IPv6 then you can ignore the line starting with ::1)

Edit: The file is probably the base installation, not from an additional package.

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.