How to properly rebuild `/etc/hosts`? (It's empty!)

Dear all!

I don’t know how this could happen, but my /etc/hosts is currently empty. I do remember some content back then in the past — but now, there isn’t any…

How can I properly rebuild it? Without the use of YaST? (I am still on Leap 15.6, but I already uninstalled YaST to learn living without it…)

TIA!

It vanished already out of your backups?

It’s a textfile, you can add anything with any editor and save it.

I use vi to write in it, but you can also use nano or more.

Here’s my default content from my 15.6 machine:

#
# hosts         This file describes a number of hostname-to-address
#               mappings for the TCP/IP subsystem.  It is mostly
#               used at boot time, when no name servers are running.
#               On small systems, this file can be used instead of a
#               "named" name server.
# Syntax:
#    
# IP-Address  Full-Qualified-Hostname  Short-Hostname
#

127.0.0.1	localhost

# special IPv6 addresses
::1             localhost ipv6-localhost ipv6-loopback

fe00::0         ipv6-localnet

ff00::0         ipv6-mcastprefix
ff02::1         ipv6-allnodes
ff02::2         ipv6-allrouters
ff02::3         ipv6-allhosts

1 Like

What backups resp. where? I don’t have personal backups of that stuff. And I don’t use BTRFS with snapshots.

Well, yes.

I rather mean, what content actually.

Thank you! That’s what I need.

Backup of configuration files in /etc. Is not a bad idea IMO.

In any case, I assume the pristine example of @myswtest is a good starting point.

No problem … an easy Reply.

  1. BTW, how is it you discovered that “hosts” was empty?
  2. Also … so, the file “hosts” did exist, but empty?

Be sure to check ownership too …obviously, should be “root : root”

Yes! That is quite a good idea. Haven’t had it, so far.

Thank you very much, indeed.

I was running https://cisofy.com/lynis/ … it tested on it.

Well, yes. File existent, but empty. I do not guess why.

I just checked it: it’s OK. Thank you.

1 Like

I know, solved – but, for completeness – (the following is from Tumbleweed but, a Leap 16 in a VM shows the same information):

 > rpm --query --whatprovides /etc/hosts
netcfg-11.6-13.5.noarch
 > 
 > rpm --query --whatrequires netcfg
nfs-kernel-server-2.8.4-49.2.x86_64
libcares2-1.34.6-1.1.x86_64
nfs-client-2.8.4-49.2.x86_64
tftp-5.2-30.1.x86_64
iptables-1.8.11-5.1.x86_64
systemd-258.4-1.1.x86_64
 > 
 > rpm --query --whatrecommends netcfg
patterns-base-enhanced_base-20241218-24.1.x86_64
aaa_base-84.87+git20260210.ecce285-1.1.x86_64
 > 
 > rpm --query --whatsuggests netcfg
no package suggests netcfg
 >
2 Likes

/etc/hosts is a pretty generic thing:

knurpht@Lenovo-P16:~> cat /etc/hosts
#
# hosts         This file describes a number of hostname-to-address
#               mappings for the TCP/IP subsystem.  It is mostly
#               used at boot time, when no name servers are running.
#               On small systems, this file can be used instead of a
#               "named" name server.
# Syntax:
#    
# IP-Address  Full-Qualified-Hostname  Short-Hostname
#

127.0.0.1       localhost localhost.localdomain
::1             localhost localhost.localdomain ipv6-localhost ipv6-loopback

# special IPv6 addresses
fe00::0         ipv6-localnet

ff00::0         ipv6-mcastprefix
ff02::1         ipv6-allnodes
ff02::2         ipv6-allrouters
ff02::3         ipv6-allhosts

knurpht@Lenovo-P16:~> 

You should be able to copy/paste this into yours

1 Like

Is this very different from the one offered (and already used as a solution by the OP) in post #4?

1 Like

<Base:System netcfg>
File: /etc/hosts (last changed over 2 years ago) – <File hosts of Package netcfg>

#
# hosts         This file describes a number of hostname-to-address
#               mappings for the TCP/IP subsystem.  It is mostly
#               used at boot time, when no name servers are running.
#               On small systems, this file can be used instead of a
#               "named" name server.
# Syntax:
#    
# IP-Address  Full-Qualified-Hostname  Short-Hostname
#

127.0.0.1	localhost localhost.localdomain
::1		localhost localhost.localdomain ipv6-localhost ipv6-loopback

# special IPv6 addresses
fe00::0         ipv6-localnet

ff00::0         ipv6-mcastprefix
ff02::1         ipv6-allnodes
ff02::2         ipv6-allrouters
ff02::3         ipv6-allhosts
1 Like

@dcurtisfra Thank you very much for this piece of information! Appreciated!

@hcvv How do you handle backups of /etc/? I mean how to cope with users and rather more with correct permissions? TIA for sharing your idea!

Simplest: mdkir $HOME/Backup ; sudo rsync -avz /etc /home/<your_user/BackupEtc/etc-$(date +%F)

Dear @knurpht Thank you for suggesting this one!

I haven’t never user rsnyc so far, I use other ways (like FreeFileSync as GUI application).

I noticed that -a is responsible for the trick. Thank you.

I guess that -zis not quite necessary (on /etc/… just some small text files…).

But just this humble question: How is mdkir $HOME/Backup related to /home/<your_user/BackupEtc/etc-$(date +%F)? I don’t see the relation, sorry.

The same as I handle backups of /boot, /var, /home, /root and /srv (the last one because I run web-site from there).
I use rsync to another system (thus combining with rsyncd).

Those are of course preserved. The backup procedure is run as root and thus preserves everything.

Backup once a week and keep them for nine weeks. Thus can restore from 10 different weekly time stamps.

Thus I can see (and restore) system files until ~10 weeks old and can do the same for the personal files from my users.