Change hostname in CentOS 7 via command line.

share on:
change server hostname in centos 7

Setting a proper hostname on a server is the first thing you should do after installing a new server. Most web panels, like cpanel or Vestacp, requires you to have a Fully qualified domain name for your hostname. A fully qualified domain name is in the form of server1.example.com or panel.example.com.

Now to the important question. What is a hostname?

A hostname is a free form string up to the length of 64 characters. It should not begin or end with a dot. Also, can’t use two dots immediately following each other. You can choose any name for your hostname. But the best practice is to choose a fully qualified domain name as your hostname. for example server1.example.com or panel.example.com.

Hostname Types.

CentOS 7 supports three types of Host Names:

Static hostname – The static host name is assigned or chosen by the user and is stored in /etc/hostname file.

Transient or Dynamic hostname – The transient/dynamic hostname is maintained by the kernel and assigned by DHCP and mDNS server at run time.

Pretty hostname – It is a free form UTF -8 hostname for the presentation to the user. For example, “Server 1 of DebYum”.

Red Hat recommends that both static and transient names match the fully-qualified domain name (FQDN) used for the machine in DNS, such as host.example.com.

 

Change server hostname in CentOS 7.

If you want to change your server hostname to a new one then you should make sure that it is persistent. Means it can survive a reboot.

The process of setting up a new hostname in CentOS 7 is easy. There are many ways to change server hostname in CentOS 7. For example, you can use the command line or Text User Interface tools like NMTUI to change your hostname.

I prefer using the command line to manage hostnames on my server.

Here is a list of different ways to manage your hostname.

 

Method #1: Edit various files to manage your hostname.

Update the /etc/hostname file on your RHEL 7 or Centos 7 Linux instance with the new hostname.

[root@panel ~]# vi /etc/hostname
panel.debyum.com

 

Update the /etc/sysconfig/network file on RHEL 7 or CentOS 7 Linux instance.

[root@panel~]# vi /etc/sysconfig/network
# Created by anaconda
HOSTNAME=panel.debyum.com

 

Check the Hostname of your server.

[root@panel ~]# hostname
panel.debyum.com

 

 

Method #2: Use hostnamectl command to manage your hostname.

Set static hostname on CentOS 7.

root@panel:~# hostnamectl set-hostname --static Server1.debyum.com

 

The transient name is assigned by DHCP and mDNS server at run time.

 

Set pretty hostname on CentOS 7.

root@panel:~# hostnamectl set-hostname "Server 1 of Debyum"

 

Restart the systemd-hostnamed service.

root@panel:~# systemctl restart systemd-hostnamed

 

Reboot your server to make things permanent.

 

Check your hostname settings.

root@server1:~# hostnamectl status
Static hostname: Server1.debyum.com
Pretty hostname: Server 1 of Debyum
Transient hostname: Server1
Icon name: computer-vm
Chassis: vm
Machine ID: 153a217486fe4jsu4r9u228db67ed581
Boot ID: 7fbe3ecfff094w9e81fr55c6a1bf3fe6
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 4.8.6-x86_64-linode78
Architecture: x86-64

 

 

Method #3: Use the nmcli command to manage your hostname.

First of all, check the hostname using the nmcli command:

root@engy:~# nmcli general hostname
engy.debyum.com

 

Set a fully qualified domain name via nmcli command as root.

root@engy:~# nmcli general hostname panel.debyum.com

 

Check the new hostname.

root@engy:~# nmcli general hostname
panel.debyum.com

 

Set a short name as your hostname using the nmcli command. The command is same as setting any kind of hostname in nmcli. (short or FQDN)

root@engy:~# nmcli general hostname panel

 

Check the new hostname in CentOS 7.

root@engy:~# nmcli general hostname
panel

 

To force hostnamectl to notice the change in the static hostname, restart hostnamed as root:

root@engy:~# systemctl restart systemd-hostnamed

 

Thanks for visiting this Page and have a Great day 🙂 .

 

 

share on:
engy

engy

Hello there, My name is Rishi Guleria and I work as a Linux system administrator. I have created this blog to share what I have learned so far and to learn new things. Good Day. :)

Leave a Response

share on: