How to Change the Hostname in CentOS

By | 2019-02-03

How the hostname is set is operating system specific. This is because the hostname is typically set by the startup process, and different operating systems have different startup processes. CentOS is no different. CentOS 7 uses systemd, while CentOS 6 uses upstart. This means how to change the hostname in CentOS depends on which major release you are using.

Regardless of the release you are using, the respective commands must be executed as the root user.

CentOS 7

On CentOS 7, use the program hostnamectl, which is part of systemd. With no arguments, hostnamectl prints some information about your machine to your terminal, including your current hostname.

[root@rename-me ~]# hostnamectl 
   Static hostname: rename-me.tylersguides.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 70509857d128490aa93a63de8f676c84
           Boot ID: d1a80e4fbebe4198a9eccf5cb42d2c6e
    Virtualization: oracle
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-957.1.3.el7.x86_64
      Architecture: x86-64
[root@rename-me ~]# 

To set a new hostname, use hostnamectl with the set-hostname sub-command.

[root@rename-me ~]# hostnamectl set-hostname centos7.tylersguides.com
[root@rename-me ~]# hostnamectl 
   Static hostname: centos7.tylersguides.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 70509857d128490aa93a63de8f676c84
           Boot ID: d1a80e4fbebe4198a9eccf5cb42d2c6e
    Virtualization: oracle
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-957.1.3.el7.x86_64
      Architecture: x86-64
[root@rename-me ~]# 

You may need to restart processes for them to see the new name. I recommend rebooting so you don’t miss anything.

CentOS 6

On CentOS 6, you have to edit /etc/sysconfig/network. If you have a minimal installation, you will probably be using vim. If you have never used vim, I wrote a short tutorial.

Replace the value for HOSTNAME in /etc/sysconfig/network:

[root@localhost ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=rename-me.tylersguides.com

[root@localhost ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=centos6.tylersguides.com

The host will be named centos6.tylersguides.com the next time you reboot. If you can’t or don’t want to reboot, run the following command.

[root@localhost ~]# hostname centos6.tylersguides.com

Keep in mind that you may need to restart processes for them to use the new name.

Name Resolution

If your machine is just for personal use, you can probably skip this. Don’t forget to do this if you are running a network server of some sort.

Make sure you update all name resolution services such as DNS, LDAP, NIS, or hosts files. Otherwise, when you try to use the new name, it won’t resolve and you will get errors similar to this:

[root@centos6 ~]# ping centos6.tylersguides.com
ping: unknown host centos6.tylersguides.com

References