Best Linux Distro

How To Change Hostname On Linux? – Complete Guide

People change the hostname in Linux for personalization, network configuration, security, compliance, and troubleshooting purposes. It allows users to customize their system’s name, aids in system identification, simplifies network management, enhances security by adding obscurity, ensures compliance with naming conventions, and helps resolve naming conflicts during troubleshooting.

The hostname is a unique identifier assigned to a system or device connected to a network. It helps distinguish one machine from another when communicating over a network. Changing the hostname can be necessary for various reasons, such as system administration, network configuration, or personal preference.

You can change the hostname using the hostnamectl command with the set-hostname option followed by the new hostname. However, changing the hostname usually requires administrative privileges (root or sudo access).

What is the Hostname in Linux?

In Linux, the hostname refers to the unique name given to a system or device connected to a network. It is used to identify and distinguish one device from another in a networked environment. The hostname is usually a combination of letters, numbers, and hyphens.

The hostname is crucial because it helps in the identification and communication between devices in a network. When a device wants to communicate with another device, it can use the hostname to address it instead of using its IP address. This makes it easier for users to remember and work with devices on a network.

In Linux, you can view and set the hostname using the hostname command. To view the current hostname, you can simply type hostname in the terminal.

Checking Current Hostname

Before changing the hostname, it is useful to check the current hostname to ensure accuracy and provide a reference point for any troubleshooting that may be required. To check the current hostname in Linux, use the following command in a terminal:


This command will display the current hostname of the system.

Example output:

sbm@Ubuntu:~$ hostname

Temporary VS Permanent Hostname Changes

In Linux, hostname changes can be temporary or permanent. Temporary changes modify the hostname for the current session only, and the original hostname will be restored upon system reboot. Permanent changes, on the other hand, update the hostname persistently, ensuring that the new hostname remains even after the system restarts.

It is important to determine whether a temporary or permanent change is required based on your specific needs and system requirements.

Changing Hostname Using the hostname Command

The hostname command in Linux allows you to change the hostname directly from the terminal. The syntax for changing the hostname using the hostname command is as follows:

sudo hostname new_hostname

Replace new_hostname with the desired hostname you want to set.

Example: Change the hostname to “My-Linux

sudo hostname My-Linux

Remember that this method only changes the hostname temporarily. To make it permanent, additional steps are required.

Changing Hostname Using the sysctl Command

Another method to change the hostname in Linux is by using the sysctl command. This method allows for permanent changes to the hostname. The syntax for changing the hostname using the sysctl command is as follows:

sudo sysctl kernel.hostname=new_hostname

Replace new_hostname with the desired hostname you want to set.

Example: Change the hostname to “My-Linux

sudo sysctl kernel.hostname=My-Linux

The sysctl command ensures that the new hostname persists even after the system reboots.

Modifying Hostname in the /etc/hostname File

Another approach to changing the hostname permanently is by modifying the /etc/hostname file. This file contains the current hostname and can be edited to set a new one. Here’s how you can do it:

Step 1: Locating and Opening the ‘/etc/hostname’ File

Use a text editor of your choice to open the /etc/hostname file. For example, using the nano editor:

sudo nano /etc/hostname

Step 2: Editing the File to Update the Hostname

In the opened file, replace the current hostname with the desired new hostname. Make sure to remove any existing content and enter only the new hostname.

Step 3: Saving and Exiting the File

After making the necessary changes, save the file and exit the editor. In nano, you can do this by pressing Ctrl + O, then Enter to save the changes, and Crtl + X to exit the editor.

Updating Hostname in the /etc/hosts File

To ensure that the system can resolve its hostname, it is necessary to update the /etc/hosts file as well. Here’s how you can do it:

Step 1: Locating and Opening the /etc/hosts File

Open the /etc/hosts file using a text editor:

sudo nano /etc/hosts

Step 2: Modifying the File to Reflect the New Hostname

In the file, locate the line that contains the old hostname and replace it with the new hostname. Ensure that the new hostname is associated with the loopback IP address ““. For example: My-Linux

Step 3: Saving and Exiting the File

After making the necessary changes, save the file and exit the editor. In nano, you can do this by pressing Ctrl + O, then Enter to save the changes, and Crtl + X to exit the editor.

Restarting Network Services for Hostname Change

After making changes to the hostname, it is necessary to restart the network services for the changes to take effect. Here’s how you can do it:

Step 1: Reloading Network Configuration

To reload the network configuration, use the following command:

sudo systemctl restart networking

Step 2: Restarting Network Services

To restart the network services, use the following command:

sudo systemctl restart NetworkManager

Verifying the New Hostname

To verify that the hostname has been successfully changed, you can use the following command:


Running this command will display the new hostname.

Example output:

sbm@My-Linux:~$ hostname

Handling Potential Issues or Errors

If you encounter any issues or errors during the hostname change process, here are some common problems and their possible solutions:

Issue: Hostname not updated after reboot.

Solution: Ensure that the /etc/hostname and /etc/hosts files are correctly updated with the new hostname.

Issue: Network connectivity problems after hostname change.

Solution: Check if the network configuration is correct, including the /etc/hosts file and network services.

Factors to Consider

Impact on network services: Changing the hostname may affect network services running on the system. Ensure that the new hostname does not conflict with existing services or cause disruption.

DNS resolution: If the system relies on DNS for hostname resolution, update the corresponding DNS records to reflect the new hostname.

Remote Server Considerations

When changing the hostname on a remote server, it is essential to take extra precautions to avoid losing connectivity:

Notify system administrators or users: Inform any relevant parties about the planned hostname change to avoid confusion or disruption.

Access methods: Ensure that you have an alternative way to access the remote server, such as IP-based access or another hostname, in case there are connectivity issues after the change.

Test in a controlled environment: Before changing the hostname on a production remote server, perform a test run on a non-production server to ensure the process goes smoothly.

Network Configuration Considerations

When working with network configurations, keep the following in mind:

IP address assignment: Changing the hostname does not automatically change the IP address. If the IP address assignment is based on the hostname, update the IP configuration accordingly.

Firewall and security rules: If there are firewall rules or security configurations tied to the hostname, review and update them to reflect the new hostname.

Guidelines for Selecting a Hostname

When selecting a hostname for your Linux system, follow these best practices:

Descriptive yet concise: Choose a hostname that provides meaningful information about the system’s purpose or location without being overly long or complex.

Avoid special characters: Stick to alphanumeric characters and hyphens (-) when naming your hostname. Special characters may cause issues or be incompatible with certain applications or systems.

Consistency: Maintain consistency across your network by using a naming convention for hostnames. This can simplify administration and troubleshooting.

Additional Tips and Tricks for Managing Hostnames in Linux

Here are some additional tips and tricks for effectively managing hostnames in Linux:

Documentation: Keep track of hostname changes and the associated configuration details for future reference and troubleshooting.

Regular audits: Conduct periodic audits to ensure hostnames remain up-to-date and aligned with your system architecture.

Automation: If managing a large number of systems, consider using configuration management tools or scripts to automate hostname changes and updates.

Testing and validation: Before implementing hostname changes in production environments, test the process thoroughly in a non-production environment to identify any potential issues or conflicts.

By following the steps and considerations outlined in this article, you can confidently change the hostname in your Linux system, whether it’s a temporary or permanent modification, and ensure smooth network operations and system administration.


Changing the hostname in Linux is a straightforward process that can be done temporarily or permanently using various methods. By following the provided steps, considering remote servers and network configurations, and adhering to best practices, you can confidently manage and update hostnames in Linux, ensuring network stability and efficient system administration.

Leave a Reply

Your email address will not be published. Required fields are marked *

twenty − 13 =