5

Let's say a website does not have a domain name like www.google.com and the only way to connect to it is to use an IP address like 216.58.212.68. If I add an entry in /etc/hosts that looks likes 0.0.0.0 216.58.212.68, will it block connections to that website? Would a web browser be blocked from visiting it too?

Additionally, would this also apply to local addresses like 192.168.0.1?

14

No. The hosts file doesn't affect any routing. It only affects name lookups. Since 216.58.212.68 is an IP address, the system won't look it up in the hosts table.

Read here for more info on the hosts file: http://manpages.ubuntu.com/manpages/trusty/man5/hosts.5.html

If you want to block connections to an IP address from your system, there are a couple of ways to do that, like:

  1. Blackhole traffic using the route command:

    route add 216.58.212.68 gw 127.0.0.1 lo
    
  2. Reject traffic using the route command:

    route add -host 216.58.212.68 reject
    
  3. Null route using the ip command:

    ip route add blackhole 216.58.212.68/32
    

Now, if you want to block traffic to a system by name, you can add a fake entry to your hosts file pointing that name to the loopback address:

127.0.0.1    badactor.evil.com

Then any traffic trying to get to that host from your system will be faked out – as long as your system is set to use the hosts file lookup prior to DNS. Any specifically DNS based lookups will still work, although you could use a DNSMASQ server like a Pi Hole to block even DNS lookups.

Make sure you read the man pages for the route and ip commands, so you'll understand how to make these commands persistent across reboots if you need them to be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.