Why is a reboot not required after changing HOSTS file content
Solution 1
On each DNS request the hosts file is read. This means it is not saved to memory, so the changes you make are in real time.
Solution 2
In Mac OS X, you do not have to reboot after editing the hosts file. All you have to do is type dscacheutil -flushcache
For Windows, at least newer than Windows 9x, changes are immediate because of the changes Microsoft made. Since there are a lot of different Linux distributions, each one probably restarts the network service differently, but for Red Hat based OS's, service network restart
should do the trick. Technically, you don't have to restart the Linux machine at all, you just have to restart all the services dependent on the network and your hosts file. It is possible that these services should not be restarted without a full system reboot, but rebooting the system is much easier than killing and relaunching 8+ services and daemons :)
Related videos on Youtube
gs5
Updated on September 17, 2022Comments
-
gs5 over 1 year
I was having a discussion with someone who asked me whether a Linux OS has to be rebooted when the hosts file is modified.
From personal experience, on Windows I change the file but don't reboot and I've seen others do the same thing. I assume Linux has no exception(s), but is there any reason why a reboot is not required (to at least justify my actions)?
Thanks
-
Zac Garrett over 13 yearsYou should not need to restart any services for editing the hosts file on any OS. The changes should appear as soon as you save the file.
-
Madison S over 13 years"Should" is the keyword. Important services cache things all the time. By flushing the cache or restarting the services, the services have to ask the system for the most recent information(which would include the host file changes). While you would expect a read from the hosts file, if a process has something in its cache that hasn't expired, it is going to use the cache.
-
gs5 over 13 yearsSo you mean to say that the contents of the file remain on disk but when a request comes into the DNS server, it reads the HOSTS file on my machine?
-
Madison S over 13 yearsYour machine checks the hosts file first. If a mapping is not found for a hostname, then it asks your DNS server.
-
JT.WK over 13 yearsI agree with the above. Check out /etc/nsswitch.conf