5

I'm trying to edit my hosts file, because doing ssh <some host in hosts> fails to translate to ip.

according to the Finder UI it is found under:

enter image description here however when trying to run cat hosts from the terminal I get an error:

➜  /etc  cat hosts
cat: hosts: No such file or directory

I tried to create a new hosts file but then I see another one is created next to the first one

enter image description here

but when I try to edit the second hosts I need to duplicate to anther destination. I copy it to /etc and again the first problem occurs.

I have tried to change the permissions to everyone can read and write. But yet it didn't help.

2

There shouldn't be a way to have two identically named files in the /etc folder. Can you do a Get Info on the original and verify the Name & Extension (and that the extension is not hidden, and that there are no spaces at the end of the name)?

  • 1
    one was hosts.txt – Elad Benda Dec 10 '14 at 14:55
  • 1
    That can happen if you create the file from scratch in TextEdit (or a similar app) and simply save it in the /etc/ folder and aren't extremely vigilant about the extension as well as the "hide extension" option. – samh Dec 11 '14 at 1:10
4

Usually only the root user is allowed to modify the file hosts.

To do that and fix your hosts file or your /etc folder (depending where you applied your read/write permissions) follow these steps:

  1. Throw away your newly created hosts file (but not the old one!)
  2. Repair your permissions with Disk Utility. enter image description here
  3. Open Terminal and enter sudo nano /etc/hosts and hit
  4. Enter your password (it will not be shown!) and hit
    The standard hosts file in Yosemite looks like this: nano
  5. Edit your hosts file.
    The format for each line is IP-address, then a tab or a single space and the hostname(s). Terminate the file with an empty trailing line.
  6. write the changes to disk with ctrlO followed by and exit nano with ctrlx
  7. Enter exit, hit and quit Terminal.app

As an alternative for the steps 3-7 you may download and install hosts.prefpane and enter your additional hosts there.

0

Just Change what you want to change on the duplicate, then replace the old hosts file.

0

The best way I have found to change hosts settings is to:

1) Make a duplicate (which you already have done)

2) Move the duplicate to a new location, such as the desktop

3) Make another duplicate on the desktop and rename it "hosts ORIGINAL" so you know it is the original hosts file, in case you need to revert back.

4) Make the changes to "hosts" on the desktop and save it.

5) Delete or rename the "hosts" file in /etc to "hosts OLD"

6) Drag "hosts" from the desktop to /etc. (you may be asked for admin credentials)

-1

I encountered this problem in Yosemite 10.10.3. I couldn't find the solution until I started to think outside the box.

Most people don’t know it, but as default the ”/private/etc” folder and ”/private/etc/hosts” file permissions are set to ”read only”. So if you only change the ”hosts” file permissions to ”read and write” you still won’t be able to edit it in TextEdit. The ”hosts” file is locked due to the ”/private/etc” folder. The trick is that you need to change both the permissions of ”/private/etc” folder then ”/private/etc/hosts” file.

sudo chmod 777 /private/etc;chmod 777 /private/etc/hosts;open -t /private/etc/hosts

I don't care if it is a bug or not!

  • You don't need write permissions to the containing folder to change a file's contents. That's not how the unix permission system works. – Jens Erat Jun 23 '15 at 20:05
  • @JensErat you don't need write permissions to edit a file in place. But some gui editors write to a temp file in the same folder, and if that works they delete the old file and rename new to old. THAT process requires write-to-parent folder. – paul Jun 24 '15 at 0:27

You must log in to answer this question.

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