The cart is empty

In this article, we'll discuss how to change the hostname on the CentOS 7 operating system. A hostname is a unique identifier assigned to your computer within a network. There might be various reasons to change the hostname, such as altering server roles or configuring services that require specific hostnames. We'll explore three straightforward methods to achieve this.

1. Using the Command Line

The easiest and quickest way to change the hostname is by utilizing the hostnamectl command. This command allows you to change the hostname without restarting the system.

  1. Open a terminal.

  2. Enter the following command, replacing new_hostname with the name you wish to set as the new hostname:

    sudo hostnamectl set-hostname new_hostname
    
  3. Verify the change using the command:

    hostnamectl
    

 

This command displays current system information, including the new hostname.

2. Editing the /etc/hostname File

You can also change the hostname by editing the /etc/hostname file, which contains the current hostname of the system.

  1. Open the /etc/hostname file in a text editor such as nano or vi:

    sudo nano /etc/hostname
    
  2. Modify the contents of the file to the new hostname.

  3. Save and close the file.

  4. To apply the change, restart the systemd-hostnamed service using the command:

    sudo systemctl restart systemd-hostnamed
    

 

3. Editing the /etc/hosts File

For proper functionality of certain network services, ensure that the new hostname is correctly mapped in the /etc/hosts file.

  1. Open the /etc/hosts file in a text editor:

    sudo nano /etc/hosts
    
  2. Add or modify the line with the new hostname. You should have a line similar to this, where 127.0.1.1 is the IP address and new_hostname is your new hostname:

    127.0.1.1    new_hostname
    
  1. Save and close the file.

Changing the hostname on CentOS 7 can be easy and quick. Whether you choose any of the methods above, always ensure to test the changes and verify that the new hostname functions correctly within your network.