How To Change HostName In Linux

We set the hostname while installing the operating system.

By default the system takes the server default name as a hostname. It should be localhost.localdomain

Beginner don’t know the hostname important and they give any name as a hostname.

When he/she installing the operating system, they may simply give distribution name as a hostname.

Say for example, they might give CentOS as a hostname while installing the CentOS in the system.

If so, don’t worry, we can change the hostname at any point of time in Linux.

Navigate to the following URL if you are looking for more options to change hostname in Linux.

Why We Need To Set The Proper Hostname?

As you may aware of that we can’t remember all the IP address of the system so, we need to assign a unique host name for each system.

It will help us to connect the system easily whenever we need because it has the hostname that we can easily remember it.

Most of the software providers requires a valid Fully Qualified Domain Name (FQDN) for the system to verify the License.

So, we need to set the proper and fully qualified domain name as a Hostname of the server to fulfill all the requirements.

It can be any name that you like but keep the below things in mind while setting up the hostname.

We can use alphabet, numbers, special symbol (. and -) and MIN 2 & MAX 64 characters.

How To Change The Hostname In systemd System?

We had written the multiple articles about systemd system manager in the past. systemd systems comes with a handy tool called hostnamectl that allow us to manage the system hostname easily.

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

We can use this command in all the major Linux distributions such as RHEL 7, CentOS 7, Fedora 18+, Ubuntu 16.04+, Debian 8+, LinuxMint, ArchLinux, Manjaro, etc except RHEL 6, CentOS 6 and OEL 6.

How To View/Change The Hostname In systemd System?

Simple 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 would like to change the hostname, use the following command format.

The general syntax:

$ hostnamectl set-hostname [YOUR NEW HOSTNAME]

Use the following command to change the hostname using hostnamectl command. In this example, i’m 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

How To View/Change The Hostname In SysVinit System?

For Older Linux distributions (i mean to say SysVinit system), we need to use the following method.

Run the following command to view the hostname.

$ hostname
magi-laptop

Use the following command to change the hostname temporarily on Linux system.

The general syntax:

$ hostname [YOUR NEW HOSTNAME]

To change the hostname temporarily. Make a note, this change will be revoked once your system reboot.

$ hostname daygeek-Y700

For RHEL 6/CentOS 6/OEL 6 systems, use the following steps to change the hostname.

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

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

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

$ vi /etc/hosts
192.168.0.100 magi-laptop

Finally restart the network service and reboot the system to takes the change effect.

$ sudo service network restart

$ sudo init 6

Run the following command to view the hostname.

$ hostname
magi-laptop

For Debian/Ubuntu based SysVinit system, use the following steps to change the hostname.

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

$ vi /etc/hostname
HOSTNAME=magi-laptop

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

$ vi /etc/hosts
192.168.0.100 magi-laptop

Finally restart the hostname service and reboot the system to takes the change effect.

$ /etc/init.d/hostname restart

$ sudo init 6

Run the following command to view the hostname.

$ hostname
magi-laptop

Enjoy…)

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...

%d bloggers like this: