Score:0

When change the hostname the content of the file /etc/hosts must be updated manually?

pg flag

I work with lubuntu 22.04. To set the hostname I would like to use the nmcli command as following:

nmcli general hostname "new-hostname"

The previous command set the new hostname in the file /etc/hostname. The problem is that the content of the file /etc/hosts remains:

# Standard host addresses
127.0.0.1  localhost
::1        localhost ip6-localhost ip6-loopback
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
# This host address
127.0.1.1  <OLD-HOSTNAME>

The last line of the file continues to store <OLD-HOSTNAME> as hostname.

If I execute the following ping command in a terminal I obtain:

> ping <OLD-HOSTNAME>
PING <OLD-HOSTNAME> (127.0.1.1) 56(84) bytes of data.
64 bytes from <OLD-HOSTNAME> (127.0.1.1): icmp_seq=1 ttl=64 time=0.062 ms
64 bytes from <OLD-HOSTNAME> (127.0.1.1): icmp_seq=2 ttl=64 time=0.093 ms
64 bytes from <OLD-HOSTNAME> (127.0.1.1): icmp_seq=3 ttl=64 time=0.092 ms
...

The question is: when I change the hostname I have to change manually the last line of the file /etc/hosts and set the <new-hostname>?

Score:2
cn flag
raj

Yes. These two files are independent, serve different purposes and you have to change /etc/hosts as well if you change the hostname.

frankfalse avatar
pg flag
Are there other operations necessary to have a correct change of hostname? Is it sufficient to change only these 2 files?
raj avatar
cn flag
raj
If your hostname is used also by other services/daemons, like for example web server, or mail server, you may need to change it in their configurations as well. If not, it should be sufficient.
frankfalse avatar
pg flag
Thank you very much. Now I have to find how to modify `/etc/hosts` by a non root user and without sudo command. I have found [this link](https://askubuntu.com/questions/895640/can-i-edit-hosts-without-sudo). When I will finish my test I will accept your answer. Any other suggested are appreciated.
raj avatar
cn flag
raj
`sudo` is actually the safest tool to do it. Only you should give the user in question permission to run only a particular command that edits the hosts file and nothing else.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.