When I open the file with TextEdit (I don't want (truly, I am not able) to use vim), it is seen as "locked", even if I modify the permissions of the file via the "Get Info" window.

How can I modify the hosts file with TextEdit?

  • 4
    I found nano to be a more approachable terminal-based text editor than vim for simple task like this.
    – Édouard
    Mar 8, 2014 at 12:58

9 Answers 9


In one line, from Terminal:

# Catalina and newer
sudo -b /System/Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

# before Catalina
sudo -b /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

Make your changes, save and close.

  • 4
    I'm on Mountain Lion and this doesn't work for me unfortunately. Whenever I try to change something I get an error "You don't own the file hosts and don't have permission to write to it". May 31, 2015 at 19:37
  • After fixing the path for Catalina this solution still does not work. The `-b' option is for "background" I am not sure how that is relevant.
    – John
    May 17, 2022 at 14:25
sudo nano /private/etc/hosts

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

  • This is correct and should be used instead of opening with TextEdit (since that hasn't worked since OS X Lion rolled out).
    – CodeBiker
    Nov 17, 2020 at 18:10
  1. Open TextEdit as sudo

     sudo -b /System/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.


You could try TextWrangler for this sort of thing; much more capable than TextEdit, and if you use the direct install from BareBones Software instead of the App Store version, it will happily open locked files. When you try, it asks permission, reminding you that you are not a member of the required group, but password entry will allow you to open and edit the file. Plus, it has optional command line additions (also not built in to App Store version) that will allow you to use TextWrangler by entering edit (name of file) to open things directly from Terminal. Open File dialog has a handy "show invisibles" option to help you open and edit hidden or dot-prefixed files too. It's a free app so no risk in trying.

  • 2
    Update for posterity: TextWrangler is now being deprecated, so BBEdit instead (TextWrangler was a "light" version of BBEdit, current unlicensed versions of BBEdit will have the same functionality as TextWrangler after the demo period expires. Switching to BBEdit even retains your old TextWrangler preferences - which is a nice touch.
    – dr.nixon
    Apr 3, 2017 at 17:49

If you like simple, you can research vim tool which installed on MacOS: sudo vi /etc/hosts


TextEdit will not work for this use case. Use an editor like Sublime Text and you will have fewer problems editing files like the hosts file.


I'm using macOS Big Sur and this is what worked for me:

sudo vi /etc/hosts

As opposed to next command that didn't work:

sudo open /etc/hosts

I got this error message

You don’t own the file “hosts” and don’t have permission to write to it. You can duplicate this document and edit the duplicate. Only the duplicate will include your changes. [Cancel] [Duplicate]

  • Not sure how this adds to the list of answers already given.
    – nohillside
    Dec 30, 2020 at 10:34

What worked for me is creating a root user following https://support.apple.com/en-us/HT204012

Then simply:

su root

and do changes.

  • 1
    Wouldn‘t it have been enough to use sudo -s (which gives you root without enabling root login)?
    – nohillside
    Jun 21, 2022 at 14:08
  • @nohillside, I tried that and sudo was not enabled or something. but now i tried and sudo works. Jun 21, 2022 at 14:13
  • Not sure about how sudo can be disabled, but good you found a way :-)
    – nohillside
    Jun 21, 2022 at 14:16
  • 1
    @nohillside, it said command not found, ,maybe i misspelled or something, but now Disabled root and tried sudo again and it still works. Ill just use sudo then :) .. thanks for pointing out Jun 21, 2022 at 14:18

I am in HighSierra, and I remember well from Lion and later until including HighSierra that you select "Hosts" and open it with Textedit without any ado.

  • What you posted really doesn't answer the question that was asked and I'd encourage you to reread the OP! Jan 3, 2019 at 18:56
  • FWIW I opened the hosts file in TextEdit under macOS High Sierra and as soon as I started to type in it, I got... "You don’t own the file “hosts” and don’t have permission to write to it. You can duplicate this document and edit the duplicate. Only the duplicate will include your changes. [Cancel] [Duplicate]" The hosts file belongs to the root user and while anyone can easily open the hosts file in TextEdit, one cannot just edit it unless opened as root, as suggested in the accepted answer, although that too may have issues all these years later. Jan 3, 2019 at 18:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .