2026年11月13日
2026年11月13日
How to Change a VPS Hostname: hostnamectl, /etc/hosts and cloud-init
This article is a localized edition of the Japanese original. Read the Japanese original
A VPS hostname is often the name the provider assigned at sign-up (on Sakura VPS, a Japanese VPS service, something like ik1-…vs.sakura.ne.jp), and you may want to change it to something that fits its role, such as web01. This article explains how to change the hostname on current Linux distributions that use systemd (Ubuntu 22.04 / 24.04, Debian 12, AlmaLinux and Rocky Linux 9, and so on).
Changing the hostname does not change your site's domain name, your WordPress URL or your DNS records. Only the name the server uses for itself changes.
Characters you can use in a hostname
- Use lowercase letters, digits and hyphens (
-). Do not start or end with a hyphen - Each label (each dot-separated part) can be up to 63 characters
- The systemd static hostname can be up to 64 characters
On Debian and Ubuntu, a common approach is to put a short name such as web01 in /etc/hostname and map the fully qualified domain name (FQDN) such as web01.example.com in /etc/hosts. The steps below follow that approach.
Step 1: Check the current hostname
hostnamectl
The Static hostname: line shows the current hostname.
Step 2: Change the hostname with hostnamectl
sudo hostnamectl set-hostname web01
This command rewrites /etc/hostname and applies the change immediately. No reboot is needed. You can also edit /etc/hostname directly in an editor, but then the change does not take effect until you reboot, so hostnamectl is less work.
Step 3: Update /etc/hosts to match the new name
If the old hostname is left in /etc/hosts, you may see a warning such as sudo: unable to resolve host web01: Name or service not known every time you run sudo, or hostname -f may fail to return the FQDN.
sudoedit /etc/hosts
Change the line with the old hostname to the new name. On Debian and Ubuntu, the standard is a line that assigns 127.0.1.1 to the machine's own hostname.
127.0.0.1 localhost
127.0.1.1 web01.example.com web01
# Do not change the IPv6 lines
::1 localhost ip6-localhost ip6-loopback
On a server with a fixed public IP address, you can write that address instead of 127.0.1.1. AlmaLinux and Rocky Linux have no 127.0.1.1 line, so add a line like this:
203.0.113.10 web01.example.com web01
Each line is written in the order "IP address, FQDN, short name".
Step 4: Keep it from reverting on reboot (cloud-init)
VPS and cloud images sometimes include cloud-init, which sets the hostname and /etc/hosts again at boot. In that case, the hostname goes back to the old one after a reboot.
Check whether cloud-init is in use.
ls /etc/cloud/cloud.cfg && cloud-init status
If the file does not exist, cloud-init is not installed and you can skip this step. If it is installed, add a setting that keeps the hostname.
sudoedit /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
preserve_hostname: true
Also, if the top of /etc/hosts has a comment saying it is managed by cloud-init, /etc/hosts is also regenerated at boot. Add the following line to the same file so cloud-init stops rewriting /etc/hosts.
manage_etc_hosts: false
Step 5: Verify the change
hostnamectl # is the Static hostname the new name?
hostname # short name
hostname -f # FQDN (is the /etc/hosts setting working?)
getent hosts web01 # can the name be resolved to an IP address?
The shell prompt (ubuntu@old-name:~$) shows the new name after you log in again.
On systems that use cloud-init, reboot once and confirm that the hostname does not revert.
sudo reboot
# after reconnecting
hostnamectl
What to review along with the hostname
| Item | What to check |
|---|---|
| Mail server (Postfix, etc.) | If myhostname is not set in /etc/postfix/main.cf, it is derived from the hostname. On Debian and Ubuntu the old hostname may have been written there at install time, so check it with postconf myhostname, and after fixing it run sudo systemctl reload postfix |
| Reverse DNS (PTR) | This is separate from the OS hostname. With providers that support it, change it in the VPS control panel. For a server that sends mail, matching forward and reverse DNS makes rejection by receiving servers less likely |
| Forward DNS | If you want to reach the server from outside as web01.example.com, add an A record in DNS. Changing the hostname alone does not make the name resolvable |
| Logs and monitoring | The hostname recorded in logs changes. If a monitoring service or log aggregation uses the hostname, update it as well |
SSH connections are not affected. Your local ~/.ssh/known_hosts records the name or IP address you connected with, and the server's host keys do not change, so as long as you connect to the same IP address you do not need to re-register the key.
Questions about WordPress? The publisher of this site is based in Japan and replies in Japanese. Contact (Japanese)