Dev.to
7/16/2026

Fix Permission denied when editing /etc/hosts
Short summary
A concise guide to resolving 'Permission denied' errors when editing the system hosts file on macOS, Linux, and Windows. Covers using sudo or Administrator elevation, a safer copy-then-install pattern with backups, and flushing DNS afterward. Also includes troubleshooting tips for common mistakes like editing the wrong file or forgetting to flush the DNS cache.
- •Use sudo (macOS/Linux) or Run as Administrator (Windows) to edit the protected hosts file
- •Safer approach: edit a working copy you own, back up the original, then install the copy with sudo
- •Flush DNS after changes and verify with grep; keep project hosts snippets versioned outside the system file
Generated with AI, which can make mistakes.
Is this a good recommendation for you?



