Change hostname linux | How to Change the Hostname of a Linux System

Change Hostname Linux System

Mostly we will set the hostname of a system during the installation process. Many people don’t care about this, and they don’t change hostname linux even if for example this was set to something really stupid by the datacenter that installed the system (most likely they will set this to “debian” on any debian installation, etc). For me, it is important to see on each one of the ssh screens I will have open at any time a different hostname that is relevant and will give me quickly the information on what system I am using or logged in.

How to Change the linux hostname on a running system

On any Linux system you can change its hostname with the command ‘hostname’ Here are some usages of the hostname command:

# hostname New_hostname

 

It will set the hostname of the system to New_hostname. This is active right away and will remain like that until the system will be rebooted (because at system boot it will set this from some particular file configurations – see bellow how to set this permanently). You will most probably need to exit the current shell in order to see the change in your shell prompt.

To Check hostname of the system use following command

# hostname

without any parameter it will show the current hostname of the system.

 

To Check ip of host use following command

# hostname –i

 

To check fully qualified domain name.

# hostname –fqd

Using this command it will show the fully qualified domain name (or FQDN) of the system.

Permanent hostname change on RedHat based systems

RedHat based system use the file /etc/sysconfig/network to read the saved hostname at system boot. This is set using the init script /etc/rc.d/rc.sysinit

/etc/sysconfig/network is a file which keeps the information about the hostname assigned to the system. If you want to change the hostname permanently, you need to change the hostname in this file.

# cat /etc/sysconfig/network

 

So edit this file and enter the appropriate name using the HOSTNAME variable and REBOOT the system then your hostname changes will be applied on system.

Use sysctl to change the hostname

Why would someone need a different method of doing the same thing as above? No idea, but here is anyway: use sysctl to change the variable kernel.hostname: Use:

to read the current hostname

# sysctl kernel.hostname

and

To change hostname.

# sysctl kernel.hostname=NEW_HOSTNAME

How to Permanent hostname change on Debian based systems

Debian based systems use the file /etc/hostname to read the hostname of the system at boot time and set it up using the init script /etc/init.d/hostname.sh

TO check hostname use following command.

# /etc/hostname server

So on a Debian based system we have to edit the file /etc/hostname and change the name of the system and then run the following command:

# /etc/init.d/hostname.sh start

To make the change active. We have to reboot the system (and will be set using the same script we used hostname.sh)

 

I hope you enjoyed reading this article …..

Keep studing…

Leave a Reply

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