How to change the hostname of a Cloud Server
A Cloud Server's hostname is stored in two places: in the Control Panel and in the server's operating system. When you deploy a server, the operating system takes the hostname from the Control Panel the first time it starts. After that, the two are kept separately, so changing the hostname in the Control Panel doesn't change it on the server, even after a restart.
To change a server's hostname, you change it in the Control Panel and then on the server itself. This guide walks you through both on a Linux Cloud Server deployed from one of UpCloud's templates. The examples use web1.example.com as the current hostname and web2.example.com as the new one.
Change the hostname in the Control Panel
Changing the hostname in the Control Panel keeps the server's details up to date, so the Control Panel and the UpCloud API show the new name. You can change it while the server is running.
- In the Control Panel, go to Servers and select the server.
- On the Overview tab, click the pencil icon next to Hostname.
- Enter the new hostname, such as
web2.example.com, and click the checkmark icon to save it.
The server's metadata service returns the new hostname after you next restart the server from the Control Panel. The operating system, however, keeps its current hostname until you change it on the server, as described in the next section.
Change the hostname on the server
Connect to the server over SSH, as described in Connecting to your Cloud Server. Before you set the new hostname, check whether the server uses the short name or the full name with the following command:
hostnameIf the command prints the short name, such as web1, set the new short name with:
sudo hostnamectl set-hostname web2If it prints the full name, such as web1.example.com, set the new full name with:
sudo hostnamectl set-hostname web2.example.comOn some servers, sudo warns that it's unable to resolve the host:
sudo: unable to resolve host web1.example.com: No address associated with hostnameThe command still sets the new hostname. The warning appears because the hosts file has no line for the hostname yet, and it stops once you've updated the hosts file in the next step.
Update the hosts file
The server looks up its own name in the /etc/hosts file, so the file needs the new hostname as well. Open the file in a text editor:
sudo nano /etc/hostsOn servers that use cloud-init, the file starts with a comment saying that manage_etc_hosts is set and that changes to the file won't persist. On UpCloud's templates, cloud-init writes the file only when the server is first deployed, so your changes stay after a restart.
Replace the old hostname with the new one wherever it appears, both the full name and the short name. Depending on the operating system, the hostname is on one line or on two lines. With the new hostname, a single line looks like this:
127.0.1.1 web2.example.com web2Two lines look like this:
127.0.0.1 web2.example.com web2
::1 web2.example.com web2If no line has the old hostname, add the following line to the end of the file:
127.0.1.1 web2.example.com web2Save the file and close the editor.
Check the new hostname
The new hostname takes effect immediately, without a restart. To check it, run:
hostnameThe command prints the new hostname:
web2Then check that the server can look up its new name:
getent hosts web2The command prints an address from the hosts file, followed by the new hostname:
127.0.1.1 web2.example.com web2Your shell prompt shows the new hostname the next time you log in. The server keeps the new hostname when you restart it.