What is a hostname?

A hostname is a name that is assigned to a device connected to a computer network. The hostname is used to identify that device and for devices connected to Internet it is composed from a prefix + a period + a domain name (ex. centos8.mxforge.com ).

When installing a new VPS or dedicated server a hostname will be asked during the setup process. While the hostname might not be important for test servers, on production servers you should consider choosing a reasonable name as it will be used on your emails headers so can be easily viewed by users outside your organization.

Fortunately, the hostname can be changed later so don’t worry if you haven’t chose the best hostname from the begining.

This article will show you how to change the server hostname on CentOS 7 and 8, in just a few simple steps.

 

 

What is the current hostname?

Check the existing hostname by using the hostname command:

$ hostname
oldserver.mxforge.com

As an alternative, theĀ  hostnamectl command can be used. The hostname will be returned, as well as a short list of other information about your operating system.

$ hostnamectl
Static hostname: oldserver.mxforge.com
Icon name: computer-vm
Chassis: vm
Machine ID: xxxxxxxxxxxxxxxxxxxxxxxxxx
Boot ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Virtualization: oracle
Operating System: CentOS Linux 8 (Core)
CPE OS Name: cpe:/o:centos:centos:8
Kernel: Linux 4.18.0-193.14.2.el8_2.x86_64
Architecture: x86-64

The hostnamectl command it is a more simple way of working with the hostname as it allows avoiding direct editing of the CentOS files like /etc/hostname .

hostname -h
Usage: hostname [-b] {hostname|-F file} set host name (from file)
hostname [-a|-A|-d|-f|-i|-I|-s|-y] display formatted name
hostname display host name

{yp,nis,}domainname {nisdomain|-F file} set NIS domain name (from file)
{yp,nis,}domainname display NIS domain name

dnsdomainname display dns domain name

hostname -V|--version|-h|--help print info and exit

Program name:
{yp,nis,}domainname=hostname -y
dnsdomainname=hostname -d

Program options:
-a, --alias alias names
-A, --all-fqdns all long host names (FQDNs)
-b, --boot set default hostname if none available
-d, --domain DNS domain name
-f, --fqdn, --long long host name (FQDN)
-F, --file read host name or NIS domain name from given file
-i, --ip-address addresses for the host name
-I, --all-ip-addresses all addresses for the host
-s, --short short host name
-y, --yp, --nis NIS/YP domain name

Description:
This command can get or set the host name or the NIS domain name. You can
also get the DNS domain or the FQDN (fully qualified domain name).
Unless you are using bind or NIS for host lookups you can change the
FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
part of the FQDN) in the /etc/hosts file.

How do I change the hostname?

Changing the hostname can be done very easily by running:

$ sudo hostnamectl set-hostname newserver.mxforge.com

Test theĀ  new hostname with hostnamectl and hostname.

$ hostnamectl
Static hostname: newserver.mxforge.com
Icon name: computer-vm
Chassis: vm
Machine ID: 4a92996cd47145f1ba4b8ad1420bad64
Boot ID: 4515953cc80846f4a9078a7916db3819
Virtualization: kvm
Operating System: CentOS Linux 8 (Core)
CPE OS Name: cpe:/o:centos:centos:8
Kernel: Linux 4.18.0-147.8.1.el8_1.x86_64
Architecture: x86-64

$ hostname
newserver.mxforge.com

 

It is important to keep in mind that if you are running a server, some services might need manual reconfiguration so they can work properly with the new hostname.