Skip to content

Instantly share code, notes, and snippets.

@copley
Created August 25, 2024 11:23
Show Gist options
  • Save copley/98bafe0dad0fc0c1a54e48759d1afea7 to your computer and use it in GitHub Desktop.
Save copley/98bafe0dad0fc0c1a54e48759d1afea7 to your computer and use it in GitHub Desktop.
/etc/hosts
If changes to the /etc/hosts file on your macOS are not working as expected, here are some common troubleshooting steps to help resolve the issue:
1. Check File Format and Syntax
Make sure there are no syntax errors in the /etc/hosts file.
Each entry should be on a new line with the IP address followed by one or more hostnames, separated by spaces or tabs. For example:
Copy code
127.0.0.1 localhost
192.168.1.1 example.com
Avoid using special characters and ensure there is no trailing whitespace after the entries.
2. Flush DNS Cache
After editing the /etc/hosts file, you need to flush the DNS cache for the changes to take effect. Open Terminal and run:
bash
Copy code
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
This command will flush both the DNS and the mDNSResponder caches.
3. Check File Permissions
Ensure that the /etc/hosts file has the correct permissions. The file should be writable by the root user. You can check the permissions by running:
bash
Copy code
ls -l /etc/hosts
The output should look something like this:
bash
Copy code
-rw-r--r-- 1 root wheel 312 Sep 15 12:34 /etc/hosts
If the file permissions are incorrect, you can fix them with:
bash
Copy code
sudo chmod 644 /etc/hosts
4. Edit with Proper Privileges
Ensure you are editing the /etc/hosts file with the proper privileges. Use sudo with a text editor like nano or vi:
bash
Copy code
sudo nano /etc/hosts
After editing, save the changes and exit the editor.
5. Disable or Configure VPNs and Proxies
Sometimes VPNs or proxy settings can override or bypass the /etc/hosts file. Try disabling the VPN or proxy temporarily to see if the /etc/hosts changes take effect.
6. Check for Hostname Conflicts
Ensure there are no conflicting entries for the same hostname in the /etc/hosts file. Remove or comment out any duplicate or conflicting entries.
7. Restart Network Services
Restart your network services to ensure the changes are fully applied. You can do this by turning your Wi-Fi off and on again or by rebooting your computer.
8. Test the Changes
Test the changes by pinging the hostname you modified or added in the /etc/hosts file. Open Terminal and run:
bash
Copy code
ping example.com
Ensure it resolves to the IP address you specified in the /etc/hosts file.
9. Verify No Other DNS Overriding
Check if other services, such as dnsmasq, are installed and might override DNS settings.
If you've tried all these steps and the hosts file is still not working, there could be an application or system-level restriction affecting DNS resolution. Let me know if you need further help!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment