How to Change hostname on CentOS 7/8 & Fedora 30/29/28

Cpanel/Whm License $3/mo Plesk License $10/mo Cloudlinux License $5/mo

How can I change hostname in CentOS 7/8 & Fedora 30/29/28?. During your CentOS or Fedora installation, you’re asked to set a hostname for the server. This is okay but changing it after installation is sometimes inevitable. Hostname uniquely identifies a computer in a network.

A server’s hostname can contain letters(a-z, A-Z), digits(0-9), hyphen(), dot(.) but must end with a letter or number. It is recommended to use a descriptive name when assigning hostnames for easy identification of a server/service.

Before you set a hostname, first check existing hostname.

$ hostname -s
cent-01
$ hostname -f
cent-01
$ hostnamectl 
   Static hostname: cent-01
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 596e865e6c594db98c4fd47fdb858138
           Boot ID: 47d2f07eb2ea44b799ae6b0c0ba8600e
    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

Where:

  • -s, –short – Used to print short host name
  • -f, –fqdn, –long – Used to print a long host name (FQDN)

Change hostname with hostnamectl

To set a persistent hostname using hostnamectl command, use the command.

sudo hostnamectl set-hostname <name> --static

See example below.

sudo hostnamectl set-hostname backend-sms-app.mydomain.com --static

Confirm your new hostname.

$ hostnamectl 
   Static hostname: backend-sms-app.mydomain.com
Transient hostname: cent-01
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 596e865e6c594db98c4fd47fdb858138
           Boot ID: 47d2f07eb2ea44b799ae6b0c0ba8600e
    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

This setting will automatically update the /etc/hostname file.

$ cat /etc/hostname 
backend-sms-app.mydomain.com

There are three classes of hostnames that can be set with hostnamectl command.

  • Transient hostname – This is a dynamic hostname managed by kernel and can be changed by DHCP or mDNS server at run time. Use the –transient flag to set a transient hostname.
  • Pretty hostname – As the name suggests, this is a user-friendly UTF8 hostname used to represent a user of a machine. Use –pretty flag to set it.
  • Static hostname – This is stored in /etc/hostname for use at runtime.

Set Pretty hostname.

sudo hostnamectl set-hostname "Computingforgeeks PC" --pretty

Set Transient hostname – Usually same as static hostname.

sudo hostnamectl set-hostname backend-sms-app.mydomain.com --transient

Confirm your settings.

$ hostnamectl 
   Static hostname: backend-sms-app.mydomain.com
   Pretty hostname: Computingforgeeks PC
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 596e865e6c594db98c4fd47fdb858138
           Boot ID: 47d2f07eb2ea44b799ae6b0c0ba8600e
    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

To map your new hostname to IP address, edit the /etc/hosts file and replace old hostname with a new one.

$ sudo vim /etc/hosts
192.168.121.17 backend-sms-app.mydomain.com backend-sms-app

There you have it.

$ ping -c 2 backend-sms-app
PING backend-sms-app.mydomain.com (192.168.121.17) 56(84) bytes of data.
64 bytes from backend-sms-app.mydomain.com (192.168.121.17): icmp_seq=1 ttl=64 time=0.019 ms
64 bytes from backend-sms-app.mydomain.com (192.168.121.17): icmp_seq=2 ttl=64 time=0.040 ms

--- backend-sms-app.mydomain.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.019/0.029/0.040/0.011 ms

Related posts

Former Deutsche Bank Director Joins Blockchain Firm AlphaPoint

SXI ADMIN

University in Cyprus Receives First Tuition Payment in Bitcoin

SXI ADMIN

Overstock’s tØ Acquires Broker Firm for Wall Street Disruption

SXI ADMIN

Kraken Halts USD Deposits, Asks Customers to Switch to Euros

SXI ADMIN

Small Australian City Plans for a Big Bitcoin Economy

SXI ADMIN

Infosys: Blockchain Tech Adoption Won’t Take A Decade

SXI ADMIN

Korean Bitcoin Gambling Site Seeks Asian Customers

SXI ADMIN

Exchanges, Startups Pick Sides After the Bitcoin Cash ‘Hash War’

SXI ADMIN

Nasdaq to Add Bitcoin and Ethereum Indices to Global Data Service

SXI ADMIN

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More