How to Change Hostname on Linux

What is a Hostname and Why Would You Want to Change it?

A hostname is an identifier for a computer or device on a network. It is used to identify the device on the network and can be used to access it remotely. Changing the hostname of a computer or device can be useful for many reasons, such as improving security, making it easier to find and identify devices on the network, or simply personalizing it.

Step 1 – Check the Current Hostname and Set Up a New One

If you want to change your computer name in Ubuntu, the first step is to check the current hostname and set up a new one. This process can be done through a few simple commands that will allow you to make sure that you are using the right hostname for your system. Additionally, it is important to understand how changing the hostname affects other components of your system. By following some basic steps, you can easily check and set up a new hostname in Linux.

Type hostname

# hostname

Output :

oldname.linodelinux.com

Type hostnamectl to verify :

# hostnamectl
hostnamectl
   Static hostname: oldname.linodelinux.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 341e401e827641bda8eac222c3988c65
           Boot ID: 0d2752d42a6a4325aa07f8d92162b852
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-957.27.2.el7.x86_64
      Architecture: x86-64

Step 2 – Modify the /etc/hosts File

To ensure that your computer is accessing the correct IP address for a given domain, you may need to modify the /etc/hosts file. This file allows you to manually specify which IP address should be used when a particular domain name is requested. By updating this file, you can make sure that your computer is always connecting to the right server and not an outdated or incorrect one.

# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.23   server.linodelinux.com

Step 3 – Run hostnamectl set-hostname command

Change hostname on linux by using hostnamectl command :

# hostnamectl set-hostname server.linodelinux.com

Step 4 – Verify Changes to Take Effect

Rebooting your system is an essential step to ensure that the changes you’ve made take effect. Without rebooting, the changes may not be applied and you may experience problems with your computer.

Type hostnamectl to verify the changes :

# hostnamectl
   Static hostname: server.linodelinux.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 341e401e827641bda8eac222c3988c65
           Boot ID: 0d2752d42a6a4325aa07f8d92162b852
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-957.27.2.el7.x86_64
      Architecture: x86-64

Type hostname

# hostname

Output :

server.linodelinux.com

Leave a Comment