howtouselinux

3 ways to change hostname in Linux

Table of Contents

Do you want to change your 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 your hostname is a simple process, but it can be tricky for beginners. If you follow the instructions in this blog post, you should be able to change your hostname without any problems!

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 your current hostname: hostnamectl status
  • If you want to change your 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 best way to change your hostname in Linux is through hostnamectl command. This command is available in systemd based Linux distributions. sudo hostnamectl set-hostname new_hostname. Replace “new_hostname” with the hostname that you want to use. Once you have executed this command, your hostname will be changed.

The following are some of the things 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 your hostname with hostname command line. You have to be the root user to change your 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.The hostname command is used to view or change the system’s hostname. If you want to view the current hostname, you can execute the following command: hostname

change hostname through configuration file in Linux

The second way to change your hostname in Linux is 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 setup new name.
  • Next Edit the /etc/hosts file: sudo vi /etc/hosts
  • Replace any occurrence of the existing computer name with your 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!

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.