4 Answers
4

localhost is a "special name" which points to the IP address 127.0.0.1 which is supposed to exist on any computer capable of using the Internet, and refers to the machine itself. (Any IP address starting with 127.x.x.x refers to the local machine).

localhost is not the same as localhost.mydomain.com.

The file /etc/hostname appears what your computer thinks its name is. In Debian it appears to link 127.0.1.1 to this name in the /etc/hosts file. [ You could probably set this to a "valid" IP address if you have a statically assigned one ]

Ideally you should not use "localhost" as your hostname as it will (in theory, can't think of any examples as I have not done it) cause issues with some programs.

Ok, but why does doing hostname -f returns localhost? Will that be fine?
–
BaboonNov 9 '13 at 4:42

What is he content of the "/etc/hostname" file. I have forgotten most of what I learnt about it, but having a hostname of "localhost" on an Internet connected device is, from memory, not best practice.
–
davidgoNov 9 '13 at 7:33

The impact is that you either enter localhost into your DNS zone file as the IP address of this machine, or you enter it as 127.0.0.1. Whichever you choose, some things will not work correctly.

If you enter it as 127.0.0.1 (this is the recommended thing to do), then you'll not be able to reach this localhost-named machine from any other machines. So it would be practical to use a different name...

If you enter it as the IP address of this machine, then you'll have a disagreement on other machines between the results of the name resolution done through files (/etc/hosts), and the DNS service, because every other machine will have a localhost line in the /etc/hosts file with the address 127.0.0.1, and the DNS service will contain a differing address.

Seems like someone set your hostname to localhost which seems confusing. I have added & edited an answer to help you unravel that mess. It’s not a difficult problem to solve once you understand the basics.
–
JakeGouldNov 9 '13 at 5:22

localhost is a loopback to your local machine. You do not have to go out of your way to name it localhost as it should be localhost out of the box. You install Linux—or any OS—on any machine & without you doing anything, there will always be a localhost in your network config.

I mean, the Mac Mini I am typing on now responds to localhost. Which means it’s my machine & my local machine. If you were on your local machine, localhost would be your local machine.

hostname is simply the nickname for you machine that can or cannot be used a s reference for other services. In many cases, you can substitute hostname.local (with hostname being your actual hostname) to be another way of having a local network loop to your machine. That combination of hostname and .local is considered a fully qualified domain name (FQDN).

For example, I play around with Apache configs a lot, and there is a benefit for me to setup separate configs for localhost or hostname.local if I am using NameVirtualHost in Apache.

Now if you want to expand past your .local that falls into the realm of getting a real domain connected to the outside world setup. Which is a whole other discussion.

EDIT: Based on your comments below, it seems like the machine in question has localhost as a hostname. Meaning you can technically reach localhost.local which is not 100% wrong, but is highly confusing. To change the hostname to something new, do the following. I will presume the new host should be called coolnewhost for example’s sake:

This will temporarily change your hostname to coolnewhost:

sudo hostname coolnewhost

To change it permanently, edit the file (I like to use nano but use what you like):

sudo nano /etc/hostname

And then edit the hostname in that file to read coolnewhost.

Another option is to use sysctl to change the hostname in the kernel.

sudo sysctl kernel.hostname= coolnewhost

And you should be relatively good. But I would also check your /etc/hosts file to see of the old localhost is setup oddly:

sudo nano /etc/hosts

There should only be one line in there that reads:

127.0.0.1 localhost

If there is anything else in there that DOES NOT match that line, you need to edit it, comment it or remove it.

"localhost" is the "name" for the loopback interface, which always has the reservered IP 127.0.0.1, and typically the device name "lo" or "lo0"

Ok, seems I get it now. But why does hostname -f gives me localhost then?

if you do not give your host a name, the hostname defaults to localhost, as nameless systems are not thought of as "network/internet ready"

if you do give your host a name, the hostname should resolve to the IP address of the host. depending of which flavor of linux you run, there are diff conf files for setting this. to see this in action, from a named machine (ie. a box host whose hostname isnt "localhost"), run this:

ping localhost
ping $hostname

you should see two different ips come back, 127.0.0.1, and the ip of your machine... if not check conf files, hup interface/network scripts, or just reboot after making changes

So my FQDN if localhost is my hostname would be localhost.mydomain.com, right?

no, localhost is not used for connecting outside of the machine, so there is no real "fqdn" for localhost (although ive seen installs default to localhost.localdomain, but thats not real on the internet, so therefore not "fully qualified" imo).

"localhost", or lo, is a pseudo device that only exists and is relevant on a local machine. the reason for its being is to traverse the stack locally.... for instance, python code that connects to a database running on "localhost"