How to Change HostName in Linux

You generally set the hostname when installing the operating system.

By default the system takes the server’s default name as the hostname.

This will be localhost.localdomain.

any name as the hostname. When they install the operating system, they can simply give the Distribution Name as the hostname.

For example, when installing CentOS on the machine they may give CentOS as the hostname.

If so, don’t worry, you can change the hostname at any time on Linux.

If you’re looking for more options to change the hostname on Linux, go to the following URL.

Why Do We Need to Set the Proper Hostname?

You may know that you can’t remember all the IP addresses of the system, so you need to assign a unique host name to each computer to make it easier to remember.

This will help you to easily connect to the system whenever you need to access them, because you can easily remember the hostname.

Most software providers require a valid Fully Qualified Domain Name (FQDN) for the system to verify the license.

Therefore, a valid and fully qualified domain name must be set as the server’s hostname to meet all requirements.

This may be any name you want, but keep in mind the following things when setting up the hostname.

Most environments have the following structure and this is typical setup of the IT infrastructure.

  • DEV – Development Environment
  • TEST – Test Environment
  • QA/UAT – QA/UAT Environment
  • PROD – Production Environment

Servers may be different from its type. There are three types of servers.

  • P – Physical Server
  • V – Virtual Server (VM)
  • C – Cloud Server

Additionally, the host name can use characters, numbers, special symbols (. And -) and MIN 2 and MAX 64 characters.

1) How to View/Change the Hostname on systemd Systems

We have written many articles in the past about systemd System Manager. systemd system comes with a simple tool called “hostnamectl” which allows you to easily manage the system hostname.

hostnamectl may be used to query and change the system hostname and related settings in Linux.

This command can be used on all major Linux distributions such as RHEL 8/7, CentOS 8/7, Fedora 18+, Ubuntu 16.04+, Debian 8+, LinuxMint, Arch Linux and Manjaro, except RHEL 6, CentOS 6 and OEL 6.

Simply run the hostnamectl command to view the system hostname.

$ hostnamectl
or
$ hostnamectl status

   Static hostname: daygeek-Y700
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 31bdeb7b83230a2025d43547368d75bc
           Boot ID: 267f264c448f000ea5aed47263c6de7f
  Operating System: Manjaro Linux
            Kernel: Linux 4.19.20-1-MANJARO
      Architecture: x86-64

If you want to change the hostname, use the following command format.

The general syntax:

$ hostnamectl set-hostname [YOUR NEW HOSTNAME]

Use the hostnamectl command to change the hostname. In this example, we are going to change the hostname from “daygeek-Y700” to “magi-laptop”.

$ hostnamectl set-hostname magi-laptop

You can view the updated hostname by running the following command.

$ hostnamectl
   Static hostname: magi-laptop
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 31bdeb7b83230a2025d43547368d75bc
           Boot ID: 267f264c448f000ea5aed47263c6de7f
  Operating System: Manjaro Linux
            Kernel: Linux 4.19.20-1-MANJARO
      Architecture: x86-64

2) How to View/Change the Hostname on SysVinit System

For Older Linux distributions (I mean to say SysVinit system), you should use the following method.

Run the following command to view the hostname.

$ hostname
magi-laptop

To temporarily change the hostname on a SysVinit machine, use the following command.

The general syntax:

$ hostname [YOUR NEW HOSTNAME]

As we said, this is a temporary change. Therefore, this change will be revoked once your system is restarted. Use the following steps 2a and 2b to make them permanent.

$ hostname daygeek-Y700

2a) How to View/Change the Hostname on RHEL 6/CentOS 6/OEL 6 System

RHEL 6 based systems use the SysVinit System Manager, so use the following steps to change the hostname.

First we need to modify the hostname in the /etc/sysconfig/network file.

$ vi /etc/sysconfig/network
HOSTNAME=magi-laptop

Next we need to change the hostname in the /etc/hosts file.

$ vi /etc/hosts
192.168.0.100 magi-laptop

Finally restart the network service and restart the system for the changes to take effect.

$ sudo service network restart

$ sudo init 6

Run the following command to view the hostname.

$ hostname

magi-laptop

2b) How to View/Change the Hostname on Debian/Ubuntu System

Old Debian/Ubuntu based systems use the SysVinit System Manager, so use the following steps to change the hostname.

First we need modify the hostname in the /etc/hostname file.

$ vi /etc/hostname
HOSTNAME=magi-laptop

Next we need to modify the hostname in the /etc/hosts file.

$ vi /etc/hosts
192.168.0.100 magi-laptop

Finally restart the network service and restart the system for the changes to take effect.

$ /etc/init.d/hostname restart

$ sudo init 6

Run the following command to view the hostname.

$ hostname
magi-laptop

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...