To modify the hostname on CentOS

Method One:

 

1.Open a terminal or SSH into the Linux system.
2.Log in to the system with root privileges.
3.Use the following command to check the current hostname:
  • hostname 

 

 

4.Use the following command to change the hostname:

  • hostnamectl set-hostname <new_hostname> 

Replace new-hostname with the desired hostname you want to set.

 

 

5.Reboot the system for the changes to take effect, or you can run the following command to apply the new hostname without rebooting:

  • systemctl restart systemd-hostnamed

 

 

6.To verify if the hostname has been successfully changed, use the following command:

  • hostname 

 

 

If the output matches the new hostname you set, it indicates that the hostname has been successfully changed. After rebooting the system, verify that the modification has taken effect.

 

 

 

Method 2:

 

1.Open the "/etc/hostname" file using the following command:

  • vi /etc/hostname 

 

 

 

2.To change the hostname to your desired name in the opened file

To start editing, press "a" and delete the previous hostname. Set your new hostname. After making the changes, press "Esc" to exit edit mode. Then, press Shift+":" and type "wq" to save and exit.

 

 

3.After restarting the Linux system, check whether the hostname has taken effect.

 

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to switch to a different repository for CentOS 8 EOL

 CentOS 8 reached its end of life at the end of 2021. When using yum repositories for...

To change the network interface name in CentOS 7.x

一.modify GRUB 1.Edit the GRUB configuration file by running the command: `vi...

An incorrect configuration in the /etc/fstab file on the Linux system is causing login issues

Encountering communication issues with the machine, I checked the system error messages through...

How to use the atop monitoring tool in Linux

Atop is a monitoring tool used to monitor resources and processes in Linux systems. It...

How to enable/disable ping on Linux system

To prevent others from discovering and potentially attacking your machine through network ping...