In Ubuntu my /etc/hosts file is empty. It happened suddenly, i did not modify it and don't think any other program also modified it. How can this happen ?

Now moreover I am not able to edit the /etc/hosts file from vi. I tried changing permission using sudo but that did not work as sudo was giving me 'unable to resolve host ghost' error. ghost is my host name. One more thing I have noticed while installing Ubuntu I set computer name as 'GHOST' which was taken as the hostname. Now if I change it to anything like 'ABC' the hostname it takes is 'abc' (all in lowercase) also if the device name is like abc.xyz.com then the hostname it takes is abc-xyz-com, replaces all . with - . Why this is happening.

Moreover how should I recover my /etc/hosts file as I am not able to edit the file at all.

  • Did you try hostnamectl set-hostname NEWNAME?
    – Byte Commander
    Jul 16 '17 at 15:16
  • this only changes the hostname right ? how does it affect /etc/hosts ?
    – saurav
    Jul 16 '17 at 15:23

To fix the /etc/hosts file...

  • boot to the GRUB menu
  • choose Advanced Options
  • choose Recovery mode
  • choose Root access
  • at the # prompt, type the following...
    • mount -o rw,remount / # remount the disk r/w
    • vi /etc/hosts # use any editor you like
    • minimum hosts file should look something like...

/etc/hosts...   abc.local   abc  localhost   abc

Reboot the computer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.