1

I'm looking for a way to cache the hosts file, the resolv.conf file and new DNS entries and the closest thing that doesn't quite work is nscd. Nscd will happily cache DNS requests that are present in the hosts file but when a server is requested that is not in the hosts file, the hosts file is read.

I'm trying to save power by not having the hard drive run all the time but everytime I visit a new page on the same site, the hard drive spins up just to read the hosts file to check domains.

Is there any way I can make it where the hosts and resolv.conf file are stored in memory then when a new domain is requested, the memory holding hosts.conf is then scanned instead of requiring disk access just to search the hosts file?

2
  • How have you found that it's to access these specific files under /etc that the disk spins up? Would the disk not spin up to e.g. store files used for the browser cache, or to perform ordinary logging operations?
    – Kusalananda
    Dec 17, 2022 at 9:30
  • If you are worried about power usage, just use a ssd. Your system is going to be reading much more than just the hosts file. There are many libs which will need to be paged in as you run programs and most of them will be in /lib & /usr/lib etc.
    – Bib
    Dec 17, 2022 at 15:52

2 Answers 2

0

You could create a tmpfs that is kept in memory. I've never used it for your specific case but I really don't see why it wouldn't work.

https://www.howtoforge.com/storing-files-directories-in-memory-with-tmpfs

Lets say we mount the ramdisk to /mnt/ramdisk (Within it we have a file called ramdisk_hosts_file with the content of your original hosts file.)

mount -o ro,bind /mnt/ramdisk/ramdisk_hosts_file /etc/hosts

Have not tested it but this should mount the file from your RAM FS directly on the hosts file.

Of course any edits will be flushed upon reboot so you might want to setup something to copy the content on boot and on shutdown.

In theory something like this should work. Obviously you would want to test this is a lab setup, might break some stuff :)

Out of interest I'll try to create something like it when I have more time.

0

but when a server is requested that is not in the hosts file, the hosts file is read

Could it be that the hosts file takes precedence over DNS? Have a look at /etc/nsswitch.conf, which might look similar to this:

hosts: local, bind

This would mean, that if a hostname comes up which the system doesn't know about, the file /etc/hosts is consulted first and only failing to find it there a DNS-query is issued. This would explain it.

You can reorder the process by replacing the line with:

hosts: bind, local

Which would mean: if an unknown hostname is to be resolved, ask DNS and only ifit comes up blank consult /etc/hosts.

You must log in to answer this question.

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