8

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 ?

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

2 Answers 2

14

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:

127.0.0.1       localhost
127.0.1.1       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.

Update:

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"
...<snip>...
#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, "127.0.0.1\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);
        else
            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);
        else
            fprintf(fp, "\n127.0.1.1\t%s\n", hostname);
#else
        fprintf(fp, "\t%s\n", hostname);
#endif
    }

    fprintf(fp, "\n" IPV6_HOSTS);

    fclose(fp);
}
1
  • 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, 2019 at 16:50
1

Unchecked on Debian, but it should be

::1                     localhost localhost.my.domain
127.0.0.1               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.

You must log in to answer this question.

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