7

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 ?

Eugene Yarmash
  • 2,383
  • 5
  • 32
  • 54

2 Answers2

13

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);
}
jscott
  • 24,204
  • 8
  • 77
  • 99
  • Here are a couple of links to [back](https://salsa.debian.org/installer-team/netcfg/blob/1.158/netcfg-common.c#L1157) it [up](https://salsa.debian.org/installer-team/netcfg/blob/1.158/netcfg.h#L40). Do you feel like looking into where `debian-installer` invokes `netcfg` by any chance? :) – x-yuri Feb 12 '19 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.

Hennes
  • 4,772
  • 1
  • 18
  • 29