Change hostname on Centos (CLI)


If you forgot to set the hostname at the installation process, it’s little bit tricky to change it using shell(CLI). This blog post will teach you how to change hostname using command line.

Prerequisite
1. You have root password/sshkey or sudo user’s password

Step 01
Check current hostname using hostnamectl. The value display under “Static hostname” is your current hostname of the system.

hostnamectl

Step 02
Change hostname using the following syntax. (In the sample snapshot, I change my hostname to ansible.) You will have to type in your sudo user password if you are not the root user.

sudo hostnamectl set-hostname <hostname>

Step 03
Check current hostname again with the command in Step 01. The hostname should have been changed.

Step 04
The system need a reboot to fully apply the change. Reboot the system with following command. You also need your sudo user password as we did in Step 02.

sudo reboot

Step 05
After the system reboot, we can check hostname again with command in Step 01. And we also notice that the prompt charged. That means we have the system has applied the setting we just made.

,

Leave a Reply

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