how to activate a new /etc/hosts instantly

Solution 1:

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.