localhost as hostname confusion

4

2

I have a basic understanding about hostname and FQDN. Now I am confused, do I really have to specify a name for my hostname? So for example:

Hostname: somename
Domain: mydomain.com
FQDN: somename.mydomain.com

Now, I see something that the hostname is localhost. What is the difference and impact of that? So my FQDN if localhost is my hostname would be localhost.mydomain.com, right?

Baboon

Posted 2013-11-09T03:48:23.323

Reputation: 149

It depends on the software you have installed on your system. Most applications do not care about the hostname (and various alternatives). Some do like if you have a mail server or a multi tier application. (And not all agree if they want a domain in the uts nodename or not. – eckes – 2017-08-14T16:42:41.683

Answers

2

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.

davidgo

Posted 2013-11-09T03:48:23.323

Reputation: 49 152

Ok, but why does doing hostname -f returns localhost? Will that be fine? – Baboon – 2013-11-09T04:42:19.793

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. – davidgo – 2013-11-09T07:33:55.047

/etc/hostname content is jarvis. Like I said hostname -f returns "localhost", unlike hostname -f returns "jarvis" – Baboon – 2013-11-10T10:01:03.543

1

"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"

nandoP

Posted 2013-11-09T03:48:23.323

Reputation: 111

0

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.

Laszlo Valko

Posted 2013-11-09T03:48:23.323

Reputation: 570

Ok, seems I get it now. But why does hostname -f gives me localhost then? – Baboon – 2013-11-09T04:51:14.477

2Maybe because your hostname was set to localhost... – Laszlo Valko – 2013-11-09T04:56:41.120

No, my hostname is not localhost. – Baboon – 2013-11-09T05:17:21.587

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. – JakeGould – 2013-11-09T05:22:07.177

0

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.

JakeGould

Posted 2013-11-09T03:48:23.323

Reputation: 38 217

But if I do hostname -f it returns localhost, if I do hostname only it returns jarvis (my set hostname). I'm so lost in here. – Baboon – 2013-11-10T09:57:41.323