7

I want to add this entry:

127.0.0.1       api.localhost

To test the api of my web app.

However, the hosts file says:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##

Is it safe to add the entry I want to add? Can things go wrong?

JdeBP
  • 3,970
  • 17
  • 17
  • Fedora maps the hostname given at install time to `127.0.0.1` under certain circumstances. If I'm not mistaking it happens when you have an Ethernet card, but no network connectivity, e.g. DHCP is not working. – Cristian Ciupitu Feb 06 '11 at 15:47

3 Answers3

12

The "do not change" warning refers to changing the name "localhost". Many software packages expect "localhost" to resolve to 127.0.0.1.

Adding more entries is not a problem. You can add the entries after the host (as shown by Iain), or add additional lines. For example:

127.0.0.1   localhost
127.0.0.1   api.localhost
127.0.0.1   testsite2.localhost

On many UNIX-like systems, you can also use 127.0.0.2 and so forth because the whole 127.*.*.* range is routed to the "local loopback device / driver".

vdboor
  • 3,630
  • 3
  • 30
  • 32
  • 1
    Quoting from pRFC 5735 - "Special Use IPv4 Addresses"](http://tools.ietf.org/html/rfc5735): *127.0.0.0/8 - This block is assigned for use as the Internet host loopback addres*s. So any TCP/IP compliant system including Windows, Linux, MacOSX etc. should route 127.x.y.z addresses to the loopback. – Cristian Ciupitu Feb 07 '11 at 01:02
6

If your hosts file already contains a line for 127.0.0.1 then all you have to do is add your api.localhost at the end of it e.g.

127.0.0.1               localhost api.localhost
user9517
  • 114,104
  • 20
  • 206
  • 289
4

Yup, this should be safe on any reasonable system. In fact, it's a common ad-blocking technique - for example, you can download premade hosts files filled with the names of common ad servers all pointed at 127.0.0.1.

db2
  • 2,170
  • 2
  • 15
  • 19