How to edit hosts file on mac?

Run terminal and open file with nano;

sudo nano /private/etc/hosts

To edit the file, you’ll add your own lines like:

158.293.32.45 www.domain.com domain.com

When you’re done making changes, press Control-X to exit the editor, and Y to save.

Finally you may need to flush your DNS cache:

dscacheutil -flushcache