5

There are several answers on how to change hostname of Ubuntu, and all of them propose to do "the same changes" to /etc/hosts. "The same" stands for "the same as in /etc/hostanme". But unfotunately, these files have completely different formats, and they can't be changed in "same" manner.

So, the question is: what to write in hosts to have hostname myname?

Currently my hosts contains:

127.0.0.1 localhost

and some IPv6 stuff which I don't understand.

So, should I replace localhost with myname, really?

terdon
  • 100,812
Dims
  • 1,803

1 Answers1

10

Yes, really. The /etc/hosts file is just a map that links IPs to hostnames. If you want to be able to do things like ssh myname and have that run ssh localhost, you need to add a line that identifies localhost (127.0.0.1) as myname:

127.0.0.1 localhost
127.0.0.1 myname

You could just replace localhost with myname, but then things like ssh localhost would fail and localhost is pretty standard. So just add another line as shown above.

terdon
  • 100,812
  • 1
    Use just one line: '127.0.0.1 localhost myname` – waltinator Jul 28 '17 at 14:37
  • I wouldn't use it in one line. Usually I use 127.0.0.1 localhost.localdomain localhost and 127.0.0.1 myserver.mydomain myserver to keep things a bit more structured – derHugo Jul 28 '17 at 17:18
  • The wording is a bit fuzzy on if having two different lines for the same IP is valid but I would believe it isn't "...one line per IP address" hosts(5) – avlec Oct 27 '22 at 21:20