Operations 4 min read

How to Quickly Change the Hostname in CentOS VMs

Learn step‑by‑step how to modify a CentOS virtual machine’s hostname by editing /etc/hostname and /etc/hosts, verifying changes with cat, and restarting the system to apply the new name, with clear screenshots and tips for avoiding common pitfalls.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Quickly Change the Hostname in CentOS VMs

Some CentOS virtual machines have a long default hostname (e.g., localhost.localdomain) that users may want to simplify.

In CentOS, the hostname is stored in the /etc/hostname file. Follow these steps to change it:

Log into the VM and run vi /etc/hostname to open the file. Replace the existing name with the desired one (e.g., slave2).

After editing, press Esc and type :wq! to save and exit. Verify the change with cat /etc/hostname.

Optionally edit /etc/hosts to map the new hostname to the VM’s IP address. Open the file with vi /etc/hosts and add a line such as 202.118.86.90 slave2.

Check the /etc/hosts file with cat /etc/hosts to ensure the entry is correct.

Reboot the VM so the new hostname takes effect: shutdown -r now.

After the reboot, verify that the hostname has changed (e.g., to slave2).

For consistency and to avoid future confusion, it is recommended to update both /etc/hostname and /etc/hosts when changing a CentOS VM’s 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.