2

I have tried dnsmasq, squid and this dns proxy, however, none of them does what I want. I want to do something like this:

127.0.0.1 *facebook.com
127.0.0.1 *imgur.com

Where every URL matched is redirected to 127.0.0.1, or just straight up blocked. (whether on the command line, on apps, on browsers, simply an emulation of some of the functions of /etc/hosts but with wildcards).

Although if nothing works, then via the browser is the most important.

5
  • 1
    AFAIK, no version of Unix supports wildcards in /etc/hosts. Dec 13, 2015 at 19:16
  • @G-Man I know, that's why I want some Application to do this. Dec 13, 2015 at 20:22
  • 1
    Well, then, maybe you should have mentioned /etc/hosts fewer than three times in your question.  It looks like your question is "What name resolution service will let me do wildcard mapping of host/domain names to IP addresses?"  And that would be off-topic as a product request. Dec 13, 2015 at 21:09
  • @Scott The title was misleading ok, but the last says what I want. Dec 13, 2015 at 21:23
  • 1
    @DisplayName, can you please clarify if you're only trying to use this from the command line or from with apps/browsers? What's the use case and maybe we can direct you towards a better solution. Please update your post with this info
    – SaxDaddy
    Dec 15, 2015 at 23:49

3 Answers 3

2

Like Jeremy W. Sherman says, you should consider setting up a localised DNS server, as the etc/hosts file cannot support wildcards.

Consider installing a program called DNSMasq, which lets you create a kind of wildcard:

address=/facebook.com/127.0.0.1
address=/imgur.com/127.0.0.1

Which would reroute all domains with imgur.com and facebook.com in them to 127.0.0.1.

Read this blog post titled /etc/hosts Bugs, and DNS Resolution or this updated blog post: How to get .dev addresses on Mac OS X 10.10 Yosemite in 5 simple steps for more information on how to install this and why this happens.

2
  • If you read my question you would have seen that I tried to install dnsmasq. I tried 8 different guides but none of them worked. Dec 23, 2015 at 23:58
  • @DisplayName see my update
    – Quill
    Dec 24, 2015 at 1:26
0

Browser:

  • Install something like RequestPolicy. (RequestPolicy is Firefox-only as far as I know. I believe uBlock or uMatrix might be able to do something similar for Chrome.)
  • Open Manage Policies.
  • Add a Block policy:
    • Leave the origin fields empty. (This implicitly matches all origins.)
    • In the destination fields, set the host to a wildcard like *.facebook.com.
    • Hit the "Add Rule" button.

Screenshot: Configuring RequestPolicy Continued to block requests to Facebook

DNS:

  • You'll want to run a full-on DNS server like named or unbound and configure System Preferences to use it as your system's DNS server.
  • You should be able to configure that to pretend to be authoritative for the entire facebook.com zone and then set up a wildcard within the zone configuration.
  • For stuff you don't want to block, you'll configure it to act as a local cache of some upstream DNS server.
  • This configuration is hinted at in the unbound documentation for its local-data configuration stanza:

    If you need more complicated authoritative data, with referrals, wildcards, CNAME/DNAME support, or DNSSEC authoritative service, setup a stub-zone for it as detailed in the stub zone section below. (emphasis added)

Running your own DNS server can get involved. Here is a walkthrough for setting up named for similar purposes.

0

Run sudo nano -e /etc/hosts to open the hosts file and put inside

127.0.0.1 *.domain-name.com #Block subdomains
127.0.0.1 domain-name.com #Block domain

You should put both to block the web site.

You must log in to answer this question.

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