The hostname is the server's name on the network. It is used in the shell prompt, in logs, in mail headers and in PTR records. A correct hostname is an FQDN such as server.example.com.
Hostname Types in Linux
| Type | Example | Description |
|---|---|---|
| static | server.example.com | The main one, stored in /etc/hostname |
| transient | server | Temporary, set by the kernel |
| pretty | My Server | For the UI (may contain spaces) |
Changing the Hostname — the Modern Way (systemd)
# Show the current hostname
hostnamectl status
# Change the hostname
sudo hostnamectl set-hostname server.example.com
# Verify
hostname
hostname --fqdn
Updating /etc/hosts
After changing the hostname, be sure to update /etc/hosts:
sudo nano /etc/hosts
# Add or edit the line:
127.0.1.1 server.example.com server
# Also make sure the public IP is listed too:
YOUR.PUBLIC.IP server.example.com server
Changing It on Older Systems (without systemd)
# CentOS 6 / Debian 7 and older
sudo nano /etc/hostname
# Enter: server.example.com
# Apply without rebooting
sudo hostname server.example.com
Verifying After the Change
# Current hostname
hostname
# FQDN
hostname --fqdn
# Full status
hostnamectl status
# The new prompt appears after:
exec bash # or open a new SSH session
mail.yourdomain.com.Hostname and SSL Certificates
When issuing a certificate, Let's Encrypt checks that the domain points to your IP. If the server hostname is server.example.com, the domain server.example.com must have an A record pointing to that server's IP.
localhost or server without a domain will cause problems with Postfix, SSL generation and some control panels. Always specify an FQDN.Diagnosing common errors
After changing the hostname, "sudo" starts complaining "unable to resolve host" — sudo tries to resolve the current hostname via DNS or /etc/hosts on startup, and if the new name is not listed there, it throws an error.
sudo: unable to resolve host server.example.com: Name or service not known
The command still runs, but with a delay and a warning on every invocation. The fix is adding a line to /etc/hosts with the new hostname as described above — this error almost always means that step was skipped.
Postfix stops accepting mail with "mail for example.com loops back to myself" — this happens when myhostname in the Postfix config matches the domain the server accepts mail for, but not the MX record.
postfix/smtpd[1234]: warning: mail for example.com loops back to myself
A mail server's hostname must be a subdomain, not a bare domain: mail.example.com, not example.com. After changing the hostname, always check postconf myhostname and restart Postfix.
Ways to set the hostname compared
| Method | Persistence | Systems | Needs a reboot |
|---|---|---|---|
| hostnamectl set-hostname | Permanent, until changed again | systemd (Ubuntu, Debian, CentOS 7+) | No |
| Manually editing /etc/hostname | Permanent | Any Linux, including without systemd | Yes, or the hostname command |
| hostname (without sudo hostnamectl) | Temporary, until reboot | Any Linux | No, but it resets |
| DHCP hostname option | Depends on the DHCP server | VPS with automatic network setup | Can be overwritten |
When changing the hostname by hand is a bad idea
- The server already receives mail and the old hostname is set in the provider's PTR record — change the PTR to the new name first, or mail will start bouncing right after the switch.
- The server has Let's Encrypt SSL certificates issued for the old name — after a hostname change the certificates do not reissue automatically, so run certbot again for the new domain.
- Clustered setups are in use (Galera, Keepalived) — hostname is often tied into cluster configuration there, and an arbitrary change can break communication between nodes.
Verifying the result
- Confirm the new name took effect:
hostnamectl statusmust show the correct Static hostname with no errors. - Check consistency with /etc/hosts:
getent hosts $(hostname)must return the correct IP, not an error. - Check the PTR record if the server is used for mail:
dig -x YOUR_IP +shortmust return the new hostname. - Restart dependent services (Postfix, control panels) and check the logs for errors tied to the old name:
journalctl -u postfix --since "5 min ago".
For setting up the mail server itself after a hostname change, see the article on Postfix, and for issuing a certificate for the new domain see the guide to SSL with Certbot.
Frequently asked questions
Do you have to reboot the server after changing the hostname?
No reboot is required — the hostnamectl set-hostname command applies the change immediately, without restarting the system. Open terminal sessions and already-running services may keep using the old name until they are restarted, so it is worth restarting hostname-dependent services such as Postfix and opening a new SSH session to confirm the change took effect.
Is the hostname the same thing as a website's domain name?
A hostname and a website's domain name are different things, though they often look alike. The hostname identifies the server on the network and is used by system services, while the domain points to an IP address through DNS and can belong to a different server or change independently of the hostname. Changing one does not rename the other.
Why doesn't the terminal prompt show the new name right away?
The shell prompt reads the hostname only once, when the shell starts, so an already-open session will not refresh itself after running hostnamectl. The new name appears only in a fresh session or after running exec bash, which restarts the current shell with the already-updated environment variables without requiring a new SSH login.
Are uppercase letters, underscores, or a leading digit allowed in a hostname?
Uppercase letters and digits are fine in a hostname, but an underscore or a leading digit can cause problems with some mail servers and older DNS resolvers, though the RFCs do not always forbid it outright. It is safer to stick to lowercase Latin letters, digits, and a hyphen, and to avoid starting the name with a hyphen or digit.