Editing hosts file resulted in empty file

Discussion in 'OS X Mountain Lion (10.8)' started by DGLauren, Jun 19, 2013.

  1. DGLauren macrumors newbie

    Joined:
    Jun 19, 2013
    #1
    I seriously hope someone sees this and can help as quickly as possible. The Mac in question is a laptop my friend normally turns off after a few hours (especially at night), and we are afraid if we turn it off, it won't come back on during the next boot up.

    We're working on a new website. The hosting company instructed us to edit the hosts file so we could keep the site hidden from the public while we work on it.
    To do so, all we needed to do was add the IP address and domain to the hosts file. I had no problem doing this on my windows OS machine, but neither of us knew how to do it on a Mac, so I found and copied all the instructions from a tutorial on the hosting company's website. I went this route:

    Applications > Utilities > Terminal
    Once in the hosts file (it read "-- bash--80x24" across the top), I entered:
    sudo nano /private/etc/hosts
    At this point, we needed to enter his password, which he did.

    My instructions said to "...edit the hosts file, appending your new mappings underneath the default ones".

    That's where I ran into problems. Being unfamiliar with Macs, it never occurred to me to use the arrow keys to get the cursor "underneath" the defaults. Just clicking didn't work and tried to add any typing to the top line. The instructions failed to include that, and it was essential.

    We called Apple support. Two different techs sounded fairly upset that we were in the hosts file, trying to make changes, even when I explained it was simply to add the hosting site IP and our domain name. Finally, one had me go about it a totally different way, than through Terminal.

    I created a copy of the file (same name), saved to the desk top, then sent the original to the Trash. Next, I amended the copy and dragged it into the /etc directory. He also walked me through flushing the cache, as per the hosting site instructions. (dscacheutil -flushcache)

    The website was still not accessible to my friend's computer, but since we'd "fixed" the hosts file, or so we thought, I said we'd take it up with the hosting support personnel and call him back if there were problems. We even rebooted, once.

    I thought, before we get into chat with the host site's techs, I'd check the hosts file again and make sure my amendments were still there. I went the same route, Applications > Utilities > Terminal and entered the sudo info, knowing I wouldn't make any changes again.

    The file was empty. Stone cold, empty. Nothing. :eek: We immediately retrieved the original from the Trash and put it on the desktop. The name at the top of this file's window is Hosts -- Locked.

    The newly created file let me add the IP/domain info, but the Password request was no longer a popup, but one line in the hosts file window. Of course, it typed in invisibly, but once we hit Enter, I could type in the window. I typed all of the info from the orig into the newly created file and tried to save it via CTRL-O. A small black bar appeared at the bottom of that window, asking (something like): Name file to write: /etc/hosts

    I needed it to be named exactly that, but CTRL-G (? I think) for Get Help, said it couldn't use the same name. I clicked to close, thinking it would ask me if I wanted to save the changes, but it didn't. Just closed. Subsequently, reopening the file again showed it was again empty.

    Finally!! My questions:
    1. How do I get the original hosts file back into the /etc directory?
    2. Does the Mac Trash have a feature like the Windows Recycle Bin that lets you choose to restore the file to it's original location?
    3. Now that Hosts -- Locked is no longer in the Trash, will the machine be able to access it as needed during reboot looping?
    4. Will my friend's computer start up properly if it has a blank/empty hosts file?

    It's killing me that I cannot talk to anyone at Apple because I do not own an Apple computer and don't have a serial number, but I DO have a Mac related issue. Any help you can give me on this would be GREATLY appreciated. And, yeah, I did mean to shout. Thank you so much.

    ----------

    Sorry. I don't know much about it, but it's a Macbook Pro, OS-X, Lion. I hope that helps.
     
  2. DGLauren thread starter macrumors newbie

    Joined:
    Jun 19, 2013
    #2
    Correction: Make that Mountain Lion

    Reminds me of a line in a Jason Mraz song, I think it is, something about having a conversation with myself.

    So, it booted up this morning with no problem (sigh of relief!) and a good night's sleep rebooted my brain, as well. Seems perfectly logical that I can get the original hosts file back into the /etc directory the same way I took it out... just drag it from the desktop back in. I don't think I'll have a hard time deleting the modified copy I made, because it was never legitimate to begin with. I'll switch their locations, then delete the copy.

    Now that I know to use the arrow keys to navigate within that file, I'll try a second time to amend the hosts database to include the web-host's IP address and our domain name. I really want him to be able to see and comment on the website as it's under construction.

    I do have further questions regarding the options that appear after I click CTRL-O to save it, but I'll wait until there's someone communicating with me before I post further.

    Thanks again, in advance, for any help you can give.
     
  3. benwiggy macrumors 68020

    Joined:
    Jun 15, 2012
    #3
    1. You can always restore it from your backup.
    2. Yes. It's called "Put Back" and uses the same shortcut as deleting a file in the Finder, <command><backspace>.
    3. It all depends on whether it has the correct permissions. Moving it around in the Finder may cause problems.
    4. Don't know -- see what happens!

    I would recommend using the app TextWrangler, which is free from the Mac App Store, for easy editing of any system file, plist, etc, etc. It sorts out the necessary permissions, asking for authorisation if necessary, and shows you hidden files in its open file dialog.
    I consider it essential for any Mac user.
     
  4. DGLauren thread starter macrumors newbie

    Joined:
    Jun 19, 2013
    #4
    Thank you, benwiggy!

    Your quote, and then some:

    1. You can always restore it from your backup.
    2. Yes. It's called "Put Back" and uses the same shortcut as deleting a file in the Finder, <command><backspace>. Thank you so much for that!
    3. It all depends on whether it has the correct permissions. Moving it around in the Finder may cause problems. That's what I was most afraid of.
    4. Don't know -- see what happens! Thank goodness it started up this morning with no problems!

    I would recommend using the app TextWrangler, which is free from the Mac App Store, for easy editing of any system file, plist, etc, etc. It sorts out the necessary permissions, asking for authorisation if necessary, and shows you hidden files in its open file dialog.
    I consider it essential for any Mac user.

    You've been a great help; thank you so much. I'm heading over to his place now to put some of these things to use.
     
  5. DGLauren thread starter macrumors newbie

    Joined:
    Jun 19, 2013
    #5
    Solved it, just in case this helps someone else

    Even at the risk of exposing my own ignorance, I came back to post this on the off chance someone else in my situation is looking for solutions. (I had help from two different Apple support techs, btw, who couldn't help me with this. Ridiculous, given how simple it turned out to be and extremely embarrassing that it never occurred to me during the process.)

    The critical, missing piece of information (and the question I called Apple support for) was that you need to use the arrow keys to navigate around the Terminal... window, or however you refer to it in the Mac world. Once you do that, you see something entirely different after you click Ctrl-O.

    To append your hosts database:
    1. Go to Applications > Utilities > Terminal
    2. I'm going to call it a window. When that window opens, just start typing:
    sudo nano /private/etc/hosts
    3. You'll either get a pop up or a new line in the window asking for your password. Enter pw. (If you can't see what you're typing as you type your pw, don't worry. It won't show, but it's being entered.)
    4. The hosts database "window" opens. Use arrow keys to navigate to the bottom of the file, underneath existing default entries.
    5. Type in the IP address (in my case, of the webhosting site) and the domain name (of the website I'm building). Your entry will look something like this:
    000.00.0.00 yourdomain.com
    6. Click Ctrl-O. (This is one of the choices in the bottom of the window, and the ^ symbol stands for the control key in those choices. You can tell from the other choices that that is a letter "O" and not a number "0".)
    7. You'll see a notification at the bottom (of the window? of the Terminal?) above the choices:
    "Wrote 2 lines"
    ... or whatever number of lines you entered. That's exactly what you need to see.
    8. Hit Enter, to Save.
    9. Hit Ctrl-X to Exit.

    I was just lucky I guess that we didn't lock ourselves out of my friend's brand new Macbook.

    I modified the empty hosts database file (never thought of four lines as a database before, but I guess any number of entries can constitute a database, if that's the type file it is) by copying everything in the original and pasting it in, plus my new line of data. Left both files where they were... original on the desktop and the newly created one in /private/etc/hosts. Tested the results and he's now able to access the hidden website.

    Thanks for reading, thanks for your help, and please forgive me for how long it takes me to say something and feel comfortable that it is coherent.
     
  6. MeatRocket macrumors regular

    Joined:
    Jun 9, 2013
    Location:
    In the Sandbox
    #6
    I'm really glad you got it all sorted! This is a perfect example of just how frustrating it can be working on two totally different systems. Like you said, in Windows it's all easy peasy. We (Windows users) just tend to take for granted that the same operation is going to be equally as easy on a Mac. Things between the two don't always have a direct one-to-one correlation and we just have to accept that and do our best to learn on the new system.
     
  7. benwiggy macrumors 68020

    Joined:
    Jun 15, 2012
    #7
    As said, you just have to edit a text file. If you're not comfortable with using a text editor in the Terminal, you can use a GUI app text editor to do so, as I mentioned earlier.

    What could be easier?
     

Share This Page