Oct 16, 2023 3 min read

How to Set or Change the Hostname on Linux

Set or Change the Hostname on Linux with our step-by-step tutorial. It refers to the unique name assigned to a device connected to a network.

Set or Change the Hostname on Linux
Table of Contents

Introduction

Before we begin talking about how to set or change Hostname on Linux. Let’s briefly understand - What is a Hostname?

A hostname refers to the unique name assigned to a device connected to a network. It is set during the installation process or while creating a virtual machine, it is assigned dynamically. There will be situations when you need to change it.

The hostname is a label assigned to a machine that identifies the machine on the network. Each device in the network needs to have a unique hostname.

In this tutorial, you will set and change a Hostname on Linux. We will also address some of the FAQs related to Hostnames.

Advantages of Hostname

  1. Network Identification: Hostnames enable devices to be easily identified on a network, facilitating seamless communication and resource sharing.
  2. Device Management: With unique hostnames, administrators can efficiently monitor and control individual devices within a network.
  3. Remote Access: Hostnames simplify remote access to specific devices, making it more convenient to manage them from anywhere in the world.
  4. Security Enhancement: Hostnames provide an additional layer of security by masking IP addresses, making it harder for potential attackers to directly target devices.
  5. Resource Allocation: By using hostnames, network administrators can allocate resources, such as bandwidth or storage, to specific devices based on their unique names, ensuring optimal utilization.

Step 1 - Displaying Current Hostname

1) Using systemd, the hostnamectl command is useful to query and change the hostname and many other settings on the Linux machine.

To view the current hostname, you need to invoke the hostnamectl command without any arguments:

hostnamectl

Step 2 - Changing the Hostname

1) To change the hostname, you need to invoke the hostnamectl command along with set-hostname argument that will be followed with the new hostname. You need to be a root user or a user with sudo privileges to be able to change the hostname.

For example, if you want to change the system hostname to mail.vegastack.com. You need to use the following command:

sudo hostnamectl set-hostname mail.vegastack.com

2) The hostnamectl command will not produce output. If successful, 0 will be returned, otherwise a non-zero failure code.

To verify that the hostname is successfully changed, use the hostnamectl command:

hostnamectl

The new system hostname along with some additional system information will be displayed on the terminal.

Output

Static hostname: mail.vegastack.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 70a3f06298014fd9ac42e5dc1de1034a
           Boot ID: 6d45a1a8d436418e97519da01ea61c1b
    Virtualization: oracle
  Operating System: Debian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.0-5-amd64
      Architecture: x86-64

FAQs to Set or Change the Hostname

What is a valid hostname?

Many valid characters for hostnames are ASCII(7) letters from a to z, the digits from 0 to 9, as well as hyphen (-). A hostname can never start with a hyphen.

Are there any restrictions on naming a hostname? 

Yes, a hostname must be between 2-63 characters long and can only contain alphanumeric characters and hyphens.

Is it necessary to restart my Linux system after changing the hostname?

 Yes, a system restart is generally required to apply hostname changes permanently. However, temporary hostname changes take effect immediately.

What are the restrictions while setting a hostname on Linux? 

Hostnames on Linux should consist of alphanumeric characters and hyphens, and they must begin and end with alphanumeric characters. The maximum length varies across distributions but is typically limited to 64 characters.

Where else is the hostname used on Linux? 

The hostname is used in various system processes and configurations, including the network services, SSH, log files, and email servers. Changing the hostname affects these services and associated logs.

Does changing the hostname affect the system's IP address? 

No, changing the hostname does not affect the system's IP address. The IP address and hostname are separate entities.

Are there any services that may be affected by changing the hostname? 

Yes, some services rely on the hostname for proper functioning. It is recommended to check services like mail servers, web servers, and SSH to ensure they continue to work correctly after a hostname change.

Conclusion

We hope this detailed tutorial helped you to set or change Hostname on Linux.

If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.