Change Hostname Permanently on CentOS7

28/12/2020
The hostname of a system can be used to make it easier to identify the host and not rely on an IP address which is difficult to remember.  There are numerous ways to change the hostname, and some of them will not last through a system reboot.  In this article, I will show you how to change the hostname of CentOS 7 permanently. Let’s get started.

Option 1 – Changing the Hostname Using hostnamectl

You can run the following command to check for the current hostname of your CentOS 7 machine:

$ hostname

As you can see from the screenshot below, the current hostname of my CentOS 7 server is linuxhint

You can use hostnamectl command to change the hostname of your CentOS 7 machine as follows:

$ sudo hostnamectl set-hostname NEW_HOSTNAME

Let’s change it from linuxhint to linuxmint for fun with the following command!

$ sudo hostnamectl set-hostname linuxmint

The hostname should be changed to linuxmint. You can verify that it changed with the following command:

$ hostname

As you can see the hostname is changed to linuxmint!

If you log out and log back in to your CentOS 7 machine or restart it, the changes should be applied system wide.

Option 2 – Changing the Hostname using nmtui:

nmtui command line program is available in NetworkManager-tui package. The NetworkManager-tui package is available in the official package repository of CentOS 7.

To install NetworkManager-tui, run the following command:

$ sudo yum install NetworkManager-tui

Press ‘y’ and then press <Enter> to continue.

NetworkManager-tui should be installed.

Now you can run the following command to start nmtui Terminal based graphical interface:

$ sudo nmtui

You should see the following window as shown in the screenshot. Now press <Down> arrow key a few times to select the marked option “Set system hostname” and press <Enter>

Once you press <Enter>, you should see the following window as shown in the screenshot below.

You can just type in a new hostname in the textbox and press <OK>.

I am going to change the hostname from linuxmint to linuxhint again.

Once you press <Enter>, you should see the following window. Press <Enter> again.

Now select Quit and press <Enter>. The hostname should be changed.

You can verify that the hostname actually changed with the following command:

$ hostname

As you can see from the screenshot below that the hostname is changed from linuxmint to linuxhint. Neat! A full system restart may be required for the changes to take effect system wide.

Fixing Hostname Resolution

By default, when you change hostname, it won’t resolve automatically to localhost or 127.0.0.1

To fix that, open the /etc/hosts file with the following command:

$ sudo nano /etc/hosts

The file should be opened.

Now add the following line as marked in the screenshot below and save it.

Now you can ping your newly set hostname as well.

So that’s how you change the hostname of CentOS 7 permanently. Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Tìm hiểu về công nghệ Livestream

Livestream hay streaming video ngày nay đã là những thuật ngữ không còn xa lạ trong đời sống hàng...
30/12/2020

CÀI ĐẶT VÀ CẤU HÌNH XAMPP TRÊN CentOS 7/8

Xampp là chương trình tạo máy chủ Web (Web Server) được tích hợp sẵn Apache, PHP, MySQL, FTP Server, Mail...
24/12/2020

LDAP [Phần 2] – Triển khai dịch vụ LDAP trên Onet Onet

Trong phần trước, Onet đã hướng dẫn các bạn triển khai LDAP trên CentOS 7. Trong bài này, mình sẽ...
30/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

SỰ KHÁC BIỆT GIỮA RESIDENTIAL PROXY VÀ PROXY DATACENTER
17/02/2024

Mua Proxy v6 US Private chạy PRE, Face, Insta, Gmail
07/01/2024

Mua shadowsocks và hướng dẫn sữ dụng trên window
05/01/2024

Tại sao Proxy Socks lại được ưa chuộng hơn Proxy HTTP?
04/01/2024

Mua thuê proxy v4 nuôi zalo chất lượng cao, kinh nghiệm tránh quét tài khoản zalo
02/01/2024