Results 1 to 10 of 10
  1. #1

    Shikarnov's Avatar
    Member Since
    Feb 26, 2008
    Posts
    542
    Tip: Unlocking Hosts file in Lion
    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

  2. #2

    vansmith's Avatar
    Member Since
    Oct 19, 2008
    Location
    Toronto
    Posts
    19,782
    Specs:
    2012 13" MBP (2.5 i5, 8GB)
    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.
    Important Links: Community Guidelines : Use the reputation system if you've been helped.
    M-F Blog :: Write for the blog
    Writing a Quality Post

  3. #3

    Shikarnov's Avatar
    Member Since
    Feb 26, 2008
    Posts
    542
    ^ A prudent warning, indeed. Thanks for bringing it up.

  4. #4

    cradom's Avatar
    Member Since
    Feb 14, 2004
    Location
    Groves, Texas
    Posts
    4,501
    Specs:
    21in. iMac 10.11 --- HP win 10
    And of course next time you do a repair permissions it'll probably change back.
    Delusions are often functional. A motherís opinions about her childrenís beauty, intelligence, goodness, et cetera ad nauseam, keep her from drowning them at birth.
    -Lazarus Long

  5. #5

    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    Location
    MA
    Posts
    8,501
    Specs:
    27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 6S, iPod Nano 7th GeniPad 3
    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.
    --
    Regards
    ...Ashwin



    Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.

  6. #6

    dekan's Avatar
    Member Since
    Mar 16, 2007
    Posts
    756
    Quote Originally Posted by Raz0rEdge View Post
    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 :-)

  7. #7

    vansmith's Avatar
    Member Since
    Oct 19, 2008
    Location
    Toronto
    Posts
    19,782
    Specs:
    2012 13" MBP (2.5 i5, 8GB)
    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).
    Important Links: Community Guidelines : Use the reputation system if you've been helped.
    M-F Blog :: Write for the blog
    Writing a Quality Post

  8. #8

    dekan's Avatar
    Member Since
    Mar 16, 2007
    Posts
    756
    Quote Originally Posted by vansmith View Post
    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!

  9. #9


    Member Since
    Feb 06, 2012
    Posts
    1
    Cool
    Quote Originally Posted by dekan View Post
    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!

  10. #10

    vansmith's Avatar
    Member Since
    Oct 19, 2008
    Location
    Toronto
    Posts
    19,782
    Specs:
    2012 13" MBP (2.5 i5, 8GB)
    Quote Originally Posted by dekan View Post
    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.
    Important Links: Community Guidelines : Use the reputation system if you've been helped.
    M-F Blog :: Write for the blog
    Writing a Quality Post

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. hosts file
    By ankhseeker in forum OS X - Operating System
    Replies: 10
    Last Post: 12-18-2014, 11:54 AM
  2. Mountain Lion Hosts File
    By Shikarnov in forum OS X - Operating System
    Replies: 4
    Last Post: 04-01-2013, 01:27 PM
  3. hosts file
    By afcbob in forum OS X - Operating System
    Replies: 2
    Last Post: 11-29-2009, 12:21 PM
  4. Hosts File
    By traveler09 in forum OS X - Operating System
    Replies: 3
    Last Post: 07-29-2009, 07:59 AM
  5. Where is the hosts file?
    By Stoomie in forum OS X - Operating System
    Replies: 3
    Last Post: 08-10-2004, 03:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •