Linux Hostname Rules and How to Change Them
Which characters a hostname may use, stricter rules that work everywhere, a naming scheme that scales, and how to change a Linux hostname so it stays changed after a reboot.

Every Linux server has a hostname. It shows up in your shell prompt, in log files, in monitoring dashboards and in the headers of the mail the server sends. Most of the time nobody thinks about it, until a bad one breaks something: sudo starts printing "unable to resolve host", a certificate request fails, or a script chokes on an uppercase letter.
This guide covers the rules a valid hostname has to follow, a stricter set of rules that works everywhere, and how to check and change the hostname on a Linux server without it changing back after the next reboot.
Hostname, FQDN and Domain: What Is the Difference?
Three names get mixed up all the time:
| Name | Example | What it is |
|---|---|---|
| Hostname | web-01 | The short name of this one machine. |
| Domain | example.com | The domain the machine belongs to. |
| FQDN | web-01.example.com | The fully qualified domain name: hostname plus domain. This is what DNS resolves. |
On most servers you set the short hostname as the system hostname and add the FQDN in /etc/hosts (more on that below). Keeping the two apart avoids a whole class of problems.
The Official Rules for a Valid Hostname
Hostnames follow the rules for DNS labels, set out in RFC 952 and RFC 1123. A valid hostname:
- uses only the letters
atoz, the digits0to9and the hyphen-; - does not start or end with a hyphen;
- is 1 to 63 characters long per label (the parts between dots), and at most 253 characters for a full name;
- is not case sensitive:
Web-01andweb-01are the same name.
Not allowed: spaces, underscores, accents, emoji and any other punctuation. An underscore may work in some places, but DNS records, certificates and many tools reject it, so treat it as invalid.
Safer Rules That Work Everywhere
A name can be technically valid and still cause trouble. Servers end up in many systems: cloud panels, monitoring tools, configuration management, backup software, Windows networks. These stricter rules keep a hostname working in all of them:
- Use lowercase only. Some tools compare names exactly, so
Web-01andweb-01can end up as two different machines. - Start with a letter. Names that start with a digit, or are all digits, are easy to confuse with IP addresses.
- Do not put a dot in the short hostname. A dot turns it into a domain name, and some software will then try to resolve part of it as a domain.
- Keep it short. Aim for 5 to 20 characters. If the server joins a Windows network, stay at 15 or fewer, the old NetBIOS limit.
- Never put secrets or personal data in it. Hostnames are visible in mail headers, logs and reverse DNS.
On NodexVPS the order form follows these rules for you: every server name starts with Node-, followed by 5 to 20 lowercase letters, digits or hyphens, and anything else is simply left out as you type.
Good and Bad Hostname Examples
| Hostname | Verdict | Why |
|---|---|---|
web-01 | Good | Short, lowercase, says what it does. |
db-prod-ams-02 | Good | Role, environment, location and number. |
Web_Server | Bad | Uppercase letters and an underscore. |
-backup | Bad | Starts with a hyphen. |
my server | Bad | Contains a space. |
2024 | Risky | All digits: looks like a number or part of an address. |
web.prod | Risky | The dot makes it look like a domain. |
A Naming Scheme That Scales
Names like gandalf or pizza are fun with two servers and confusing with twenty. A simple pattern tells you what a machine is at a glance:
role-environment-location-number
web-prod-fra-01
db-staging-ams-01
vpn-nyc-01
Pick a pattern, write it down, and use it for every new server. Numbers with a leading zero (01, 02) keep lists sorted correctly.
How to Check the Current Hostname
Any of these commands shows it:
hostname
hostnamectl
cat /etc/hostname
hostnamectl gives the most detail, including the static hostname (stored in /etc/hostname) and the operating system. To see the fully qualified name, run:
hostname -f
How to Change the Hostname on Linux
Step 1: Set the new name
On any distribution that uses systemd (Ubuntu, Debian, AlmaLinux, Rocky Linux, Fedora and most others):
sudo hostnamectl set-hostname web-01
This changes the name right away and writes it to /etc/hostname, so it survives a reboot. Use a valid name from the start: depending on the systemd version, hostnamectl either refuses an invalid name or quietly changes it into something you did not choose.
Step 2: Update /etc/hosts
The server must be able to resolve its own name. Open the file:
sudo nano /etc/hosts
Replace the old name with the new one. On Debian and Ubuntu the line usually looks like this:
127.0.1.1 web-01.example.com web-01
On AlmaLinux and Rocky Linux, add the names to the 127.0.0.1 line or give them their own line with the server's IP address. Put the FQDN first, then the short name.
Step 3: Stop cloud-init from changing it back
Most cloud and VPS images run cloud-init, which can set the hostname again on every boot. If your new name disappears after a restart, open /etc/cloud/cloud.cfg and set:
preserve_hostname: true
If the same file has manage_etc_hosts: true, cloud-init also rewrites /etc/hosts at boot. Either set it to false or make your change in the template it uses, /etc/cloud/templates/hosts.debian.tmpl (or the .redhat one).
Step 4: Check the result
hostnamectl
hostname -f
sudo true
The last command should run without any "unable to resolve host" warning. Open a new SSH session to see the new name in your prompt.
Common Hostname Problems
"sudo: unable to resolve host"
The hostname is missing from /etc/hosts. Add it as in Step 2 and the warning goes away. Commands still run, but each one waits a moment for the failed lookup first.
The hostname changes back after a reboot
cloud-init is setting it again. Set preserve_hostname: true as in Step 3.
Mail from the server lands in spam
Mail servers check that the name your server announces matches its reverse DNS (PTR record). Give the server an FQDN, point that name's A record at the server's IP address, and set the IP address's reverse DNS to the same name in your hosting control panel.
Quick Checklist
- Lowercase letters, digits and hyphens only.
- Starts with a letter, does not end with a hyphen, no dot in the short name.
- 5 to 20 characters, or 15 or fewer for Windows networks.
- Set with
hostnamectl set-hostname. - FQDN and short name in
/etc/hosts. preserve_hostname: trueon servers that run cloud-init.
A good hostname takes a minute to choose and saves hours of confusion later. Set it once, follow the same pattern on every server, and your logs, dashboards and terminals will all tell you exactly which machine you are looking at.