--- title: "How to Set the Hostname and FQDN on Ubuntu 24.04" description: "Set a persistent hostname and FQDN on Ubuntu 24.04 with hostnamectl and /etc/hosts, stop cloud-init from resetting it, and check DNS and reverse DNS records." category: "Server Setup" language: en source: https://cubepath.com/docs/server-setup/how-to-configure-hostname-and-fqdn-correctly updated: 2026-09-25 --- # How to Set the Hostname and FQDN on Ubuntu 24.04 The hostname identifies a server in shell prompts, logs and monitoring, and the fully qualified domain name (FQDN) is the full DNS name that other systems use to reach it, such as `web01.example.com`. Mail servers, Kerberos, some control panels and many clustering tools refuse to work, or misbehave, if the FQDN does not resolve correctly. In this tutorial you will set a persistent hostname and FQDN on Ubuntu 24.04, make sure cloud-init does not overwrite them, and verify forward and reverse DNS. ## Prerequisites To follow this tutorial, you will need: - A server running Ubuntu 24.04 LTS, for example a CubePath VPS. Differences for Rocky Linux 9 are noted where they matter. - A non-root user with `sudo` privileges. - A domain you control, if you want the FQDN to resolve publicly. This tutorial uses `web01` as the hostname and `example.com` as the domain. Replace them with your own, and replace `your_server_ip` with the server's public IP address. ## Step 1 - Checking the current configuration `hostnamectl` shows the hostname that systemd manages: ```bash hostnamectl ``` ```output Static hostname: ubuntu-2404 Icon name: computer-vm Chassis: vm Machine ID: 3f1c... Boot ID: 9a7e... Virtualization: kvm Operating System: Ubuntu 24.04.3 LTS Kernel: Linux 6.8.0-84-generic Architecture: x86-64 ``` The short hostname and the FQDN are reported by the `hostname` command: ```bash hostname hostname -f ``` ```output ubuntu-2404 ubuntu-2404 ``` `hostname -f` does not read a setting of its own. It takes the short hostname and resolves it through `/etc/hosts` and DNS. If it prints only the short name, as above, no FQDN is configured yet. Some terminology used in the rest of the guide: | Term | Example | Where it is stored | |---|---|---| | Static hostname | `web01` | `/etc/hostname` | | Pretty hostname | `Web server 01` | `/etc/machine-info` (free text, for display only) | | FQDN | `web01.example.com` | Resolved from `/etc/hosts` or DNS | ## Step 2 - Choosing a valid name A hostname label may contain letters, digits and hyphens, must not start or end with a hyphen, and can be at most 63 characters. The whole FQDN can be up to 253 characters. Use lowercase, and avoid underscores, since they are not valid in hostnames. Pick a name that describes the role and is unique within your infrastructure, for example `web01`, `db-primary` or `mail`. The FQDN is that name plus a domain you control: `web01.example.com`. Avoid using the bare domain (`example.com`) as a server FQDN, since it usually points to your website. ## Step 3 - Setting the static hostname Set the static hostname with `hostnamectl`. On Ubuntu and Debian the convention is to store the short name here and let `/etc/hosts` provide the FQDN: ```bash sudo hostnamectl set-hostname web01 ``` This updates `/etc/hostname` and the running kernel hostname immediately. Verify: ```bash cat /etc/hostname hostnamectl --static ``` ```output web01 web01 ``` Optionally set a descriptive pretty hostname, which some tools display: ```bash sudo hostnamectl set-hostname "Web server 01" --pretty ``` The shell prompt keeps showing the old name until you open a new session. Log out and back in, or start a new shell with `exec bash`. > Note: On Rocky Linux and other RHEL-based systems the convention is to store the FQDN as the static hostname: `sudo hostnamectl set-hostname web01.example.com`. Either way, `hostname -f` must return the FQDN once `/etc/hosts` is configured in the next step. ## Step 4 - Mapping the FQDN in /etc/hosts The resolver checks `/etc/hosts` before DNS, so an entry here makes the FQDN resolve even when DNS is not reachable. Open the file: ```bash sudo nano /etc/hosts ``` Find the line that references the old hostname (on Ubuntu it is usually `127.0.1.1 old-hostname`) and replace it. The first name after the address is the canonical name, so the FQDN must come before the short name: ```text 127.0.0.1 localhost your_server_ip web01.example.com web01 # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ``` Using the public IP address instead of `127.0.1.1` means services that look up their own hostname (a mail server announcing itself, for example) get an address other hosts can reach. If the IP might change, `127.0.1.1 web01.example.com web01` also works for `hostname -f`. Save the file and check the result: ```bash hostname hostname -f hostname -d ``` ```output web01 web01.example.com example.com ``` Confirm how the name resolves through the system resolver: ```bash getent hosts web01.example.com ``` ```output 203.0.113.10 web01.example.com web01 ``` ## Step 5 - Keeping cloud-init from resetting the hostname Most cloud images, including Ubuntu 24.04 VPS images, run cloud-init at boot. Depending on the provider's configuration, cloud-init can set the hostname and rewrite `/etc/hosts` from its metadata on every boot, undoing your changes. Check whether cloud-init is present: ```bash cloud-init status ``` ```output status: done ``` If the command exists, tell cloud-init to leave the hostname alone. Create a drop-in configuration file: ```bash sudo nano /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg ``` Add the following: ```yaml preserve_hostname: true manage_etc_hosts: false ``` `preserve_hostname: true` stops cloud-init from changing the hostname, and `manage_etc_hosts: false` stops it from regenerating `/etc/hosts`. If `/etc/hosts` starts with a comment saying it is managed by cloud-init and edits will be lost, this second setting is what makes your edit stick. Reboot to confirm the configuration persists: ```bash sudo reboot ``` After reconnecting, run `hostname -f` again. It should still print `web01.example.com`. ## Step 6 - Creating DNS records `/etc/hosts` only affects the server itself. For other systems to resolve the FQDN, create an **A record** (and an **AAAA record** if the server has IPv6) in the DNS zone of your domain: | Type | Name | Value | |---|---|---| | A | `web01` | `your_server_ip` | | AAAA | `web01` | `your_server_ipv6` | After the record propagates, check it from the server or your workstation. On Ubuntu, `dig` is part of the `bind9-dnsutils` package: ```bash sudo apt install bind9-dnsutils dig +short web01.example.com A ``` ```output 203.0.113.10 ``` ### Reverse DNS Reverse DNS (a PTR record) maps the IP address back to a name. It matters mostly for mail: many receiving servers reject or spam-flag mail from IPs whose PTR does not match the sending hostname. The PTR record is managed by whoever controls the IP address block, which for a VPS is the hosting provider, not your domain's DNS zone. Check the current value: ```bash dig +short -x your_server_ip ``` ```output web01.example.com. ``` If it returns a generic provider name, set the PTR to your FQDN through your provider. The forward (A) record for that name must point back to the same IP. ## Step 7 - Updating services that cache the hostname Most services read the hostname when they start. After renaming, restart anything that embeds it, or reboot. Common cases: - **Postfix**: `myhostname` in `/etc/postfix/main.cf` should match the FQDN. Check it with `postconf myhostname`, set it with `sudo postconf -e 'myhostname = web01.example.com'`, then `sudo systemctl restart postfix`. - **rsyslog and journald**: new log lines use the new name after a restart or reboot. Older lines keep the old one. - **Monitoring agents**: many register the host by hostname, so the server may appear as a new host in your dashboard. ## Troubleshooting - **`sudo: unable to resolve host web01: Name or service not known`**: the new hostname is not in `/etc/hosts`. Add it as shown in Step 4. - **`hostname -f` prints only the short name**: the FQDN is missing from `/etc/hosts`, or the short name is listed before it on the line. The FQDN must be the first name after the IP. - **The hostname reverts after reboot**: cloud-init is setting it. Add the drop-in from Step 5. - **`hostnamectl` fails with an invalid hostname error**: the name contains characters such as underscores or spaces. Use only letters, digits, hyphens and dots. ## Conclusion Your server now has a persistent hostname, an FQDN that resolves locally and in DNS, and a matching reverse DNS record. As next steps, you can configure a mail transfer agent that relies on the FQDN, issue a TLS certificate for `web01.example.com`, and set the server's timezone and locale for consistent logs.