fev 072019

Como posso mudar o hostname no Ubuntu ou Ubuntu Server usando a linha de comando?

Neste tutorial vamos mostrar um exemplo para modificar o nome do host (hostname) do seu ubuntu:

Como ver o hostname atual do seu Ubuntu:


Alterando o hostname temporariamente:

hostname meu-novo-hostname

Alterando o hostname permanentemente:

1ª Opção:

hostnamectl set-hostname meu-novo-hostname

Verificando a alteração do hostname usando o comando:

hostnamectl status

2ª Opção:
Também e possível alterando o hostname no arquivo /etc/hostname.

First, let’s configure the hostname. If you issue the command hostname, you can see the hostname of your server (Figure A).

Figure A

Figure A

My Ubuntu Server 18.04 hostname.

To change that hostname, open and edit the necessary file with the command sudo nano /etc/hostname. Change the hostname to meet your needs and then save/close the file.

Next open the hosts file with the command sudo nano /etc/host. Where you see your old hostname (i.e. localhost), change it to the new name. Save and close that file. Now, reboot your machine.

That was a test.

When the machine reboots, you should see your hostname has reverted to the original. Why is that? Because there’s a little setting, called preserve_hostname, tucked away in a particular configuration file, that dictates to the platform to either preserve or not preserve the hostname as set in /etc/hostname. To make that change, go back and alter your hostname (in both /etc/hostname and /etc/hosts) and then issue the command:

sudo nano /etc/cloud/cloud.cfg

In that file, look for the entry:

preserve_hostname: no

Change that entry to:

preserve_hostname: yes

Save and close that file. Now your newly configured hostname will remain, even after a reboot.

A small but frustrating change

Until you know about the preserve_hostname option, this change to the configuration of the Ubuntu Server hostname will certainly be one of frustration. I would suggest to Canonical, whenever such a change is made, it might behoove them to give some sort of indication in the standard configuration file. For example, in /etc/hostname it might have been smart to add this line:

# In order to preserve an altered hostname, set preserve_hostname to yes in
# /etc/cloud/cloud.cfg

Once the next LTS release comes out, that comment could be removed. It’d be a simple direction that would go a long way to prevent admins from growing frustrated with the implemented change.

Sorry, the comment form is closed at this time.