How to Change Hostname on Linux

How to Change Hostname on Linux

6 mins read256 Views Comment
Updated on Aug 5, 2024 11:04 IST

This article is about hostname in Linux, It covers need to change the hostname, How to check and change current hostname.

2023_04_MicrosoftTeams-image-25.jpg

In the world of Linux, the hostname is an essential component of a system’s identity. Changing the hostname on a Linux system is a relatively simple process. The hostname is the name assigned to a computer or server that identifies it on a network. A hostname is a label that is assigned to a device on a network. It is typically used to identify and differentiate a machine from other devices on the network. A hostname can also be used to identify a particular service running on a machine. In this article, we will explore how to change the hostname on Linux.  In this Linux tutorial, you will learn how to change the hostname on Linux. 

2023_04_image-44.jpg

Points to be covered: 

Recommended online courses

Best-suited Linux courses for you

Learn Linux with these high-rated online courses

– / –
3 months
7 K
3 months
– / –
2 months
– / –
1 year
– / –
200 hours
– / –
3 months
– / –
3 months
– / –
2 months
– / –
3 months

What is a Hostname? 

A hostname is a name given to a device connected to a network. The hostname is used to identify the device and distinguish it from other devices on the network. Hostnames are typically used to identify servers, workstations, printers, and other devices connected to a network. A hostname can be any combination of letters, numbers, and hyphens and can be up to 255 characters long. 

How to Delete a File in Linux?
Learn Basic Linux Commands (With Syntax)
How to Set Environment Variables in Linux?

You can also explore: Linux courses

Must read: Difference between Linux and Unix

Why Change Hostname on Linux? 

By default, the system hostname is set during the installation process of Linux, or if you are creating a virtual machine, it is dynamically assigned to the instance at startup. The hostname is an identifier that is used to distinguish one machine from another on a network. It is usually a short and memorable name that is easy to remember and type, making it easier to manage and maintain the system. However, there are situations where it becomes necessary to change the hostname of a Linux machine. 

How do I check my current hostname? 

You can use the command hostname in the terminal to check your current hostname. 

$hostname 

2023_04_image-27.jpg

How to Change Hostname on Linux? 

In this article, we’ll discuss changing the hostname on a Linux system, specifically on Ubuntu. However, the steps outlined in this article can also be applied to other Linux distributions. The hostname is a critical component of a Linux system’s identity, and changing it can have several benefits, including improved organization, increased security, and more personalization. 

Later in this article, we’ll also cover changing the hostname on Fedora, a popular Linux distribution used in many enterprise environments. So, whether you’re using Ubuntu or Fedora, read on to learn how to change the hostname on your system. 

Before we dive into the specific steps for changing the hostname on Ubuntu and Fedora, let’s take a moment to discuss the various methods you can use to accomplish this task. As mentioned earlier, there are two primary methods for changing the hostname on a Linux system: 

  • Editing the /etc/hostname file 
  • Using the hostnamectl Command 

Each method has advantages and disadvantages, so choosing the appropriate method based on your Linux distribution and kernel version is essential. We’ll explore each method in detail, so you can decide which one to use for your system. 

Now, let’s begin with the first method for changing the hostname on Ubuntu: using the /etc/hostname file and later hostnamectl Command. 

Method 1: Editing the /etc/hostname and /etc/hosts file 

The /etc/hostname file contains the hostname of the Linux system. To change the hostname using this method, follow these steps: 

Step 1: Open a terminal window

Step 2: Type the following Command to check the hostname 

$hostname 

or 

$cat /etc/hostname 

2023_04_image-71.jpg

Step 3: Type the following Command to open the /etc/hostname file in a text editor

$sudo nano /etc/hostname 

2023_04_image-23.jpg

Step 4: Replace the existing hostname with the new hostname you want to use

Here I have replaced “ubuntu” with “anish-laptop” 

2023_04_image-28.jpg

Save and exit the nano text editor. 

Steps to save and exit a nano text editor: – 

  1. Press CTRL + O to save the file. 
  2. Press ENTER to confirm the filename and location. 
  3. Press CTRL + X to exit nano. 

Step 5: Type the following Command to open the /etc/hosts file in a text editor

$sudo nano /etc/hosts 

2023_04_image-43.jpg

Step 6: Replace any instance of an existing hostname with the new hostname you want to use

Here I have replaced “ubuntu” with “anish-laptop” 

2023_04_image-30.jpg

Save and exit the file. 

To save and exit the file in the Nano editor, follow the instructions above while saving the /etc/hostname file. 

Step 7: Reboot the system for the changes to take effect

$sudo Reboot 

2023_04_image-72.jpg

After Reboot, type the following Command to verify the changes. 

$hostname 

Method 2: Using the hostnamectl Command 

The hostnamectl Command is a command-line tool that allows you to view and modify the hostname of a Linux system. This tool is available on most modern Linux distributions, including Ubuntu, Debian, Fedora, CentOS, and Red Hat Enterprise Linux. 

To change the hostname using the hostnamectl Command, follow these steps

Step 1: Open a terminal window 

Step 2: Type the following Command to view the current hostname

$hostnamectl 

2023_04_image-34.jpg

This will display information about your system, including the current hostname. 

Step 3: To change the hostname, type the following Command

$hostnamectl set-hostname [new-hostname] 

Replace [new_hostname] with the new hostname you want to use. 

Here I have replaced it with “anish-laptop” 

2023_04_image-73.jpg

Step 4: Verify that the hostname has been changed by typing the following Command 

$hostnamectl 

2023_04_image-36.jpg

Or you can open a new terminal tab to verify. 

2023_04_image-37.jpg

Now, let’s use the same method for changing Fedora’s hostname: the hostnamectl Command. 

Fedora, like Ubuntu, uses the systemd hostnamectl Command to manage the system hostname.  

To change the hostname on Fedora using this method, follow these steps

Step 1: Open a terminal window on your Fedora system

Step 2: Enter the following Command to check the current hostname: hostnamectl

2023_04_image-38.jpg

Step 3: To change the hostname, use the following Command, replacing “new-hostname” with the desired hostname  

$sudo hostnamectl set-hostname new-hostname. 

Here I have replaced it with “anish-laptop” 

2023_04_image-74.jpg

Step 4: Enter your password when prompted

Step 5: Verify that the new hostname has been set by entering the hostnamectl Command again

2023_04_image-75.jpg

Once you’ve completed these steps, the new hostname will be set on your Fedora system. It’s important to note that the new hostname will only take effect once you restart the system. 

After Reboot, we can see the hostname is changed. 

2023_04_image-42.jpg

In summary, changing the hostname on Fedora is similar to changing it on Ubuntu, and you can use the hostnamectl Command to accomplish this task. With the steps outlined in this article, you can easily change the hostname on Ubuntu, Fedora, or other Linux-based systems. 

Conclusion 

Changing the hostname on a Linux system has multiple benefits, including improving organization, enhancing security, and allowing for personalization. It’s a simple process that can be done on Ubuntu or Fedora to enjoy these advantages. By following the steps outlined in this article, you can change the hostname on your Linux system and start enjoying these benefits. 

Author-Anish Lohiya

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio