/etc/hostname and /etc/HOSTNAME
The files /etc/hostname and /etc/HOSTNAME are used to configure the system's hostname in Unix-like operating systems. The location and usage of these files can vary between different Linux distributions. Here's an overview of these files and how they are used:
/etc/hostname
This file is commonly used in Debian-based distributions (such as Debian, Ubuntu, and their derivatives) to set the system's hostname.
Contents of /etc/hostname
The /etc/hostname file typically contains a single line with the hostname of the system. For example:
myhostnameIn this example, myhostname is the name of the system.
Setting the Hostname
Edit the /etc/hostname File:
sudo nano /etc/hostnameEnter the desired hostname and save the file.
Apply the New Hostname: To apply the new hostname without rebooting, use the
hostnamecommand:sudo hostname -F /etc/hostnameVerify the Hostname:
hostname
Additional Configuration
It is also good practice to update the /etc/hosts file to map the new hostname to 127.0.1.1. For example:
127.0.0.1 localhost
127.0.1.1 myhostname/etc/HOSTNAME
This file is typically used in Red Hat-based distributions (such as RHEL, CentOS, Fedora, and their derivatives) to set the system's hostname.
Contents of /etc/HOSTNAME
The /etc/HOSTNAME file also contains a single line with the hostname of the system. For example:
myhostnameSetting the Hostname
Edit the /etc/HOSTNAME File:
sudo nano /etc/HOSTNAMEEnter the desired hostname and save the file.
Apply the New Hostname: To apply the new hostname without rebooting, use the
hostnamectlcommand:sudo hostnamectl set-hostname myhostnameVerify the Hostname:
hostname
Additional Configuration
It is also good practice to update the /etc/hosts file to map the new hostname to 127.0.0.1 or 127.0.1.1. For example:
127.0.0.1 localhost
127.0.1.1 myhostnameKey Differences and Usage
File Locations:
Debian-based systems:
/etc/hostname.Red Hat-based systems:
/etc/HOSTNAME.
Hostname Commands:
Debian-based systems:
hostname -F /etc/hostname.Red Hat-based systems:
hostnamectl set-hostname <hostname>.
Configuration Syntax:
Both files use a single line to specify the hostname.
Conclusion
The /etc/hostname and /etc/HOSTNAME files are essential for setting the system's hostname in Unix-like operating systems. Understanding their locations and usage in different Linux distributions helps in managing and configuring hostnames effectively. Remember to update the /etc/hosts file to ensure proper hostname resolution.
Last updated