Blocking Outbound Connections to Specific Hosts on Mac
Sometimes it’s useful to block outbound connections to a certain host. For example, when testing how your locally-running code reacts when a service it depends on or calls goes down.
To block outgoing connections to a given host, say: api.zention.co.uk
, edit the hosts file with sudo vi /private/etc/hosts
and add a line like this:
0.0.0.0 api.zention.co.uk
Then flush the DNS cache so the hosts file is re-read:
dscacheutil -flushcache
You can then test the code which calls the service as your Mac will no longer be able to connect to that host.
When you have finished, you can re-enable outbound connections to that host by removing the line and re-flushing the DNS cache again.