How to Change Server Hostname
This guide explains how to change the hostname on your server.
The hostname is the system name of your server, for example:
ns1.server.com
Changing the server hostname does not automatically create DNS records or change reverse DNS. If you want the hostname to resolve correctly, make sure the required DNS records are configured. If you also need reverse DNS changed, update it from the Client Area.
Temporary Hostname Change
You can change the hostname temporarily with the following command:
hostname ns1.server.com
Replace ns1.server.com with the hostname you want to use.
The hostname command changes the hostname immediately, but this change may not remain after a reboot unless the system configuration files are also updated.
Debian / Ubuntu
On Debian and Ubuntu systems, edit the following file:
/etc/hostname
You can replace the hostname with this command:
echo "ns1.server.com" > /etc/hostname
Or edit the file manually:
vi /etc/hostname
The file should contain only your hostname, for example:
ns1.server.com
Next, update the hostname immediately:
hostname ns1.server.com
You should also check the /etc/hosts file:
vi /etc/hosts
If your old hostname is listed there, replace it with the new one.
Example:
127.0.1.1 ns1.server.com ns1
CentOS / Red Hat / AlmaLinux / Rocky Linux
On modern CentOS, Red Hat, AlmaLinux and Rocky Linux systems, the recommended method is to use hostnamectl:
hostnamectl set-hostname ns1.server.com
Replace ns1.server.com with your new hostname.
You can check the current hostname with:
hostnamectl
You should also check the /etc/hosts file and update the old hostname if it is listed there:
vi /etc/hosts
Example entry:
127.0.0.1 localhost 127.0.1.1 ns1.server.com ns1
On older CentOS or Red Hat systems, the hostname may also be stored in /etc/sysconfig/network. If this file exists and contains a HOSTNAME variable, update it there as well.
Example:
NETWORKING=yes HOSTNAME=ns1.server.com GATEWAY=213.186.XX.254
FreeBSD
On FreeBSD, you can change the hostname immediately with:
hostname ns1.server.com
To make the change permanent after reboot, edit:
/etc/rc.conf
Add or update the hostname line:
hostname="ns1.server.com"
Verify the Hostname
After changing the hostname, verify it with:
hostname
You can also use:
hostname -f
If everything is configured correctly, the command should return your new hostname.
If the new hostname is displayed correctly and remains the same after reboot, the hostname change has been completed successfully.