How to change hostname on Linux

How to change hostname on Linux

Overview

In this post we will cover the steps on how to change hostname on Linux. We’ll go through the basics on how to change a hostname on a Linux machine and why we do it and all the steps will be demonstrated using the terminal. Additionally, we’ll showcase a couple of different methods on how to do it.

The options we’ll demonstrate can be applied on almost any Linux and can be used for an example to change hostname on Ubuntu/Debian or to change hostname on Fedora/RedHat.

This process can also be executed automatically by utilizing Ansible(demonstrated that procedure too in this post) and what’s also great about it, it completed the process without the need to restart the Linux server.

Requirements:

  • User with a sudo privileges

Hostname in Linux

Being able to change the hostname of our Linux machine is a useful option that allows us to identify our machine the way we want/need. So, let’s start first identifying our Linux machine and we can do that by running the command “hostname” or “hostnamectl”. Let’s see an example:

change hostname on Linux

Now let’s move on to the steps on how to change a hostname in Linux.

Change hostname on Linux with the hostname command

As we specified earlier and mentioned the “hostname”, apart from being able to tell us the hostname of the Linux machine but we can use it to change the hostname too.

To change the hostname, run the command like in the following:

sudo hostname markontech

change hostname on Linux
Change hostname on Linux with the hostname command

Although we can change the hostname with the “hostname” command, the change is not permanent as you can tell and we were provided with a hostname version called “the transient hostname”(a form of a temporary name). After the reboot that name will disappear but the “transient hostname”, once active can be discovered by services such as DHCP, DNS and mDNS so still it does have some use cases.

Set hostname on Linux with hostnamectl Command

With the “hostnamectl” command we can change the hostname on Linux permanently if we choose to. To do the change, run the following command.

sudo hostnamectl set-hostname markontech

change hostname on Linux
Set hostname on Linux with hostnamectl Command

Once you run the command, restart the Linux machine in order to apply the changes.

Change hostname on Linux by editing the host files

Another method to change the hostname on Linux is by manually editing the hosts files by using any text editor. With this method, you’ll also change the hostname on Linux permanently.

There are two hosts files present on Linux and In order to do the mentioned method(changing the hostname), we need to edit one of these files. The files in question are “hosts” and the “hostname”. They can be located in the following paths:

/etc/hosts
/etc/hostname

So, to edit these, run one of these text editors/commands:

sudo nano /etc/hosts
sudo nano /etc/hostname

Or

Sudo vim /etc/hosts
Sudo vim /etc/hostname

change hostname on Linux
Change hostname on Linux by editing the host files
change hostname on Linux
Change hostname on Linux by editing the host files
change hostname on Linux
Change hostname on Linux by editing the host files

After you have edited one of the files, restart the Linux machine in order to apply the changes.

Change hostname on Linux without rebooting

So far, we showcased multiple options on how to change hostname on Linux but all these so far require restarting the machine and sometimes there may come a situation where a reboot is not possible for us. There is a method on how to change the hostname on Linux without rebooting.

But small heads up first and beware – this option is to be used as a last resort since this method involves editing the hostname directly on a kernel itself. It may also happen that some Linux distros will deny access to edit the hostname even with sudo privileges and in that case then it will be necessary to login as root and edit the hostname.

The procedure looks as following:

sudo echo my_host_name > /proc/sys/kernel/hostname

If the command gets blocked and Linux machine returns the error that “the permission is denied” for this command then “root” account login is necessary in order to change the hostname. The procedure goes like the following:

sudo -i
echo my_host_name > /proc/sys/kernel/hostname

Logout and login again and the new name should appear.

Summary

Let’s summarize the article – we covered in this post multiple methods on how to change hostname on Linux, i.e. we showcased the steps on how to set a hostname on a Linux machine. 

We also covered the methods on how to apply these changes temporarily and permanently by executing different CLI commands and also by editing the hosts file using a text editor. Out of all these options, probably the most convenient approach is to change the hostname on Linux without rebooting the machine.

Thank you very much for your time…

You may also like...

Leave a Reply

Your email address will not be published.