1

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"
2
  • 3
    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 🤔
    – matigo
    Sep 28, 2021 at 12:28
  • @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.
    – Thomas R
    Sep 28, 2021 at 12:42

1 Answer 1

2

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.

2
  • 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
    Sep 28, 2021 at 13:07
  • Btw: sometimes a restart of the browser is necessary as well to ensure that the changes are taken into account.
    – Thomas R
    Sep 30, 2021 at 9:02

You must log in to answer this question.

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