You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Can't block websites using Terminal

Hey everyone, having some trouble with the below and wanted to see if anyone has solutions or ideas! Asked around, even in Reddit, without any luck so far:


PROBLEM:

Can’t block websites for productivity via hosts in Terminal on personal computer. Don't want to use Screen Time because it's easy to workaround.


It worked successfully on my work computer, so know it's possible. However, no luck on my personal computer. They’re the same Mac models and I’m using the same WiFi router for both.


MAC DETAILS:

MacBook Pro, 2023 with Ventura 13.4.


SO FAR:

I’ve followed all of the below instruction and, again, it worked on my work computer, but not personal one:


  1. Type in the command below and press Return on your keyboard. This will open the hosts file: sudo nano -e /etc/hosts
  2. Use the arrow keys to go to the bottom of the line. Type 127.0.0.1 and the website address you want to block. For example, to block YouTube, I would enter the following in Terminal: 127.0.0.1 www.instagram.com
  3. Type each website on a separate line. Once you’re done, press Control + O followed by Return on your keyboard to save the file, and then Control + X to close the file.
  4. Type the following command to flush the cache, which will enforce these changes across the browsers. Press Return to enter the command: sudo dscacheutil -flushcach (I've also tried: sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder)


Full instructions found here.


TROUBLESHOOTING ATTEMPTS:

  • Tried different variations of the URLs:
  • with/without www.
    • with/without https://
  • Tried using 0.0.0.0
  • Tried using with and without a tab after the numbers
  • Cleared caches (Safari & deleted caches out of Finder folder), restarted, etc.

While these forum posts are older, I tried them without luck:


Forum 1

Forum 2


Here's what I'm seeing on my end for a quick example:




Any help will be much appreciated and hope it helps others in the same boat... thank you!

MacBook Pro 14″, macOS 13.4

Posted on Jun 20, 2023 2:27 PM

Reply
Question marked as Top-ranking reply

Posted on Aug 18, 2023 5:54 PM

Do you have VPN or Private Relay on? I had the same issue and found out that Private Relay was bypassing it - I turned off private relay and works like a charm.....now if someone can figure out how to make both of them work!

Similar questions

19 replies

Jun 20, 2023 2:33 PM in response to yokevinnn

I wouldn't really call this "blocking," you're just routing the URL to a new IP address. I guess the end result is the same, but you're not really "blocking" the domains. `Don't want to use Screen Time because it's easy to workaround.` - There is an easy workaround for this method as well.


I don't know your end goal here.. If you have children and are trying to block certain sites set up Parent Controls that will affectively block these sites.


I would also say it's more efficient blocking these sites at the router / modem level as it will persist over your Wi-Fi and all devices connected cannot access the website. Blocking this on each device individually is a hassle and obviously only works on Macs, as this will not prevent iPhone and iPad from reaching the website.



Jun 20, 2023 4:25 PM in response to yokevinnn

Set up your network with a Pi-hole or similar for DNS, and block all outbound DNS queries from your network at your firewall except for those originating from the Pi-hole host, and set the Pi-hole as the DNS server within your DHCP server configuration.


PS: that last entry in /etc/hosts shown above is incorrectly formatted, and—once corrected—a duplicate.


Jun 21, 2023 10:22 AM in response to pjswiz

Thanks for the response! This is just for personal productivity. The Terminal approach worked on my work computer, just not personal one—and helped with productivity much more than Screen Time. Curious as to why it would work on one Mac, but not another, despite them both being pretty new.


I'll def give blocking these at the router/modem level a shot, however!

Jun 21, 2023 3:10 PM in response to yokevinnn

yokevinnn wrote:

Thanks for the response! For your note on the last entry, do you mean the line with https? (Wonder if an incorrectly formatted entry has any effect on it functioning?)


This file is for domains only, is decades older than URLs and HTTPS and related, and is older than DNS itself.


Best case, the error here gets ignored.


Worst case, the error causes something (else) to tip over.


This text file is a very ancient and low-level and central part of IP networking.


The directions you're following are amusing, too. This part of the directions isn't going to work at all well: "For example, to block YouTube, I would enter the following in Terminal: 127.0.0.1 www.instagram.com".


And the cache flush command shown s also incorrect. It's more properly this:

sudo dscacheutil -flushcache


If you try to use the command listed, you'll get an error:


% sudo dscacheutil -flushcach
Password:
Usage: dscacheutil -h
       dscacheutil -q category [-a key value]
       dscacheutil -cachedump [-buckets] [-entries [category]]
       dscacheutil -configuration
       dscacheutil -flushcache
       dscacheutil -statistics
% 


I didn't look for other errors or issues with the directions.


I'd use Screen Time here, or would use Pi-hole or something else here, or a DNS-blocking service, and would generally discourage using the /etc/hosts file for this and such. It'll work for what you're trying yes, if you're careful, and if the directions are correct. Or that Rescue Time app mentioned earlier, if that works for you. For some folks, I've also removed the problem apps. Forcing a new download slows down the habit.

Jun 25, 2023 9:26 AM in response to MrHoffman

Awesome for the cache clearing... didn't realize the error! (BTW, the instructions you're chuckling at: the IG.com edit was my own, since I just pulled it from my own edits... but those instructions worked perfectly well on my work computer—also a new Mac. I'm only having trouble on personal computer—2 of them actually—old and new 🤷‍♂️).


Will go the Screen Time route for now and then potentially blocking at the router level if needed, as mentioned above.


Also spoke to someone from Apple and they also suggested adding layers of users to Screen Time: as in, create an Admin account separate to my main user account that'll disincentivize me from just tapping the "15 more minutes" button, etc.


Appreciate the insight, though, and since I've seen a few other people having this problem, I hope it helps some others!!


Aug 18, 2023 6:35 PM in response to ericm007

ericm007 wrote:

Do you have VPN or Private Relay on? I had the same issue and found out that Private Relay was bypassing it - I turned off private relay and works like a charm.....now if someone can figure out how to make both of them work!


The purpose and design of Private Relay is in conflict with the centralized data collection purpose of the first-few-hops VPN.


If you want privacy and security, consider using end-to-end TLS and Private Relay.


If you want your data to be centralized, personally identified, and quite possibly he metadata collected and logged, use end-to-end TLS as is usual, and wrap that im the first-few-hops VPN.


If you do need a first-few-hops VPN for geolocation shifting or testing CDN content or such, and still want some modicum of privacy, have a look at running your own Algo server.

Aug 20, 2023 10:56 AM in response to ericm007

This is actually a great rec: no VPN, but the Private Relay solution worked on everything except for Instagram's website 🤔. For ex., when I toggle the Private Relay function, access to Reddit goes in and out, so at least it works for that.


As for Instagram's site, I wonder how they're bypassing it... You'd think Apple would be a little bit more keen on backdoors to block companies they're competing with for ad spend revenue 😜. (Kidding, but wouldn't put it past them.)

Oct 26, 2023 3:03 PM in response to free-motion

Thanks for sending this! Unfortunately it didn't work for me... though, I hope it helps someone else coming here for the same reasons in the future.


So weird that it worked on my work computer, but not personal. And then Reddit, for example, is blocked successfully... but other sites won't budge.


Unfortunately, I can't block at the router level (Spectrum app doesn't support newer OS systems 🙃).


Will report back if I find a solution.

Can't block websites using Terminal

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.