Skip to Content

3 ways to change hostname in Linux

Do you want to change the hostname in Linux? There are three different ways that you can do this: through the command line, through a configuration file, or through a GUI tool.

In this blog post, we will discuss each of these methods in detail.

We will also provide instructions on how to execute them. Changing the hostname is a simple process, but it can be tricky for beginners. 

Keep in mind that changing the hostname may affect the system in various ways. For example, it may cause problems with network configuration or with applications that rely on the hostname to identify the system.

Be sure to test the system thoroughly after making any changes to ensure that everything is working as expected.

The following commands can be used to change the hostname in Linux.

  • hostnamectl set-hostname newhostname
  • hostname newhostname
  • change the hostname in /etc/hostname and /etc/hosts and reboot

 

steps to change hostname in Linux

  • Enter the following command to check the current hostname: hostnamectl status
  • If you want to change the hostname, use this command: sudo hostnamectl set-hostname new_hostname
  • To check if the hostname was changed, enter the command from step 1 again.

 

change hostname with hostnamectl command in Linux

The hostnamectl command is the recommended method for changing the hostname on most modern Linux distributions. To change the hostname, use the set-hostname flag and specify the new hostname.

sudo hostnamectl set-hostname new_hostname. Replace “new_hostname” with the hostname that you want to use.

Once you have executed this command, the hostname will be changed. This change will take effective without reboot.

The following are examples that you can do with hostnamectl command:

Commands:

  •  status         Show current hostname settings
  •  set-hostname NAME   Set system hostname
  •  set-icon-name NAME   Set icon name for host
  •  set-chassis NAME    Set chassis type for host
  •  set-deployment NAME  Set deployment environment for host
  •  set-location NAME   Set location for host

change hostname with hostname command in Linux

We can also change the hostname with hostname command line. You have to be the root user to change the hostname.

Open a terminal window and type the following command: sudo hostname new_hostname. Press Enter to run the command.

The new_hostname will be set as the hostname for the system.

Replace “new_hostname” with the hostname that you want to use. 

The hostname will be reverted back to the old one after the system is rebooted. If you need to change the hostname permanently, check out the following methods.

change hostname through configuration file in Linux

We can change the hostname in Linux through a configuration file. Type the following command to edit /etc/hostname using nano or vi text editor:

  • sudo vi /etc/hostname: Delete the old name and input a new name.
  • Next Edit the /etc/hosts file: sudo vi /etc/hosts
  • Replace any occurrence of the existing hostname with a new one.
  • Reboot the system to changes take effect: sudo reboot

 

 

FAQ about hostname in Linux

How to change the hostname in Linux permanently?
If you want to change the hostname permanently, you will need to edit the /etc/hosts and /etc/hostname files. You can use any text editor to do this. We recommend using nano or vi.

What is the default hostname in Linux?
The default hostname for a Linux computer is “localhost”.

Changing your hostname is a simple process, but it can be tricky for beginners. These are three different ways that you can change your hostname in Linux. Choose the method that you are most comfortable with and follow the instructions.

If you have any questions, feel free to leave a comment below!