I want to block some time consuming websites on my macOS.
Therefore I added some entries in /etc/hosts like
127.0.0.1 youtube.com
I reset the DNS Cache with sudo killall -HUP mDNSResponder.
ping youtube.com seems to work, as the respons comes from 127.0.0.1.
But all my installed Browsers still shows youtube.com. Is there anything I missed?
Related
I tried to use /etc/hosts file to redirect some websites to localhost.
To do so, I opened it in terminal using sudo nano /etc/hosts then I modified the file, and saved it. As the last step I flushed the DNS cache with sudo killall -HUP mDNSResponder.
Here's what my hosts file looks like:
$ cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 somethig.com
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
This simply doesn't work. I tried to reboot, without luck. I also did a lot of research but did not find any working answers on the internet. Does the OS still use this file, or will this never really work?
I'm using macOS 10.13.
I believe that you need to also override the IPv6 address for "something.com". Do this by adding the additional line "::1 somethig.com". So far as I can figure out, restarting Safari is also required. (You may also need to flush the DNS cache via the method you mentioned.)
I have trouble with dnsmasq - it does not resolve directly defined name.
$ sudo dnsmasq -d -A /test/172.17.0.2 --log-queries &
dnsmasq: started, version 2.48 cachesize 150
dnsmasq: compile time options: IPv6 GNU-getopt DBus no-I18N DHCP TFTP "--bind-interfaces with SO_BINDTODEVICE"
dnsmasq: read /etc/hosts - 2 addresses
$ ping test
ping: unknown host test
What is wrong?
You only set up a server. Your system's resolver (which is used by ping, your browser, and all other applications on your machine) must first know that this server exists and that it should be used. This can be done by modifying /etc/resolv.conf. For first, make sure, this line is in that file:
nameserver 127.0.0.1
But beware: modern systems auto-generate this file and potentially overwrite your changes. So watch out for "DO NOT EDIT THIS FILE BY HAND" comments in that file and instead do what's recommended in the file.
In general, we can find out the DNS server addresses we are using by cat /etc/resolve.conf. However, after dnsmasq is installed, the DNS server address becomes 127.0.0.1 in the file /etc/resolve.conf. How can I find out the real DNS server addresses in this case?
Well, recently I finally find out the way to see which DNS servers are using.
cat /var/log/kern.log | grep nameserver will show the DNS server configuration received by the NetworkManager.
nmcli dev show <IF> | grep DNS will also do that.
cat /var/log/syslog | grep dnsmasq will also tell you which upstream nameservers dnsmasq is using. This may be the most accurate way.
cat /var/run/NetworkManager/resolv.conf also works for me.
I have not had a similar issue in Windows (10) and nothing I've searched on docs seems to indicate why, if this does not work, that that is the case
I open up terminal and edit /etc/hosts (which I've done many times on a PC and a few years back on an OSX too)
Just for grins if that doesn't work I type in sudo killall -HUP mDNSResponder
Then for more grins I reboot
This has absolutely no effect. Can anyone point me to why? Thanks.
UPDATE: The embarrassing fact of the matter is that the lines I entered in /etc/hosts were in reverse, i.e. domain first, as:
mydomain.com 192.168.33.10 #wrong
192.168.33.10 mydomain.com #what it should have been
The accepted answer, however, is well-written and appreciated.
I've seen two common problems with using /etc/hosts on macOS (/OS X):
Incorrect formatting: each entry in the /etc/hosts file must be an IP address followed by a space or tab, followed by the name, then a linefeed at the end of the line. Try printing the hosts file with cat -vet /etc/hosts to make normally invisible characters visible. Each line should look like one of these:
127.0.0.1^Iwww.example.com$
127.0.0.1 www.example.com$
(The "^I" is a tab, and the "$" is the linefeed at the end of the line.) It's also ok if the entry has multiple names listed (also separated by spaces or tabs).
If you see a "^M" (carriage return) just before the "$", you have DOS/Windows formatted text and you need to remove the carriage return(s).
Incorrect testing: Don't use the command-line tools dig, host, and nslookup, since they all test DNS directly and therefore bypass the /etc/hosts file. Browsers sometimes cache things, which can give misleading results. The "right" way to test the system's name resolution system is with the dscacheutil command:
dscacheutil -q host -a name www.example.com
...but since that's annoyingly verbose, I tend to just use ping, and see what address it says it's going to test.
I came across this thread to try and solve the same issue on macOS Catalina and was not successful. This is because macOS Catalina has another thing going; it will only make changes in the hosts file effective if you change them as the root user (this is not done with the sudo command) !!
By default there is not a root user on your system so here's a link with a step by step guide to do so:
https://support.apple.com/en-us/HT204012
then I was able to:
su
nano /etc/hosts
for more information:
https://discussions.apple.com/thread/250805304
Below steps worked for me
flushing DNS sudo killall -HUP mDNSResponder (or kill DNS in activity monitor and let it reload)
2.Changing read-write permissions of /etc/host file should be -rw-r--r-- .Use commands $ sudo chmod g+r /etc/hosts and $ sudo chmod o+r /etc/hosts
Adding entry u want to add in host file with ipxxx.xx.xx.com
moved these 2 lines in the end
255.255.255.255 broadcasthost
::1 localhost Administrators-MacBook-Pro.local
Step 1 again
On my server machine, I have an application that responds to port 9876.
I've closed the application with kill.
If I do netstat | grep 9876, no process is shown.
However, I can still do POST and GET request to the server machine (from both Postman and Chrome) on port 9876.
How is that possible?
Just netstat doesn't show you listening sockets/process
Issue sudo netstat -lp | grep 9876, as Payalord mentioned, if you don't sudo you'll only list sockets controlled by your user. The last column will be PID/Program name which will help you find out who's keeping this socket open.
man pages are your friends:
-p, --program
Show the PID and name of the program to which each socket belongs.
-l, --listening
Show only listening sockets. (These are omitted by default.)
As for avoiding the application from spawning a subprocess, you'll need to investigate this as there's not enough information here to know why it happens and how to avoid it.
Make sure you are running commands on the right user, usually the best to run the commands on root user.
Also maybe better to run netstat like this: netstat -tulpn | grep :9876
Hope this helps to clarify the problem.