The hostname is used to identify the server when connected on a network and it is configured during the initial server setup. Changing the initial server hostname is a task that you might like to perform while you are using the server and in this tutorial, we will show you how to change the hostname on a Linux VPS running CentOS 7 or Ubuntu 16.04 as an operating system.
What is a hostname?
Hostname is a label used to identify the machine and will help you to easily distinguish the server from another one. The hostname should be a Fully Qualified Domain Name (FQDN) and should point to the server IP address so you can access the server by using it.
Check the current hostname
Changing the hostname is a fairly simple process and requires only a few minutes of your time. You will need to connect to your server via SSH in order to be able to make that change. If you do not know how to connect to your server via SSH, you can use our guide about this which is available here.
Once you log in you should determine the current hostname. If you are using a CentOS 7 VPS or an Ubuntu 16.04 VPS, you can use the hostnamectl
command.
# hostnamectl status
The status output should be very similar to the one below:
Static hostname: vps.server.com Icon name: computer-container Chassis: container Machine ID: * * * Boot ID: * * * Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 2.6.32-042stab120.3 Architecture: x86-64
The value static hostname is the value you should look at.
Change the current hostname
To change the hostname on your CentOS or Ubuntu machine you should run the following command:
# hostnamectl set-hostname virtual.server.com
Please note that you need to replace virtual.server.com
with the new hostname you like to use.
Now, when you check the current hostname status using the command hostnamectl status
you will see the new hostname virtual.server.com
instead of the old one vps.server.com
. That means you have successfully changed the hostname on your server.
More command options
For more command options you can add the --help
flag at the end.
# hostnamectl --help hostnamectl [OPTIONS...] COMMAND ... Query or change system hostname. -h --help Show this help --version Show package version --no-ask-password Do not prompt for password -H --host=[USER@]HOST Operate on remote host -M --machine=CONTAINER Operate on local container --transient Only set transient hostname --static Only set static hostname --pretty Only set pretty hostname Commands: status Show current hostname settings set-hostname NAME Set system hostname set-icon-name NAME Set icon name for host set-chassis NAME Set chassis type for host set-deployment NAME Set deployment environment for host set-location NAME Set location for host