How to Configure Server Hostname and FQDN Correctly
Level: Beginner
Estimated time: ~15 minutes
Goal: Configure a persistent server hostname and establish a Fully Qualified Domain Name (FQDN) for proper system identification and local DNS resolution.
Introduction
A hostname is the label assigned to a device on a network, while a Fully Qualified Domain Name (FQDN) includes both the hostname and the domain name it belongs to (e.g., web-server-01.example.com). Correctly configuring these is critical for services like mail servers (Postfix, Exim), internal networking coordination, and identifying your servers in terminal prompts across a large fleet.
In this tutorial, you will define a persistent hostname using systemd tools and establish a local FQDN override.
Note on FQDN Types:
- Public FQDN: set via public DNS (e.g., Cloudflare, Route53). This is how the world finds your server.
- Local FQDN: how the server identifies itself and resolves its own name internally. These should ideally match, but the local configuration is what ensures system stability.
Prerequisites
Before you begin, ensure you have:
- Operating system: Ubuntu 20.04/22.04/24.04 LTS, Debian 11/12, or RHEL/CentOS/AlmaLinux 8/9.
- Access: a user with
sudoprivileges. - Required knowledge: basic use of the Linux terminal and text editors (like
nanoorvim).
Step 1: Verify the current configuration
First, check what your server currently thinks its name is.
Run the status command:
hostnamectl status
Expected output (or similar):
Static hostname: localhost.localdomain Icon name: computer-vm Chassis: vm Machine ID: abc123def456 Boot ID: 123abc456def Virtualization: kvm Operating System: Ubuntu 24.04 LTS Kernel: Linux 6.8.0-31-generic Architecture: x86-64
Next, verify if the FQDN is resolving properly:
hostname -f
If the system responds with an empty space, exactly localhost, or an error indicating the host cannot be resolved, your FQDN is not properly configured.
Step 2: Set the new Hostname for Linux server hostname configuration
For most distributions, hostnamectl is the correct way to set hostname on server instances and keep the change persistent. This part of the guide covers practical Linux hostname configuration for common deployments, including how administrators typically configure hostname on Ubuntu and configure hostname on Debian without editing /etc/hostname by hand.
Run the following command, replacing <YOUR_HOSTNAME> with the desired short name (e.g., web-01):
sudo hostnamectl set-hostname <YOUR_HOSTNAME>
While Linux technically allows periods in a hostname, it is recommended to use only lowercase letters, digits, and hyphens. Using periods in the short hostname (e.g., web.01) can cause confusion with the FQDN and is generally avoided.
This command updates the system immediately. You may notice that your current terminal prompt (usually user@old-hostname:~$) hasn’t changed right away. Log out and log back in, or open a new bash session by typing bash, to see your prompt catch up to the new name.
Step 3: Update /etc/hosts for FQDN resolution
This step ties the local Hostname to an FQDN and Domain Name so hostname resolution works predictably. In practice, this is the part of server hostname setup that defines the machine’s local network identity and completes the underlying Linux network identity configuration used by many services.
Mapping your FQDN to the 127.0.1.1 loopback address is the safest production pattern (especially on Debian/Ubuntu). This ensures the name resolves even if the network is down or if your public IP changes.
Open the hosts file:
sudo nano /etc/hosts
You will see existing content that looks similar to this:
127.0.0.1 localhost 127.0.1.1 old-hostname
Do not remove the 127.0.0.1 localhost line. Instead, update the 127.0.1.1 line (or add it) to include your FQDN followed by the short hostname.
Update the file using this pattern:
127.0.0.1 localhost 127.0.1.1 <YOUR_FQDN> <YOUR_HOSTNAME>
Example if your FQDN is web-01.example.com and hostname is web-01:
127.0.0.1 localhost 127.0.1.1 web-01.example.com web-01
Avoid using public IPs in /etc/hosts unless specifically required. If the IP address changes (common in cloud environments) and you forget to update this file, hostname resolution will break.
Save and exit the file (in nano, press Ctrl+O, Enter, then Ctrl+X).
Step 4: Verify the changes
Now ensure the system correctly registers the FQDN.
Check the active FQDN:
hostname -f
Expected output:
web-01.example.com
Check the active short hostname:
hostname -s
Expected output:
web-01
Finally, ping the FQDN to ensure it successfully resolves back to the IP address you specified in the hosts file:
ping -c 3 <YOUR_FQDN>
By completing these steps, the server can now correctly identify and resolve its own network name.
Cloud-init note for VPS hostname setup
On cloud VPS instances such as AWS, GCP, and DigitalOcean, cloud-init may overwrite hostname changes during reboot. If you change hostname Linux guests and the old value returns after restart, review /etc/cloud/cloud.cfg and set preserve_hostname: true so your VPS keeps the intended Hostname and related Domain Name settings.
Troubleshooting
If the Hostname does not persist after reboot, first confirm that hostnamectl status shows the expected static value and then check whether cloud-init is restoring an old setting. If hostname -f does not return the expected FQDN, review the 127.0.1.1 entry in /etc/hosts and verify that the short Hostname appears after the FQDN. If services still report the wrong name, confirm that the server Domain Name, local resolver order in /etc/nsswitch.conf, and any provider metadata all match the intended configuration.
Rollback
If you misconfigured the names and need to return the server to its default state:
Reset the short host name:
sudo hostnamectl set-hostname localhost
Restore the /etc/hosts file by removing your customized IP line. Ensure the local loopback looks like this:
sudo nano /etc/hosts
127.0.0.1 localhost 127.0.1.1 localhost
Conclusion
You have successfully defined a persistent hostname and mapped your local host networking to provide an accurate FQDN. Doing this correctly sets the necessary baseline layer of identity for your web services, mail configurations, and remote orchestration tools.
Document Version: 1.0
Last Updated: April 2026
Owner: Technical Documentation Team