How to Set Hostname Permanently in Linux
Last Updated :
05 Mar, 2025
Setting a hostname is a fundamental step in configuring a Linux system. The hostname acts as an identifier for your machine on a network, making it easier to manage and communicate with other devices. In this article, we'll explore the importance of having a well-defined hostname and the process of setting it permanently in a Linux environment.
A hostname not only gives the recognition of your system but also has a crucial role in networking. Whether you're on a local network or the internet, you should have a clear and identifiable hostname for the administration of your Linux machine.
You can set a temporary hostname using the hostname command while a permanent hostname configuration stays the same even after reboots. This article lets you know how to configure a permanent hostname, understand the relevant configuration files, and the necessary steps to apply the changes.
Also Read: Linux Commands Cheat Sheet
Setting Hostname Permanently and Temporarily in Linux:
Linux allows you to set hostnames both temporarily, for immediate use, and permanently, ensuring the name persists across reboots.
Set Hostname Permanently in Linux
Step 1: Check the Current Hostname
First, you need to check the current hostname of the device by using the hostname command in terminal
hostname
Output:
Hostname displayedEnsure you know the current hostname before making any changes. This command displays the current hostname of your Linux machine. We can see that the hostname of our machine is Kali.
Step 2: Check File Directories
You need to check the contents of these two file directories "/etc/hostname and /etc/hosts" and you will erase some content in these files and rewrite it in the next step.
cat /etc/hostname
cat /etc/hosts
Output:
Hostname and hosts are displayedInspect the contents of these files to confirm the current hostname.
Step 3: Edit /etc/hostname using Vi Editor
Use the sudo vi /etc/hostname command to enter into the vi editor and change the contents in the /etc/hostname file after entering into the vi editor follow these steps:
- Enter insert mode by pressing i.
- Delete the existing hostname and enter the new one.
- Save the changes by pressing Esc, then:wq!.
here we are changing our hostname to GeeksforGeeks
sudo vi /etc/hostname
Output:
command to edit the hostname file
hostname set to GeeksforGeeksStep 4: Edit /etc/hosts using Vi Editor
Use the sudo vi /etc/hosts command to enter into the vi editor and change the contents in the /etc/hosts file after entering into the vi editor follow these steps:
- Enter insert mode by pressing i.
- Delete the existing hostname and enter the new one.
- Save the changes by pressing Esc, then:wq!.
here we are changing our hostname to GeeksforGeeks
sudo vi /etc/hosts
Output:
command to edit the host file
hostname set to GeeksforGeeksStep 5: Save the Hostname Permanently
Now save the hostname permanently by using the sudo hostname <name> command and replace the <name> with the hostname with the hostname you entered in the "/etc/hostname and /etc/hosts" files. We can see GeeksforGeeks has set to the hostname permanently
sudo hostname <name>
Output:
.png)
Step 6: Verify Hostname Changes
You can also verify the hostname changes using hostname and hostnamectl commands in the terminal. Ensure that the changes are reflected in the system.
hostname
hostnamectl
Output:
Hostname successfully set to GeeksforGeeksStep 7: Verify After Reboot
Reboot the system and recheck the hostname to confirm the changes have been applied after a restart.
Set Hostname Temporarily in Linux
Step 1: Check the Current Hostname
First, you need to check the current hostname of the device by using the hostname command in terminal
hostname
Output:
Hostname displayedThis command displays the current hostname of your Linux machine. We can see that the hostname of our machine is Kali.
Step 2: Set the Hostname Temporarily
Here we are setting the temporary hostname as GeeksforGeeks using the command sudo hostnamectl set-hostname <name> and entering the sudo password. Replace <name> with your desired hostname. This change is temporary and will be lost after a system reboot.
sudo hostnamectl set-hostname <name>
Output:
Hostname set to GeeksforGeeksStep 3: Verify Hostname Changes
You can verify the change in hostname by using the hostname or hostnamectl command in the terminal, which will display the temporary hostname we created.
hostname
hostnamectl
Output:
-(1).png)
Ensure that the changes are reflected in the system.
Setting Hostname Permanently From GNOME Settings
Step 1: Open Settings
Open the Applications menu and search for the "About" option then, open it. You will find various settings such as Device Name, Memory, etc.
Search "About"Step 2: Rename the Hostname
Click the arrow mark next to the Device Name. A window will appear, allowing you to rename the device. Rename it accordingly. We are changing our machine's hostname from "Ubuntu" to "GeeksforGeeks."
Click on arrow
Rename itStep 3: Restart
Save the new hostname by clicking "Rename," and the hostname is permanently changed to the new name, here it is changed to "GeeksforGeeks." Restart the machine to ensure the new hostname is set.
RenameNote: Changing the hostname from GNOME settings may not take effect immediately, so a restart of the Linux machine is necessary. This method is not always reliable.
Setting Hostname From Network Manager TUIhostname
Step 1: Check the Current Hostname
First, you need to check the current hostname of the device by using the hostname command in terminal
hostname
Current hostnameStep 2: Set the Hostname
Set the hostname using Network Manager TUI with the 'nmcli general hostname <new_name>' command in the terminal, and replace '<new_name>' with your desired new hostname. Here we are setting our new hostname to GeeksforGeeks from GFG.
nmcli general hostname GeeksforGeeks
Set hostnameStep 3: Privilege Access
After running the previous step, it will prompt for administrator access. Provide the necessary permission by authenticating with your machine's password.
AutenticationStep 4: Restart NetworkManager
After setting the hostname, restart NetworkManager using 'sudo systemctl restart NetworkManager' and restart your machine to apply the changes permanently.
sudo systemctl restart NetworkManager
Restart NetworkManagerStep 5: Verification
You can verify the changes using the hostname command in the terminal:
hostname
Hostname ChangedAlso Read:
Conclusion:
In conclusion, setting a hostname in Linux includes both temporary and permanent steps. It is essential for customization and system identification. While temporary changes are used for immediate needs, making permanent adjustments ensures that the chosen hostname stays the same even after reboots. We can change the hostname temporarily by using the sudo hostnamectl set-hostname <name> command and permanently by changing the hostname to "/etc/hostname and /etc/hosts" It will reflect changes in the hostname even after the reboot. You can also use GNOME and NetworkManager TUI to set the hostname permanently. You can follow the simple steps in the article to change the hostname of a Linux machine permanently
Similar Reads
How to Set or Change System Hostname in Linux?
A hostname is a label assigned to a device connected to a computer network. It is used to identify the device in various forms of electronic communication such as logging in, sending emails, or in network traffic. This name can be anything from a simple single-word string to a more complex string of
4 min read
How to Set Environment Variable in Linux Mint
An environment variable in Linux Mint is a dynamic value that affects how processes and applications operate within the system. These variables are key-value pairs stored in the system's memory and are used to pass configuration information to programs. For example, environment variables can define
4 min read
How to Change Kali Linux hostname?
Changing the hostname in Kali Linux involves modifying system configuration files to update the label assigned to your device on a network. The hostname serves as a unique identifier for the device, simplifying communication by using a readable name instead of an IP address. Adjusting the hostname c
5 min read
How to Rename a Group in Linux?
Renaming a group in a Linux system is a straightforward but essential administrative task. If you're reorganizing your user management structure or enhancing security measures, this quick guide will walk you through the simple steps to rename a group. Linux provides a powerful command, 'groupmod' to
4 min read
How to change the default SSH port in Linux
SSH (Secure Shell) is a network protocol used to securely connect to the remote server where the data between the server and client is transferred in an encrypted format. In the world of Linux system administration and security, one essential practice is changing the default SSH port. This article w
7 min read
How to Rename File in Linux | rename Command
Changing the names of files in Linux is something we often do, and the "rename" command is like a helpful friend for this job. This guide is like a journey to becoming really good at renaming files on Linux, showing you how handy and useful the "rename" command can be. Whether you're just starting o
8 min read
How to Change Namespace in Kubernetes ?
Kubernetes namespaces offer an indispensable instrument for resource organization and isolation in the ever-changing realm of container orchestration. Like distinct districts in a busy metropolis, each namespace in your Kubernetes cluster serves as a dedicated place where you may organize related st
4 min read
How to Edit Text Files in Linux: Proven 3 Methods
Need to tweak a file in Linux but not sure where to start? Editing text files in Linux is a handy skill that can help you fix settings, write scripts, or just note down notes all from your keyboard. This blog is here to help you get started, even if youâre new to Linux. Weâll show you simple ways to
4 min read
How To Setup Proxychains In Linux Without Any Errors?
Proxychains in Linux is another tool for anonymity providing anonymity and safe browsing with proxychains is easy. The proxychains works on socks4, socks5, HTTP, and https protocols. Setting up proxychains is easy but many users get errors while using them, some of the most common errors occur durin
4 min read
How to Change Apache HTTP Port in Linux?
The Apache HTTP server is one of the internet's most popular web servers today, thanks to its versatility, consistency, and a plethora of features, some of which are actually not available on other web servers, such as Nginx's competitor. Some of Apache's most significant features include the abilit
2 min read