Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
hostnamectl set-hostname web1.example.com
hostnamectl
Static hostname: web1.example.com
hostname -f
web1.example.comLog out and in again to see the new name in your prompt.
Step by step
- 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 exampleweb1.example.com. - Set it — it is saved in
/etc/hostnameand works at once (no reboot):
hostnamectl set-hostname web1.example.com
hostnamectl set-hostname "Web server 1 (shop)" --prettyThe pretty name is only a description for people.
- Make the name resolve on the server (sudo, mail and some apps look it up). Add the server's own IP:
127.0.0.1 localhost localhost.localdomain
::1 localhost localhost.localdomain
192.0.2.10 web1.example.com web1Also create the DNS A record for the new name (and remove the old one later).
- Stop the old name from coming back:
preserve_hostname: truenmcli general hostname web1.example.comThe first stops cloud-init; the nmcli line is the NetworkManager way and keeps DHCP from setting a name.
- 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 SSHknown_hostsentries on other machines.
Check that it worked
hostnamectl --static
hostname -f
getent hosts "$(hostname -f)"
192.0.2.10 web1.example.com web1Reboot once (in a maintenance window) and check again — the name must stay.
Common problems
| Problem | Cause | Fix |
|---|---|---|
sudo: unable to resolve host web1 | The new name is not in /etc/hosts or DNS. | Add it to /etc/hosts (step 3). |
| The old name is back after reboot | cloud-init or DHCP sets it. | preserve_hostname: true; set it with nmcli general hostname. |
| The prompt still shows the old name | The shell read it at login. | Log out and in again. |
hostname -f shows only the short name | No FQDN in /etc/hosts / DNS. | Put the full name first in the /etc/hosts line. |
| Monitoring shows two servers | The old and the new name both report. | Remove the old target / instance in monitoring. |
Invalid hostname | Underscore or other bad characters. | Only a-z, 0-9, - and dots. |
Go further
- Command pages: hostnamectl, nmcli.
- Next how-tos: Set a static IP address, Set the time zone and sync the clock, Check the DNS records of a domain.
- Learn it: RHCSA — Hostname, RH124 — Hostname, RH124 — /etc/hosts.
- Many servers: cloud-init, Ansible.
- Practise: Lab: give server1 a fixed address, a name and a hosts entry.
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).