• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Change Hostname on CentOS 7

Written by Admin, Updated On May 7, 2019
centos, hostname
How to change hostname on CentOS 7

The hostname will be set at the time of installation of CentOS operating system. If you are creating virtual machine then it will be assigned dynamically at the instance creation time. This tutorial helps you to change a hostname on a CentOS 7 system.

Prerequisites#

Before starting, make sure you are logged in with non-root user with sudo privileges.

Change Hostname on CentOS#

By assigning a hostname you can differentiates a system on a network. It is important that you shouldn’t have same hostname on a same network.

There are three types of hostnames as below:

  1. static – This is the most important type of hostname. This is used to identify a particular server between machines on network. It’s stored at /etc/hostname file. For example, TecNStuff’s Server.
  2. pretty – This is more user-friendly and can use more characters and punctuation. It is used to represent the user. This is stored at /etc/machine-info.
  3. transient – A transient hostname is changed in the Linux kernel. It is the same as the static hostname.

It is best practice to use a fully-qualified domain name for both static and transient hostnames like server.tecnstuff.net.

Check Existing Hostname#

Run below command to check the existing hostname for your system:

hostnamectl
Static hostname: local.tecnstuff
Pretty hostname: CentOS7 Server
Icon name: computer-vm
Chassis: vm
Machine ID: 44dddc11e56e403d8053ecdf0b92d462
Boot ID: c299757e3c8145e09eb2d55172189a24
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-957.5.1.el7.x86_64
Architecture: x86-64

As you can see on above output, here current hostname name is local.tecnstuff.

Change the Hostname using hostnamectl#

In CentOS 7 system, we can use hostnamectl command to change the hostname. Following is the basic syntax to change hostname:

sudo hostnamectl set-hostname NEW_HOST_NAME
sudo hostnamectl set-hostname server.yourdomain.com
sudo hostnamectl set-hostname server.yourdomain.com --static
sudo hostnamectl set-hostname "Pretty HostName" --pretty
sudo hostnamectl set-hostname server.yourdomain.com --transient

You can run command as below to change hostname:

sudo hostnamectl set-hostname server.tecnstuff.net

Here we changed hostname from local.tecnstuff to server.tecnstuff.net using hostnamectl command. Now the hostname is server.tecnstuff.net for server, you can check it again using hostnamectl command.

Same as you can set pretty name for server using below command:

sudo hostnamectl set-hostname "TecNStuff's Server" --pretty

Change the Hostname by Edit /etc/hosts file#

You also can change hostname by changing in /etc/hosts file. Open /etc/hosts file using your favorite text editor:

sudo nano /etc/hosts

In file, change the hostname as per you choice at line that begins with 127.0.0.1

Save and close file. For this method you need to restart your CentOS system.

Verify Hostname#

After changed using second method, you can again check hostname using hostnamectl command and now it should output with new hostname as below:

Static hostname: server.tecnstuff.net
Pretty hostname: TecNStuff's Server
Icon name: computer-vm
Chassis: vm
Machine ID: 44dddc11e56e403d8053ecdf0b92d462
Boot ID: c299757e3c8145e09eb2d55172189a24
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-957.5.1.el7.x86_64
Architecture: x86-64

As you can see above the hostname is successfully changed from local.tecnstuff to server.tecnstuff.net and also Pretty hostname changed from CentOS7 Server to TecNStuff's Server.

Conclusion#

You learned how to change the hostname on CentOS 7 using different methods. If you face any issues you can leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Change the SSH Port in Linux Server
Next Article   How to Enable SSH on Ubuntu 18.04 System

Related Posts

  • How to Change Hostname Debian 11

    How to Change Hostname on Debian 11

    February 3, 2023
  • How to Change-Hostname Ubuntu 22.04

    How to Change Hostname on Ubuntu 22.04

    January 19, 2023
  • How to Install Php 8 on CentOS 8

    How to Install PHP 8 on CentOS 8

    January 27, 2021

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook