How To

Set system hostname on CentOS / RHEL / Rocky / Alma

How can I set or change hostname in CentOS / RHEL / Rocky / AlmaLinux system? During your OS 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.

Original content from computingforgeeks.com - post 20806
set hostname centos fedora

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
....

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
....

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
    ....

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 content:

Keep reading

How to Upgrade Fedora 44 to Fedora 45 (GUI, CLI and Rollback) Fedora How to Upgrade Fedora 44 to Fedora 45 (GUI, CLI and Rollback) Enable RPM Fusion and Install Multimedia Codecs on Fedora 45 / 44 / 43 Fedora Enable RPM Fusion and Install Multimedia Codecs on Fedora 45 / 44 / 43 Free CCNA 200-301 Practice Questions with Answers Networking Free CCNA 200-301 Practice Questions with Answers Homelab and Sysadmin Gifts by Budget: $25, $50, $100 and $250+ Networking Homelab and Sysadmin Gifts by Budget: $25, $50, $100 and $250+ Switch Fedora From GRUB to systemd-boot With bootctl Fedora Switch Fedora From GRUB to systemd-boot With bootctl Install FFmpeg on Fedora 45 / 44: RPM Fusion vs ffmpeg-free Fedora Install FFmpeg on Fedora 45 / 44: RPM Fusion vs ffmpeg-free

Leave a Comment

Press ESC to close