Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

danallen

macrumors regular
Original poster
Oct 8, 2018
119
34
Houston
1603300273906.png


Today, I tried changing the hosts file. The result is the hosts file is now ignored. Can you help me get it working again?

STEPS
  1. Loggedin as root user in terminal
  2. Created a file containing what I wanted in the hosts file as ~/hosts This file contained a copy of what was in the hosts file, with a long long list of domains like this 127.0.0.1 domain.tlc My intention was to block ads for all those domains.
  3. Copied /private/etc/hosts to /private/etc/hostsHOTROLL (to keep a backup of the hosts file)
  4. Deleted /private/etc/hosts
  5. Copied ~/hosts /private/etc/hosts
  6. ran from command line as root user: killall -HUP mDNSResponder
The result did not perform as expected, so I wanted to roll back to what I had. I did that by:
  1. Deleted /private/etc/hosts
  2. Copied /private/etc/hostsHOTROLL to /private/etc/hosts
  3. an from command line as root user: killall -HUP mDNSResponder
The problem now is I have some domains need to go to certain ips, but that is not happening. It is as if the hosts file is not there.

Here is what is in my hosts file:
Code:
##
# 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
255.255.255.255    broadcasthost
::1             localhost


10.0.0.16 busterboss.fortress200g.work
10.0.0.17 rmw.fortress200g.work
10.0.0.15 mondostretch.fortress200g.work
10.0.0.8  debian-bups-500g.fortress.work

I can ping 10.0.0.15
When I ping mondostretch.fortress200g.work, it is resolving to the ip on the name server where it is registered. It needs to resolve to 10.0.0.15, which is an ip on my lan, not reachable from the internet.

Any assistance you can provide will be immensely appreciated.

1603300522283.png


1603300683553.png

Thank you.
 
Last edited:

danallen

macrumors regular
Original poster
Oct 8, 2018
119
34
Houston
Check permissions on the hosts file, particularly ensure its world readable (`sudo chmod +r /etc/hosts` if not)

Thank you for taking a look at this. Since first posting this question, I edited the OP to show the file listing, showing permissions on the hosts file. The file permissions are:
1603301098778.png


I ran the command you posted for good measure, no change.

To verify it is readable, I logged into terminal with my account, not the root account, then ran cat /private/etc/hosts:
1603301243822.png


This is a copy of the file that was working until I tried editing it.
 

Stephen.R

Suspended
Nov 2, 2018
4,356
4,746
Thailand
I’ve had a similar issue with a generated hosts file (ie a tool automatically makes an edit to add new entries, and suddenly no entries are respected).

ive never worked out exactly what causes that specific issue (besides once or twice when something caused it to become unreadable for non root users) but removing the custom lines (and then running the tool again) usually does the trick - my guesses are (a) it inadvertently inserts a null byte or some other invalid character into the file; (b) doesn’t save the file with a trailing newline character;
Or (c) the file is aggressively cached by something.

id try adding a dummy line manually (eg even just point foo.com to loopback ip or something) and see if it resolves properly, then try adding the lines you want; also don’t rule out a restart. Yes that should be overkill, but overkill that works is still a valid option.

oh also how did you edit the copied hosts file (ie what program?)
 
  • Like
Reactions: danallen

danallen

macrumors regular
Original poster
Oct 8, 2018
119
34
Houston
The file that I wanted to use as the new hosts file was created using a program editor called Atom. Atom cannot edit the hosts file directly, because it runs with my regular user account, which has read only permissions. So I created a file in Atom stored in my home directory, then used terminal to
1. delete /private/etc/hosts
2. Copy the new hosts file from my home directory to /private/etc/hosts

When that did not do what I was looking for, I deleted the hosts file again, and copied a backup of the hosts file I made this morning back to /private/etc/hosts Seems to me, that should have restored my computer to what it was doing.

I did system cold boot, no change.

This really sucks out loud.

Trying with a new hosts file and the test you suggested.
 

danallen

macrumors regular
Original poster
Oct 8, 2018
119
34
Houston
FIXED

Restarting twice fixed it.

FIRST RESTART
After the first restart, there was no hosts file. I cannot prove there was a hosts file present when I shutdown, but I thought there was. The way I have bash history (I run bash, not zsh) setup, I cannot always see what I ran. I don't understand bash history well. All I can say is the history appears to be kept separately for different tabs open to the same server. So, when I open terminal now, the history is for a different session than the one I was using for manipulating the hosts file.

When there was no hosts file, I re-copied from the backup I had made to recreate the hosts file as it was before the problem arose. Pinging one of the domains (mondostretch.fortress200g.work) in the hosts file continued responding from the ip associated with the domain on the name server where I have the domain registered.

SECOND RESTART
For kicks, I restarted again, actually thinking I might see the hosts file disappear again. Instead, the hosts file was there and it now is functioning correctly (i.e., pinging mondostretch.fortress200g.work responds with the ip indicated in the hosts file.

All my prior experience editing the hosts file showed that changes to the hosts file always took immediate effect. Not necessary to restart a service, flush any caches, much less, restart the computer.

Trying to reproduce some of this...
 

danallen

macrumors regular
Original poster
Oct 8, 2018
119
34
Houston
KEY FINDING
If the hosts file is deleted, then a new hosts file created, the hosts file will be ignored. Restarting the system will cause it to be recognized.
 

superparati

macrumors regular
Apr 11, 2016
169
36
London
Dear,

I feel this is an ongoing issue as I can see a lot of topic on google talking about it but without very much a solution.
I've the same issue on a brand new computer, MBP16" with Catalina
The computer is running with a profiles setup by my company running with parallel application portal.

I updated the hosts file by copy and pasting the content to the new hosts file + flush the dns -> did not work
I've deleted the host file, created a new one with root access -> did not work
I've run this command just to be sure sudo chmod +r /etc/hosts -> did not work
Between each hosts file update I've rebooted my computer and do additional dns flush running this command "sudo killall -HUP mDNSResponder" -> did not work
I've also installed
Onyx and did run the maintenance script + reboot -> did not work

Current permission of the file
Screenshot 2021-02-18 at 15.07.07.png


When I try to ping my website from the IP, I've Request timeout.
The lookup with the domain name point toward the right IP but nothing more.

Any other suggestion would be more than welcome.

Thanks
 
Last edited:

Clark2201

macrumors newbie
Aug 12, 2021
1
0
I'm having a problem with hosts files also (well actually safari).
I'm trying to restrict certain websites using the hosts file, but it seems that Safari ignores it when the hostname doesn't start with 'www'.

For example:

127.0.0.1 www.facebook.com
#works fine

127.0.0.1 movies.app
#ping shows 127.0.0.1 looping and chrome uses hosts, but safari ignores hosts in this case and just goes directly to the website

Is there any config change available to make safari work as expected?
 

auxbuss

macrumors 6502
Feb 18, 2014
439
322
UK
Just adding a note to say that most (all?) browsers, and some command line tools, will lookup both IP6 in addition to IP4 entries in `/etc/hosts/`. So, if there is no IP6 entry in the hosts file for a domain, then the domain might not be blocked – if that's what you're trying to do.

So you require something like this, for example:
Code:
127.0.0.1 www.facebook.com
::1 www.facebook.com

Here's the response from `curl`:
Code:
❯ curl -v https://www.facebook.com
*   Trying [::1]:443...
* connect to ::1 port 443 failed: Connection refused
*   Trying 127.0.0.1:443...
* connect to 127.0.0.1 port 443 failed: Connection refused
* Failed to connect to www.facebook.com port 443 after 1219 ms: Couldn't connect to server
* Closing connection 0
curl: (7) Failed to connect to www.facebook.com port 443 after 1219 ms: Couldn't connect to server
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.