How to edit /etc/hosts without breaking your local setup
Most people open /etc/hosts , change one line, refresh the browser, and hope. That works until it does not. Then you spend twenty minutes on Permission denied , a forgotten DNS flush, or a commented line from last week that is still active. This is a simple workflow that keeps hosts edits boring. What the hosts file does When your machine resolves a name like myapp.test , it can use a local override before public DNS. Common cases: Point myapp.test to 127.0.0.1 for local work Point a real domain at a staging IP before DNS cutover Temporarily block a host with 0.0.0.0 Give services readable names instead of raw IPs The idea is simple. The mess comes from how people edit and apply it. A workflow that holds up 1. Do not treat /etc/hosts as your only copy Keep a file you own: ~/dev/hosts/personal.hosts Or one file per project / client. Edit that. Apply it on purpose. 2. Edit the copy, then copy it into place macOS / Linux: code ~/dev/hosts/personal.hosts sudo cp /etc/hosts "/etc/hosts.bak. $( date +%Y%m%d-%H%M%S ) " sudo cp ~/dev/hosts/personal.hosts /etc/hosts Windows: edit your copy, back up the live file, then replace: C :\ Windows \ System32 \ drivers \ etc \ hosts You need admin rights for the live file. That is normal. 3. Flush DNS every time you apply Make this part of the apply step, not a later panic search. macOS sudo dscacheutil -flushcache ; sudo killall -HUP mDNSResponder Windows (Admin) ipconfig /flushdns Linux (systemd-resolved) sudo resolvectl flush-caches 4. Verify in the terminal before the browser ping -c 1 myapp.test # Linux: getent hosts myapp.test Right IP in the terminal, wrong page in the browser? Stop rewriting hosts. Look at browser DNS, HTTPS, redirects, or HSTS. 5. Avoid two active lines for the same hostname This breaks people constantly: 10.0.0.5 www.client.com 127.0.0.1 www.client.com Pick one. Comment the other, or better, keep separate profile files and swap the whole file. Example: local frontend + API 127.0.0.1 shop.test 127.0.0.1 api.