Apple - How to edit the /etc/hosts file?

In one line, from Terminal:

sudo -b "/Applications/Textedit.app/Contents/MacOS/TextEdit" /etc/hosts

Make your changes, save and close.


sudo nano /private/etc/hosts

to edit the hosts file within Terminal, Control-O to save, then hit enter


  1. Open TextEdit as sudo

    sudo -b /Applications/TextEdit.app/Contents/MacOS/TextEdit
    
  2. Open /etc/hosts in this new TextEdit instance using any of the following methods:

    • Append the path to the end of the previous command
    • Use File → Open
    • Drag the file to the sudo'd TextEdit on the Dock

Alternatively, you can edit in Terminal using nano:

SUDO_EDITOR=nano sudo -e /etc/hosts

Using sudo -e ensures that temporary files etc. are handled in the proper way and you don't run into permission issues with them. nano is a simple console-based editor which is quite user-friendly.