6

I add to my /etc/hosts file this :

127.0.0.1 youtube.com

Save the file. Flush everything with :

dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Reboot my computer 30 times.

Then I run:

dscacheutil -q host -a name youtube.com

And I get :

name: youtube.com
ip_address: 216.58.220.46

Clear my cache completely since the beginning of time in any browser, go to : youtube.com, and I get youtube.com, not my localhost.

I'm using OS X 10.10.5, Yosemite.

  • Is /etc/hosts still in use on OS X 10.10? – CousinCocaine Sep 30 '15 at 16:26
  • @CousinCocaine It appears so, but and it seems like it works for other folks. Though I've read a handful of threads where folks seem to be having similar issues. – Trip Sep 30 '15 at 16:27
  • 1
    I thought 10.10 moved away from /etc/hosts. Dont know the new way/mechanism of doing this. Not sure though. – CousinCocaine Sep 30 '15 at 16:31
  • First, dscacheutil and mDNSResponder doesn't affect /etc/hosts. Secondly, watch out with https sites as they don't play nice with these tricks. Additionally sites as facebook and youtube have a huge load of other hostnames. Blocking them using /etc/hosts is next to impossible. Better use something like Little Snitch if you really need to block those. – EDP Feb 6 '16 at 9:23
  • The problem stands around the 30 times. – dan Aug 8 '16 at 14:15
8

Check your hosts file, because probably it's malformed. It should look similar to this one:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
127.0.0.1       youtube.com
127.0.0.1       www.youtube.com
255.255.255.255 broadcasthost
::1             localhost
 

Opening the file with sudo nano /etc/hosts it should have a trailing empty line. All entries for 127.0.0.1 should be added above the broadcasthost. The space between IP-address and hostname should be one tab: .

After writing the file to disk with nano, the changes should take effect immediately (usually without executing any dnscacheutil or killall commands). Check this with ping youtube.com.

Google Chrome is (probably) the only app which doesn't adhere properly (as a test in a VM revealed).

  • OOC, can I use the hosts file to make youtube.com alias to a different remote site? For example, going to youtube.com would be google.com ? – Trip Sep 30 '15 at 16:58
  • @Trip Sure! That was the purpose of the hosts file in ancient days: <ip-address-google.com> tab youtube.com and www.youtube.com but only if the google-ip is different than the youtube-ip. In my case it isn't: both redirect to 216.58.213.46 – klanomath Sep 30 '15 at 17:01
  • Ah so it can only be redirected to a specific IP then and not a general address ( ie. bobmarley.com )? – Trip Sep 30 '15 at 17:24
  • 1
    @Trip On the left side has to be an ip-address and on the right a hostname. – klanomath Sep 30 '15 at 17:33
  • Ah thanks so much good sir. You helped me so much. There's no trendy way to mask just a subdomain is there? – Trip Sep 30 '15 at 17:40
4
sudo killall -HUP mDNSResponder

No reboot required.

"It works!" for me, OS X 10.11.6

-2

100% working try so:

Ex:
0.0.0.0 example.com
::  example.com
0.0.0.0 www.example.com
::  www.example.com

You have to put 0.0.0.0 and :: both for www. or without www

  • 1
    Welcome to AskDifferent. Please have a look at the faq to see what needs to be considered in writing good answers. Right now it‘s not really clear how your answer actually helps to solve the problem described in the question. To amend your answer just use the „edit“ link at the bottom of your text. – nohillside Oct 7 '18 at 7:07

You must log in to answer this question.

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