Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 71System · Linux

How to change the hostname

A clone of a VM still called localhost.localdomain, a server that was renamed in the inventory, a cloud image with a random name — the hostname shows up in logs, monitoring, prompts, certificates and mail headers, so it should be right. This how-to sets it with hostnamectl, makes the name resolve locally, keeps cloud-init and DHCP from changing it back, and lists what else to check after a rename.

  • 5 Lessen
  • 4 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

71.1

The quick answer

hostnamectl set-hostname web1.example.com
hostnamectl
 Static hostname: web1.example.com
hostname -f
web1.example.com

Log out and in again to see the new name in your prompt.

71.2

Step by step

  1. Choose a good name: lowercase letters, digits and - only (no _, no spaces), the short name unique in your network, the full name (FQDN) in your DNS domain — for example web1.example.com.
  2. Set it — it is saved in /etc/hostname and works at once (no reboot):
hostnamectl set-hostname web1.example.com
hostnamectl set-hostname "Web server 1 (shop)" --pretty

The pretty name is only a description for people.

  1. Make the name resolve on the server (sudo, mail and some apps look it up). Add the server's own IP:
/etc/hosts
127.0.0.1   localhost localhost.localdomain
::1         localhost localhost.localdomain
192.0.2.10  web1.example.com web1

Also create the DNS A record for the new name (and remove the old one later).

  1. Stop the old name from coming back:
/etc/cloud/cloud.cfg.d/99-keep-hostname.cfg
preserve_hostname: true
nmcli general hostname web1.example.com

The first stops cloud-init; the nmcli line is the NetworkManager way and keeps DHCP from setting a name.

  1. After a rename, check what used the old name: monitoring and inventory (Prometheus targets, Ansible inventory), log shipping labels, Postfix myhostname, TLS certificates with the old name, and SSH known_hosts entries on other machines.

71.3

Check that it worked

hostnamectl --static
hostname -f
getent hosts "$(hostname -f)"
192.0.2.10      web1.example.com web1

Reboot once (in a maintenance window) and check again — the name must stay.

71.4

Common problems

ProblemCauseFix
sudo: unable to resolve host web1The new name is not in /etc/hosts or DNS.Add it to /etc/hosts (step 3).
The old name is back after rebootcloud-init or DHCP sets it.preserve_hostname: true; set it with nmcli general hostname.
The prompt still shows the old nameThe shell read it at login.Log out and in again.
hostname -f shows only the short nameNo FQDN in /etc/hosts / DNS.Put the full name first in the /etc/hosts line.
Monitoring shows two serversThe old and the new name both report.Remove the old target / instance in monitoring.
Invalid hostnameUnderscore or other bad characters.Only a-z, 0-9, - and dots.

71.5

Go further

1Which command changes the hostname without a reboot and keeps it after a reboot?
2After a rename, sudo says "unable to resolve host". Why?

The new name can not be resolved; add it to /etc/hosts (or DNS).

Alle commando's in dit hoofdstuk