How to Change Linux Hostname Using 4 Different Methods

Estimated reading: 5 minutes 186 views

A hostname is a name used to identify a device on a network. Usually, it is set as localhost during the operating system installation.

However, if there are several devices in a network, this might generate duplicates and cause network conflict. To avoid that, you’re advised to change the hostname of your Linux system.

In this article, we will illustrate three methods to change the hostname on CentOS and other Linux distributions like Debian, Ubuntu, and Red Hat Enterprise Linux (RHEL).

A hostname is a name used to identify a device on a network. Usually, it is set as localhost during the operating system installation.

However, if there are several devices in a network, this might generate duplicates and cause network conflict. To avoid that, you’re advised to change the hostname of your Linux system.

In this article, we will illustrate three methods to change the hostname on CentOS and other Linux distributions like Debian, Ubuntu, and Red Hat Enterprise Linux (RHEL).

How to Change Hostname Permanently on Linux

In this section, you’ll learn how to change the current hostname permanently on Linux virtual private servers – using the hostnamectl command, Hostinger VPS, nmtui, or editing your configuration files.

If you’re just starting, you’ll find all the instructions on how to set up VPS in our step-by-step guide.

Using the hostnamectl Command

Modern Linux distributions come with systemd, a system and service manager that has the hostnamectl command installed by default to manage and change hostnames.

Here’s how to change your current hostname using hostnamectl:

  1. Open the terminal.
  2. Log in to your Linux server via SSH.
  3. If your system doesn’t have systemd, use this command to install it and press Enter:
apt-get install systemd
  1. Once the installation is complete, type the following command to view the current hostname and hit Enter:
hostnamectl
  1. Here’s how the output looks like:

  1. On the output, the current hostname is listed as the static hostname. To change it permanently, insert the following command – using your new desired hostname – and hit Enter:
hostnamectl set-hostname hostname
  1. The command above does not produce an output. Therefore, to verify the new hostname, run the command below and press Enter:
hostnamectl

The output will show you the new hostname.

Using the Network Manager Text User Interface (nmtui)

The Network Manager Text User Interface (nmtui) is a text user interface that lets you configure the network and hostname in a terminal window. It is available in various Linux distributions, including CentOS and RHEL.

Here’s how to change the hostname using the nmtui command:

  1. Open the terminal and connect to your account via SSH.
  2. Install the NetworkManager-tui package by inserting the following command and hitting Enter:
# For Debian-based distributions
apt-get install network-manager

# For RPM-based distributions
yum install NetworkManager-tui

3.Run this command to check the status of the network manager and press Enter:

service NetworkManager status

4.To start the nmtui service, type the following command and hit Enter:

service NetworkManager start

5. Next, type this command and click Enter:

nmtui

6. On the NetworkManager TUI window, select Set system hostname and press Enter.

7. Type the desired hostname and choose OK.

You’ve successfully changed the hostname.

Editing the Configuration Files

The next option is editing the configuration files – /etc/hostname and /etc/hosts. Here’s how to do it:

  1. Open the terminal and connect via SSH.
  2. Open the /etc/hostname file and replace the old hostname with the new one:
nano /etc/hostname
  1. Next, update the hostname record in the /etc/hosts file so that the system resolves the new hostname in the network.
# nano /etc/hosts

4. Reboot your system and open the terminal. To verify if the hostname was indeed preserved during the reboot, run the following command and hit Enter:

hostname

This will display the new hostname you have set up.

How to Change Hostname Without Rebooting

In this section, you’ll learn how to change the hostname without rebooting the system. However, as soon as you restart the system, it will revert to the old hostname.

This method is useful when you need to complete a minor task that requires temporary changes. The basic syntax of the command is:

hostname [new_host_name]

Here’s how to use it:

  1. On the terminal, type the hostname command along with the new hostname and hit Enter.
hostname Greencloud2
  1. Verify if the hostname has been set correctly by inserting the following command and pressing Enter:
hostname

The output will display the updated hostname.

Conclusion

The system hostname is an essential part of a Linux server as it helps identify your machine within a network.

Most Linux systems provide tools and commands so users can customize the system hostnames to their preferences. We shared three methods to change hostnames based on your needs:

  • Running the hostnamectl command, using nmtui, or editing the hosts file to change the hostname permanently.
  • Using the hostname [new_host_name] command to change the hostname temporarily and without rebooting the system.

We hope this article has helped you set up a new hostname in your Linux system. If you have any questions or suggestions, please share them in the comment section below.