How to fix "hostname: command not found" on CentOS/RHEL 8 Linux

I am trying on my CentOS system and got the following error when I run that tool


It cried out:

./ line 141: hostname: command not found

Fixing “hostname: command not found” on CentOS/RHEL 8

I searched for hostname

dnf search hostname

Then I saw it:

Last metadata expiration check: 0:07:51 ago on Thu Jun 10 21:36:55 2021.
============================================ Name Exactly Matched: hostname =============================================
hostname.x86_64 : Utility to set/show the host name or domain name
=========================================== Name & Summary Matched: hostname ============================================
python2-backports-ssl_match_hostname.noarch : The ssl.match_hostname() function from Python 3
=============================================== Summary Matched: hostname ===============================================
GeoIP.x86_64 : Library for country/city/organization to IP address or hostname mapping
python-idna-ssl.noarch : Patch ssl.match_hostname for Unicode(idna) domains support

Getting confirmation

I wanna make sure I was installing what I needed so I typed:

dnf info hostname


Last metadata expiration check: 0:08:03 ago on Thu Jun 10 21:36:55 2021.
Available Packages
Name         : hostname
Version      : 3.20
Release      : 6.el8
Architecture : x86_64
Size         : 32 k
Source       : hostname-3.20-6.el8.src.rpm
Repository   : baseos
Summary      : Utility to set/show the host name or domain name
URL          :
License      : GPLv2+
Description  : This package provides commands which can be used to display the system's
             : DNS name, and to display or set its hostname or NIS domain name.


Finally, I installed the hostname utility to set/show the host name or domain name on my CentOS 8. Execute:

dnf install hostname

Testing hostname



Adding hostname command is easy with the help of the dnf command on your CentOS and RHEL when needed on demand.


I fixed formatting and converted messed-up text with an image.

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki