Linux Tactic

Easy Steps to Change the Hostname on CentOS 7 Without Restarting

How to

Change the Hostname on CentOS 7 Without Restarting

CentOS 7 is a popular Linux distribution used by many server administrators around the world. One of the essential tasks when setting up a new server is to configure its hostname.

A hostname is a name that identifies a computer on a network, and it is used to identify the machine instead of its IP address. In this article, we will explore how to change the hostname on CentOS 7 without restarting the system.

Understanding Host Names

Before we dive into the process of changing the hostname on CentOS 7, it is essential to understand what a hostname is and why it matters. A hostname is a text-based label that identifies a computer on a network.

This name is used instead of the machine’s IP address, as it is more user-friendly and easier to remember. A Fully Qualified Domain Name (FQDN) includes both the hostname and the domain name.

For example, if the hostname of a computer is ‘server’, and the domain name is ‘example.com’, the FQDN would be ‘server.example.com’. The FQDN is used to identify a computer on the internet.

Display the Current Hostname

To change the hostname on CentOS 7, we first need to know the current hostname. To display the current hostname, we can use the

hostnamectl command. Open a terminal window and enter the following command:

“`

hostnamectl

“`

This command will display the current hostname, including the static hostname, the pretty hostname, and the transient hostname, as shown below:

“`

Static hostname: server

Pretty hostname: localhost.localdomain

Transient hostname: server

“`

The static hostname is the name that is stored in the hostname configuration file, and it persists across reboots. The pretty hostname is a human-readable name that is displayed in the command prompt.

The transient hostname is a hostname that is used temporarily and can change without affecting the static hostname.

Change the Hostname

There are several ways to change the hostname on CentOS 7. We will discuss three methods: using the

hostnamectl command, using the nmtui command, and using the nmcli command. Method 1: Using the

hostnamectl command

The

hostnamectl command is the recommended method for changing the hostname on CentOS 7. To change the hostname, we need to use the sudo command to run the

hostnamectl command with root privileges. We can set the static hostname, the pretty hostname, and the transient hostname with this command.

For example, to set the static hostname to ‘newserver’, enter the following command:

“`

sudo

hostnamectl set-hostname newserver

“`

After running the command, we need to log out and log back in for the changes to take effect. We can confirm that the hostname has been changed by entering the ‘

hostnamectl’ command again. Method 2: Using the nmtui command

The nmtui command is a text-based interface for NetworkManager.

We can use this command to set the system hostname. To change the hostname using nmtui, open a terminal window and enter the following command:

“`

sudo nmtui

“`

Use the arrow keys to navigate to the ‘Set system hostname’ option and press Enter. Enter the new hostname and press Enter to confirm the changes.

Exit the nmtui interface, and the changes should take effect immediately. Method 3: Using the nmcli command

The nmcli command is a command-line tool for controlling NetworkManager.

We can use this command to set the general hostname, which is the static hostname. To change the hostname using nmcli, open a terminal window and enter the following command:

“`

sudo nmcli general hostname newserver

“`

After running the command, we need to log out and log back in for the changes to take effect.

Verify the change

After changing the hostname, we need to verify that the new hostname has been set correctly. To do this, open a terminal window and enter the following command:

“`

hostnamectl

“`

This command should display the updated hostname, including the static hostname, the pretty hostname, and the transient hostname.

Conclusion

In conclusion, changing the hostname on CentOS 7 is a critical task for server administrators. In this tutorial, we explored three methods for changing the hostname on CentOS 7 without restarting the system.

By understanding hostnames and using the appropriate commands, we can change the hostname quickly and easily. If you have any questions or comments, feel free to leave them below.

In this tutorial, we have learned how to change the hostname on CentOS 7 without restarting the system. We began by understanding what hostnames are and why they are essential.

Then, we discussed three methods to change the hostname, which include using the

hostnamectl command, the nmtui command, and the nmcli command. Finally, we verified the hostname changes using the

hostnamectl command. Changing the hostname on CentOS 7 is a critical task for server administrators.

Understanding how to do this ensures that our systems can be easily identified on a network. By following the steps outlined in this tutorial, we can change the hostname on CentOS 7 with ease.

Popular Posts