How to Change Hostname on Linux
This article is about hostname in Linux, It covers need to change the hostname, How to check and change current hostname.
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.
Points to be covered:
Best-suited Linux courses for you
Learn Linux with these high-rated online courses
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.
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
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
Step 3: Type the following Command to open the /etc/hostname file in a text editor
$sudo nano /etc/hostname
Step 4: Replace the existing hostname with the new hostname you want to use
Here I have replaced “ubuntu” with “anish-laptop”
Save and exit the nano text editor.
Steps to save and exit a nano text editor: –
- Press CTRL + O to save the file.
- Press ENTER to confirm the filename and location.
- 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
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”
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
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
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”
Step 4: Verify that the hostname has been changed by typing the following Command
$hostnamectl
Or you can open a new terminal tab to verify.
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
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”
Step 4: Enter your password when prompted
Step 5: Verify that the new hostname has been set by entering the hostnamectl Command again
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.
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
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