Overview
hostname is a utility used to check or change the system's hostname. The hostname uniquely identifies the system on the network and is used by services like DNS (Domain Name System) to locate the system.
Key Functions
- Check the current system hostname
- Check the FQDN (Fully Qualified Domain Name)
- Check the system's IP address
- Temporarily set the system hostname (requires root privileges)
Key Options
The hostname command allows you to query information related to the hostname using various options.
Information Retrieval
Generated command:
Try combining the commands.
Description:
`hostname` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Various usage examples of the hostname command.
Check Current Hostname
hostname
Prints the current hostname of the system.
Check FQDN (Fully Qualified Domain Name)
hostname -f
Prints the Fully Qualified Domain Name of the system.
Check System's IP Address
hostname -i
Prints the IP address assigned to the system.
Temporarily Change Hostname
sudo hostname my-new-host
Temporarily changes the system's hostname to 'my-new-host'. This change will be reset upon reboot.
Tips & Notes
Points to note and useful tips when using the hostname command.
Permanent Hostname Change
Hostnames set with the hostname command are reset upon system reboot. To permanently change the hostname, you need to modify different configuration files depending on your Linux distribution.
Permanent Hostname Change (Debian/Ubuntu Based)
echo "new-hostname" | sudo tee /etc/hostname\nsudo systemctl restart hostname.service
Modify the /etc/hostname file and restart the hostname service.
Permanent Hostname Change (CentOS/RHEL Based)
sudo hostnamectl set-hostname new-hostname
Use the hostnamectl command to permanently change the hostname.
Configuring the `/etc/hosts` File
When the system's hostname is changed, it's recommended to add the new hostname to the 127.0.0.1 (loopback address) entry in the `/etc/hosts` file. This ensures the local system correctly recognizes itself, which may be necessary for the proper functioning of some applications.