Operations 4 min read

How to Quickly Change the Hostname on a CentOS VM

This guide walks you through the step‑by‑step process of changing a CentOS virtual machine's hostname by editing /etc/hostname and /etc/hosts, verifying the changes, and rebooting to apply them, complete with screenshots for each stage.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Quickly Change the Hostname on a CentOS VM

By default a CentOS virtual machine uses localhost or localhost.localdomain as its hostname.

The hostname is stored in the /etc/hostname file. Open it with vi /etc/hostname and replace the existing name with the desired one (e.g., slave2), then save and exit using :wq!.

After editing, verify the new hostname with cat /etc/hostname:

Alternatively, you can modify /etc/hosts to map the VM’s IP address to the new hostname. Edit the file with vi /etc/hosts and add a line containing the IP (e.g., 202.118.86.90) followed by the new hostname:

Confirm the hosts file update with cat /etc/hosts:

To apply the changes, reboot the VM using shutdown -r now:

After the reboot, the hostname will reflect the new name (e.g., slave2):

For consistency and to avoid future confusion, it is recommended to update both /etc/hostname and /etc/hosts with the new hostname.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxtutorialCentOShostname
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.