Tip: Unlocking Hosts file in Lion

Joined
Feb 26, 2008
Messages
542
Reaction score
25
Points
28
Hi All,

As my job involves developing websites, I spend a LOT of time in my hosts file adding domain names that point to my local server, and etc.

Well, after upgrading to Lion, I found that my hosts file (which I keep as an alias on my dock) opened in TextEdit instead of TextMate, like I had it set to open with in Snow Leopard. "No matter," I thought, "I'll just edit in TextEdit and go on with my day."

So I edited, but it wouldn't save. Of course, Lion locked the hosts file, which I suppose shouldn't have been unexpected. What was unexpected was that the Get Info pane wouldn't let me make changes, even after authenticating. My username wasn't there, and I couldn't add it either. I thought I was screwed...

Anyway, long story short, I found a procedure that let me reclaim control over my hosts file by pushing through what I think is a bug, and the terminal.

  1. In Finder, navigate to /private/etc/
  2. Locate the hosts file
  3. Right-click on it and select Get Info
  4. Unclock the Get Info pane and authenticate
  5. Add yourself as a user (note - after adding, your username still won't appear in the list)
  6. Close the Get Info box
  7. Open Terminal
  8. sudo nano /etc/hosts
  9. CTRL + O to save
  10. Go back to Finder, right-click on the hosts file and Get Info again.
  11. Your username will be there. Set its permissions to Read and Write.

Cheers,

Z
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
I'm sure it's more convenient but I'm not sure you want to go around making files outside of the user directory r/w. This is a bad habit to get into (from a security standpoint) and there's a reason that the file has limited permissions.

Perhaps a warning is warranted for such a tip: don't do this for any files unless you absolutely know what you're doing and feel comfortable fixing things (in the case that something goes wrong). Even then, I'd still recommend against doing it.
 
Joined
Feb 14, 2004
Messages
4,781
Reaction score
166
Points
63
Location
Groves, Texas
And of course next time you do a repair permissions it'll probably change back.
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,745
Reaction score
2,071
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
So in the end wouldn't it be easier just to "sudo vi" the file from the terminal preserving the permissions from any disk repairs while keeping your changes.
 
Joined
Mar 16, 2007
Messages
756
Reaction score
14
Points
18
So in the end wouldn't it be easier just to "sudo vi" the file from the terminal preserving the permissions from any disk repairs while keeping your changes.

That's exactly how I do it - except vi is way over my head so I am using nano :)
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
Yes, it would be easier and probably significantly safer.

You could also cobble something together in AppleScript:
Code:
do shell script "open /Applications/TextEdit.app/ /etc/hosts" with administrator privileges
Save that as an application in AppleScript Editor and voila. Each time you launch the application, you can enter your password and then you'll have write access (you might have to unlock the file however).
 
Joined
Mar 16, 2007
Messages
756
Reaction score
14
Points
18
Yes, it would be easier and probably significantly safer.

You could also cobble something together in AppleScript:
Code:
do shell script "open /Applications/TextEdit.app/ /etc/hosts" with administrator privileges
Save that as an application in AppleScript Editor and voila. Each time you launch the application, you can enter your password and then you'll have write access (you might have to unlock the file however).

Love it!
 
Joined
Feb 6, 2012
Messages
1
Reaction score
0
Points
1
That's exactly how I do it - except vi is way over my head so I am using nano :).

vi is pretty sweet. vim is even better. You can learn vim by typing "vimtutor" into a terminal window. It will walk you through how to navigate and use many of the vi/vim features!
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
That's exactly how I do it - except vi is way over my head so I am using nano :)
You could use MacVim which is like a "hybrid" in that you can use normal OS X keyboard shortcuts and Vim ones. It provides you with an easy way to transition into the world of Vim.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top