Score:0

how to activate a new /etc/hosts instantly

au flag

I'm using a terminal command to replace the /etc/hosts - file.

alias nonews="sudo -S chattr -i /etc/hosts < /etc/password_file && sudo -S cp /etc/hosts_all_blocked /etc/hosts < /etc/password_file && sudo -S chattr +i /etc/hosts < /etc/password_file"

Where the hosts_all_blocked is a file with which distracting webpages are blocked. And the /etc/password_file contains the sudo-password.

So after using I want the relevant pages to be blocked immediately. I tried the command and sometimes it works immediately as wished, but sometimes even if the file has changed, there is no effect on the possibility to open webpages.

How can I ensure that the changes in the /etc/hosts are having an immediate effect?

Btw: For the unblocking I'm using a 20-minute delay:

alias getnews="sleep 1200 && sudo -S chattr -i /etc/hosts < /etc/password_file && sudo -S cp /etc/hosts_all_allowed /etc/hosts < /etc/password_file && sudo -S chattr +i /etc/hosts < /etc/password_file"
in flag
If the browser is already open on sites, there’s a very high probability it’s using its own internal DNS cache. You would have to flush the cache of the browser(s) as well
Thomas R avatar
au flag
@matigo I just checked your hint with an additionally added webpage. No effect after changing the file. Then I had an instant effect after deleting the cache. Thanks a lot. If you are placing your comment as an answer I will select it as the correct answer.
Score:1
in flag

Browsers will often keep a local copy of DNS lookups in their cache to save a little bit of time when loading sites. This means that you'll need to clear the cache of the browser in order for changes to the /etc/hosts file to be seen for already-loaded sites (including those that may have been loaded in previously closed tabs).

With this in mind, you may want to expand your alias to a shell script that also removes the browser cache (but leaves cookies intact ... unless you want to start with a "fresh" browser that knows nothing about your past usage each and every time).

If you use Firefox as your main browser, you can clear the cache via a shell script with this command:

rm -r ~/.cache/mozilla/firefox/*.default*/*

If you use Google Chrome as your main browser, you can clear the cache like this:

rm ~/.cache/google-chrome

For other browsers, you will need to dig through the ~/.cache directory to find where they keep their temporary resources.

Note that these commands should be run as the user, not with sudo.

Thomas R avatar
au flag
thanks for your answer and as well for the additional information regarding how to delete the cache with a terminal command. I did not yet test the terminal part. But since the question is already answered without this part I just accepted your answer. You are awesome!
Thomas R avatar
au flag
Btw: sometimes a restart of the browser is necessary as well to ensure that the changes are taken into account.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.