set or change hostname in linux

How to Set or Change Hostname in CentOS / RHEL

Hostname is basically an identifier for your system on a network, making it easy for others to find your system on the network. There are 3 types of hostnames for a system – static, pretty and transient. In this article, we will learn how to change hostname in CentOS/RHEL.


How to Set or Change Hostname in CentOS / RHEL

Static hostname is the most important, stored in /etc/hostname file, and used by others to find your machine on the network. Pretty hostname allows for more characters, is user-friendly, but not used for machine code. It is stored in /etc/machine-info. Transient hostname is a temporary hostname that is reset on every reboot. In this article, we will look at how to set or change each hostname in CentOS/RHEL.


1. Check Existing Hostname

Open terminal and run the following command to check existing hostname.

$ hostnamectl

The output will contain a few information, including static hostname. On some systems, such as virtual machines, it may directly show only static hostname.


2. Set Static Hostname

Generally, Linux systems support Fully Qualified Domain Names(FQDN) as hostnames. It should consist of :

  • Lower-case letters a – z
  • Numbers 0 – 9
  • Periods and hyphens
  • Hostnames must be have 2 – 63 characters
  • Hostnames must start and end with a number or letter

Here is the syntax to set static hostnames. Replace my.hostname.server with your new hostname.

$ hostnamectl set-hostname my.hostname.server

Now you can verify the change with the following command.

$ hostnamectl


3. Modify /etc/hosts

Alternatively, open /etc/hosts file in a terminal.

$ sudo vi /etc/hosts

Look for the line starting with 127.0.0.1. It will be like

127.0.0.1  localhost localhost.localdomain localhost 4 localhost4.localdomain4 old.hostname

Replace old.hostname with your new hostname that you have used above.

Save and close the file.

Reboot your system and run hostnamectl command to verify the new static hostname.


4. Set Pretty Hostname (Optional)

It is optional to set pretty hostname. However, you can do so by simply adding quotation marks around your new hostname. Here is an example.

$ hostnamectl set-hostname "My Pretty Hostname"

After that, run the following command to verify the change.

$ hostnamectl status

You will see the following output.

Static hostname: mynewhostname
Pretty hostname: My Pretty Hostname


5. Set Transient Hostname (Optional)

Transient host names are system generated and change after every reset. Nevertheless, if you want to change it, you can do so by adding -transient option in hostnamectl command.

$ sudo hostnamectl -transient set-hostname temporary.hostname

You can check the hostname value using hostnamectl or hostnamectl status commands.

Here are couple of alternative commands to set pretty and static hostnames.

$ sudo hostnamectl --prettyset-hostname "Pretty Hostname"
$ sudo hostnamectl --staticset-hostname temporary.hostname

That’s it. In this article, we have learnt how to set hostname in Linux systems.

Also read:

How to Convert Webpage to PDF in Linux
How to Find & Remove Duplicate Files in Linux
How to Shutdown/Reboot Remote Linux
How to Setup DNS Caching Server in Linux
How to Delete Partition in Linux

Leave a Reply

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