How to permanently change the hostname on Linux?

LinuxLinuxBeginner
Practice Now

Introduction

Maintaining a consistent and meaningful hostname is crucial for managing and identifying your Linux systems. This tutorial will guide you through the process of permanently changing the hostname on your Linux machine, ensuring a smooth and efficient system administration experience.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/SystemInformationandMonitoringGroup -.-> linux/uname("`System Information Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/hostname("`Hostname Managing`") linux/UserandGroupManagementGroup -.-> linux/set("`Shell Setting`") linux/UserandGroupManagementGroup -.-> linux/export("`Variable Exporting`") linux/UserandGroupManagementGroup -.-> linux/unset("`Variable Unsetting`") subgraph Lab Skills linux/uname -.-> lab-409897{{"`How to permanently change the hostname on Linux?`"}} linux/hostname -.-> lab-409897{{"`How to permanently change the hostname on Linux?`"}} linux/set -.-> lab-409897{{"`How to permanently change the hostname on Linux?`"}} linux/export -.-> lab-409897{{"`How to permanently change the hostname on Linux?`"}} linux/unset -.-> lab-409897{{"`How to permanently change the hostname on Linux?`"}} end

What is a Hostname?

A hostname is a unique name assigned to a device or computer connected to a network, such as the internet or a local area network (LAN). It serves as an identifier for the device, allowing other devices to communicate with it. Hostnames are an essential component of network communication and are used in various applications, including web browsing, secure shell (SSH) connections, and email routing.

Hostnames can be customized to reflect the device's purpose, location, or any other relevant information. They can consist of a combination of letters, numbers, and hyphens, and are typically structured in a hierarchical manner, with subdomains and top-level domains.

For example, the hostname "labex.io" could represent the main website of the LabEx company, while "server1.labex.io" could be the hostname of a specific server within the LabEx network.

Hostnames play a crucial role in network administration and user experience. They provide a human-readable way to identify devices, making it easier to remember and access them, compared to using IP addresses, which can be more difficult to remember.

graph TD A[Device] --> B[Hostname] B --> C[Network Communication] C --> D[Web Browsing] C --> E[SSH Connections] C --> F[Email Routing]
Hostname Purpose
labex.io Main website of LabEx company
server1.labex.io Specific server within the LabEx network

Changing the Hostname Permanently

To change the hostname on a Linux system permanently, you can follow these steps:

Step 1: Identify the Current Hostname

You can check the current hostname by running the following command in the terminal:

hostname

This will display the current hostname of your system.

Step 2: Edit the Hostname Configuration Files

  1. Open the /etc/hostname file using a text editor:
sudo nano /etc/hostname
  1. Replace the current hostname with the new one you want to set. For example, if you want to change the hostname to "labex-server", the file should contain:
labex-server
  1. Save the changes and exit the text editor.

Step 3: Update the /etc/hosts File

Next, you need to update the /etc/hosts file to reflect the new hostname:

sudo nano /etc/hosts

Locate the line that starts with "127.0.0.1" and replace the current hostname with the new one. For example:

127.0.0.1 labex-server localhost

Save the changes and exit the text editor.

Step 4: Apply the Hostname Change

Finally, to apply the new hostname, you can reboot your system:

sudo reboot

After the reboot, the new hostname will be applied and visible throughout your system.

graph LR A[Identify Current Hostname] --> B[Edit /etc/hostname] B --> C[Update /etc/hosts] C --> D[Apply Hostname Change]
Step Description
1 Identify the current hostname using the hostname command
2 Edit the /etc/hostname file and replace the current hostname with the new one
3 Update the /etc/hosts file to reflect the new hostname
4 Reboot the system to apply the new hostname

Verifying the Hostname Change

After changing the hostname, you can verify the new hostname in several ways:

Verify the Hostname in the Terminal

You can check the current hostname by running the following command in the terminal:

hostname

This should display the new hostname you set in the previous steps.

Verify the Hostname in System Information

You can also check the system information to verify the new hostname. Open the System Settings and navigate to the "Details" section. The hostname should be displayed there.

Alternatively, you can use the following command to display the system information, including the hostname:

hostnamectl

This will provide a detailed overview of the system, including the current hostname.

You can use network-related commands to verify the new hostname. For example:

ping labex-server

This command will ping the system using the new hostname, confirming that the change has been applied.

graph LR A[Terminal] --> B[System Information] B --> C[Network-related Commands]
Verification Method Command
Terminal hostname
System Information hostnamectl
Network-related Commands ping labex-server

By following these steps, you can ensure that the hostname change has been successfully applied and is now visible throughout your Linux system.

Summary

In this Linux tutorial, you have learned how to permanently change the hostname on your system. By updating the hostname, you can enhance the organization and identification of your Linux machines, making system administration tasks more efficient. Remember, updating the hostname is a straightforward process that can be easily accomplished, empowering you to take full control of your Linux environment.

Other Linux Tutorials you may like