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 being your actual hostname) to be another way of having a local network loop to your machine. That combination of
.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
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
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
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:
If there is anything else in there that DOES NOT match that line, you need to edit it, comment it or remove it.