hostname


The hostname command is a core utility in RHEL and other Linux systems used to display or, with sufficient privileges, set the system's current host name. This name is crucial for identifying a machine on a network.


Example 1: Display the Current Hostname

# Simply execute the command to see the full hostname
hostname

Explanation: Executing the hostname command without any arguments retrieves and prints the system's Fully Qualified Domain Name (FQDN) to standard output. This is the most common way to quickly identify a server.


Example 2: Display the Short Hostname

# Use the -s or --short flag to display the name before the first dot
hostname -s

Explanation: This command displays the short, or alias, name of the host. It effectively trims the domain part from the FQDN, which is useful for scripts or prompts where the full name is too long.


Example 3: Display the FQDN

# The -f or --fqdn flag explicitly requests the Fully Qualified Domain Name
hostname -f

Explanation: Using the -f flag ensures you get the Fully Qualified Domain Name, which includes both the hostname and the domain name. A correctly configured network DNS resolver is required for this to work reliably.


Example 4: Display the Host's IP Address

# The -i or --ip-address flag resolves the hostname to its network address
hostname -i

Explanation: This command shows the primary network IP address associated with the system's hostname. It relies on the local host file or DNS to resolve the name to an address.


Example 5: Temporarily Change the Hostname

# Use superuser privileges to set a new hostname
sudo hostname rhel-server-temp

Explanation: Executing hostname with a new name as an argument and with sudo privileges changes the system's hostname immediately. However, this change is temporary and will be lost upon rebooting the system.


Example 6: Permanently Change the Hostname in RHEL 8

# Use the hostnamectl utility for a persistent change
sudo hostnamectl set-hostname rhel8-permanent-server.example.com

Explanation: In modern RHEL 8 systems, hostnamectl is the standard tool for managing system hostnames persistently. This command updates all necessary configuration files, ensuring the new name survives a reboot.


Example 7: View Hostname in Solaris/AIX

# The command is identical for viewing the hostname
hostname

# In Solaris, the persistent name is stored in /etc/nodename
cat /etc/nodename

Explanation: While the command to view the current hostname is the same in UNIX variants like Solaris and AIX, the method for persistent storage differs. In Solaris, the hostname is read from the /etc/nodename file at boot.


Example 8: Set Hostname in Debian/Ubuntu

# While hostnamectl is available, a traditional method is editing /etc/hostname
sudo nano /etc/hostname

# And also updating the /etc/hosts file
sudo nano /etc/hosts

Explanation: On Debian-based systems, a common manual method involves directly editing the /etc/hostname file to set the persistent name. It is also critical to update the /etc/hosts file to map the new hostname to the local IP address.