How to change Hostname on Linux

Table of Contents

Introduction

Wondering how to change the hostname on a Linux machine ? 

 Setting the desired hostname is one of the very important steps in the deployment process. In this article we are going to discuss some ways through which you can edit the hostname of the Linux machine.

Using “hostname” command to edit the hostname

First of all let’s start with checking the current hostname of the machine. ‘hostname‘ command can help you with that 

root@ubuntu-machine-1:/# hostname
Ubuntu-machine-1

You can use the ‘help’ option along with the hostname command to know about other options 

root@ubuntu-machine-1:/# hostname --help

To set a new hostname we are going to use the hostname command in this example , please note that setting the hostname with the hostname command is not persistent across the reboot as it doesn’t edit the /etc/hostname file to set the hostname. 

root@ubuntu-machine-1:/# hostname DiscoveringSystems

It shows it has DiscoveringSystems as the hostname , however we were able to see the actual hostname didn’t change  in the prompt and also it didn’t edit the /etc/hostname file with the new hostname 

root@ubuntu-machine-1:/# hostname
DiscoveringSystems

root@ubuntu-machine-1:/# cat /etc/hostname
Ubuntu-machine-1

We will be seeing the new hostname will be updated in the prompt after log out / login or spinning up a new bash shell , but still the /etc/hostname file won’t get edited with the new hostname , because of this it is not persistent across the reboot 

root@Ubuntu-machine-1:~$ exit
logout

Client-machine > ssh root@< vm-ip >
root@< vm-ip >'s password:
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-43-generic x86_64)
.
.
.
root@DiscoveringSystems:/#

root@DiscoveringSystems:/# hostname
DiscoveringSystems
root@DiscoveringSystems:/# cat /etc/hostname
Ubuntu-machine-1
root@DiscoveringSystems:/#


Now to confirm this is not persistent we reloaded the machine

Client-machine > ssh root@< vm-ip >
root@< vm-ip >'s password:
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-43-generic x86_64)
.
.
.
root@Ubuntu-machine-1:/#

root@Ubuntu-machine-1:/# cat /etc/hostname
Ubuntu-machine-1
root@Ubuntu-machine-1:/# hostname
Ubuntu-machine-1

Manually editing the hostname

Let’s explore the manual way of editing the hostname  in this section 

The  /etc/hostname is the place to set the hostname , so that we can see it shows up from the next bash session , not even a reboot is required 

This is the current hostname 

root@ubuntu-machine-1:/# cat /etc/hostname
ubuntu-machine-1

We are editing the hostname to Discoveringsystems.ds.com ( creating the FQDN – full name )

root@ubuntu-machine-1:/# echo "DiscoveringSystems.ds.com" > /etc/hostname

root@ubuntu-machine-1:/# cat /etc/hostname
DiscoveringSystems.ds.com

Still when you execute the hostname command to view the current hostname settings , we will be able to see the old hostname , inorder for the settings on the “/etc/hostname “ file to take effect , we can log out and log in the session or reboot the machine 

Closing the session to confirm whether it is taking effect from the next bash session 

root@ubuntu-machine-1:/# hostname
ubuntu-machine-1
root@ubuntu-machine-1:/#exit 

.
<trying in the next bash session > 
.
root@DiscoveringSystems:/# hostname
DiscoveringSystems.ds.com

The change we made to the /etc/hostname is persistent across reboot , so we will see the new hostname stays even after reboot

root@ubuntu-machine-1:/# reboot
Connection to < vm-ip > closed by remote host.
.
.<trying after the reboot >
.
.

root@DiscoveringSystems:/# hostname
DiscoveringSystems.ds.com

The hostname command has other options we can use to check either the short version of the hostname or entire FQDN  , by default when executed without any options it will show the FQDN 

# Default 

root@DiscoveringSystems:/# hostname
DiscoveringSystems.ds.com

#FQDN option 

root@DiscoveringSystems:/# hostname -f    
DiscoveringSystems.ds.com

#short option 

root@DiscoveringSystems:/# hostname -s    
DiscoveringSystems

Using “hostnamectl” command to edit the hostname

Instead of editing the /etc/hostname manually , we can use the hostnamectl command to edit the hostname , changing the hostname using the hostnamectl edits the /etc/hostname file to set the new hostname 

To view the detailed info on the current hostname 

root@DiscoveringSystems:/# hostnamectl
 Static hostname: DiscoveringSystems.ds.com
       Icon name: computer-vm
         Chassis: vm
      Machine ID: <masked for privacy>
         Boot ID: <masked for privacy>
  Virtualization: vmware
Operating System: Ubuntu 22.04 LTS
          Kernel: Linux 5.15.0-40-generic
    Architecture: x86-64
 Hardware Vendor: VMware, Inc.
  Hardware Model: VMware Virtual Platform

To explore more options part of the Hostnamectl command 

[root@DiscoveringSystems /]# hostnamectl --help
hostnamectl [OPTIONS...] COMMAND ...

To set the new hostname using the hostnamectl command 

root@DiscoveringSystems:/# hostnamectl set-hostname DS-1.ds.com

root@DiscoveringSystems:/# hostnamectl
 Static hostname: DS-1.ds.com
       Icon name: computer-vm
         Chassis: vm
      Machine ID:  <masked for privacy>
         Boot ID:  <masked for privacy>
  Virtualization: vmware
Operating System: Ubuntu 22.04 LTS
          Kernel: Linux 5.15.0-40-generic
    Architecture: x86-64
 Hardware Vendor: VMware, Inc.
  Hardware Model: VMware Virtual Platform

Now we can see the new hostname is set on the /etc/hostname file , however the change didn’t take effect in the prompt , for it to take effect we either need a new bash session ( log out / login ) or a reboot 

root@DiscoveringSystems:/# hostname
DS-1.ds.com
root@DiscoveringSystems:/# cat /etc/hostname
DS-1.ds.com

<reboot or new bash session to see the changes to the prompt >

root@DS-1:/#
root@DS-1:/#

Leave a Comment

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