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.
-
Open a terminal.
-
Enter the following command, replacing
new_hostname
with the name you wish to set as the new hostname:sudo hostnamectl set-hostname new_hostname
-
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.
-
Open the
/etc/hostname
file in a text editor such asnano
orvi
:sudo nano /etc/hostname
-
Modify the contents of the file to the new hostname.
-
Save and close the file.
-
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.
-
Open the
/etc/hosts
file in a text editor:sudo nano /etc/hosts
-
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 andnew_hostname
is your new hostname:127.0.1.1 new_hostname
-
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.