3

This question already has an answer here:

I have googled and searched Ask Ubuntu on this, but got no relevant answers. I have no hosts file in /etc, I cannot find a hosts file in search, I am talking about the system hosts file not lampp/etc. I mention that because there is no hosts file there either - maybe that is a clue.

I want to know how to create a hosts file in Ubuntu or obtain a sample file I could copy and paste or I am happy to use the terminal if someone can help with best way to create the file and edit it.

I am not even sure what extension it should have - basically I just want the default hosts file that I understand Ubuntu should come with in my /etc folder.

marked as duplicate by muru command-line Feb 18 '18 at 11:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • The default contents are created by the netbase package. See /var/lib/dpkg/info/netbase.postinst. – muru Feb 18 '18 at 11:37
5

Lines in the /etc/hosts file have the following format:

<IP> <HOSTNAME>.<DOMAIN> <ALIAS>

You can create and/or edit the file with your preferred editor:

sudo nano /etc/hosts     # with nano in a terminal
gksudo gedit /etc/hosts  # with gedit
kdesudo kate /etc/hosts  # with kate

Here are some useful lines for the file – lines beginning with # are comments and get ignored, empty lines are allowed:

# The following line is desirable for IPv4 capable hosts
127.0.0.1       localhost

# The following lines are desirable for IPv6 capable hosts
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

You can read more about the format and get examples on the manpage (How can I get help on terminal commands?):

man hosts

There's also a Howto on ubuntuforums.org.

Further reading:

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