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

Configure Samba File Share on Debian 13 / 12 Debian Configure Samba File Share on Debian 13 / 12 Setup WireGuard VPN on Ubuntu 24.04 / Debian 13 / Rocky Linux 10 Debian Setup WireGuard VPN on Ubuntu 24.04 / Debian 13 / Rocky Linux 10 Use NetworkManager nmcli on Ubuntu and Debian Debian Use NetworkManager nmcli on Ubuntu and Debian Build FreeRADIUS From Source on RHEL, Ubuntu, and Debian FreeRADIUS Build FreeRADIUS From Source on RHEL, Ubuntu, and Debian Install Trivalent Hardened Browser on Fedora Fedora Install Trivalent Hardened Browser on Fedora Configure SELinux for WordPress Website on CentOS Stream / RHEL CentOS Configure SELinux for WordPress Website on CentOS Stream / RHEL

Leave a Comment

Press ESC to close