Every server has a hostname: the name it uses for itself. You see it in your terminal prompt, in log files and in the headers of emails the server sends. It is easy to confuse with a domain name or an IP address, but each does a different job. This guide explains the difference, shows how to see and set a hostname, and gives naming habits that save confusion later.
Before you start
- A server you can log in to over SSH (Linux) or Remote Desktop on port 3389 (Windows).
- Optional: a domain you control, for example
example.com, if you want a fully qualified hostname.
Hostname, domain name and IP address
- IP address: the number other machines use to reach your server, such as
203.0.113.10. It is what the network actually routes to. - Domain name: a name you register and control, such as
example.com. You use DNS records to point names under it at IP addresses. - Hostname: the name the server calls itself, such as
web1. It is set in the operating system, not in DNS.
When you combine a short hostname with a domain, you get a fully qualified domain name (FQDN), such as web1.example.com. That is the form mail servers and many apps expect.
A key point: setting a hostname on the server does not make it reachable by that name. For web1.example.com to work from the internet, you also need an A record in your domain's DNS pointing to the server's IP.
Why the hostname matters
- Recognising servers. With several servers open in different terminals, a clear prompt such as
root@web1stops you running a command on the wrong one. - Logs and monitoring. Logs, alerts and monitoring dashboards label data with the hostname.
- Email. A mail server introduces itself with its hostname. Receiving servers compare it with the reverse DNS (PTR) record for the IP. A matching name helps your mail get delivered.
- Some software needs a resolvable FQDN to install, such as control panels and mail servers.
Good naming habits
- Use lowercase letters, numbers and hyphens only. Do not start or end with a hyphen. Avoid underscores and spaces.
- Name by role, not by person or project mood:
web1,db1,mail,rdp-office. - Number them if you might have more than one:
web1,web2. - For a FQDN, use a domain you own and plan to keep:
web1.example.com. - Keep it short. Each label (the part between dots) can be up to 63 characters, but short names are easier to type and read.
See and set the hostname on Linux
To see the current hostname:
hostnamectlThe Static hostname line shows it. To set a new one:
sudo hostnamectl set-hostname web1.example.comThen edit /etc/hosts so the name resolves locally. Add or update a line such as:
127.0.1.1 web1.example.com web1On AlmaLinux and Rocky Linux, many people map the server's public IP instead, for example 203.0.113.10 web1.example.com web1. Either way, check with:
hostname -fIt should print web1.example.com.
See and set the hostname on Windows
In PowerShell:
hostnameTo rename the computer, run PowerShell as Administrator:
Rename-Computer -NewName "WEB1" -RestartWindows computer names are limited to 15 characters for compatibility with older network naming, so keep them short.
Warning: -Restart reboots the server straight away, which ends your Remote Desktop session. Save your work first, then reconnect to 203.0.113.10:3389 after a couple of minutes. Renaming can also affect software licensed to the computer name.
How the hostname relates to reverse DNS
For a server that sends email, aim for all three to match:
- The server's hostname is
mail.example.com. - An A record points
mail.example.comto203.0.113.10. - The PTR (reverse DNS) record for
203.0.113.10points tomail.example.com.
You manage the first in the OS, the second at your DNS provider, and the third through the owner of the IP address. For a server, ask support in a ticket.
Check it works
- Linux:
hostname -fshows the full name. - Windows:
hostnameshows the new name after the restart. - From your computer,
nslookup web1.example.comreturns your server's IP, if you created the A record.
Troubleshooting
sudoprints "unable to resolve host": the new hostname is missing from/etc/hosts. Add it as shown above.- The prompt still shows the old name: log out and back in. The prompt is set when the shell starts.
- The name does not work from the internet: the hostname is only local. Create an A record at your DNS provider.
- The hostname resets after a reboot: on some images a setup tool rewrites it. Set it with
hostnamectl, which writes it permanently, and check for a cloud-init setting.
Next steps
- Understand the reverse side: What is reverse DNS (rDNS) and why it matters.
- Set hostname and rDNS step by step: Hostname and reverse DNS on Linux.
- Point your domain at the server: Point a domain to your VPS: DNS basics.
