hostname
hostname
The hostname command in Linux is used to display or set the system's hostname, which is a unique identifier assigned to a machine in a network. The hostname is used to identify the machine within a network and is essential for network communication and administration tasks.
Basic Usage
hostname [options] [new_hostname]options: Optional flags to modify the command's behavior.
new_hostname: The new hostname you want to set for the machine.
Common Options and Examples
Display the Current Hostname:
hostnameThis command displays the current hostname of the system.
Set a New Hostname:
sudo hostname new_hostnameThis command sets the system's hostname to
new_hostname. Note that you need superuser privileges to change the hostname.Display the Short Hostname:
hostname -sThis command displays the short hostname (up to the first dot).
Display the FQDN (Fully Qualified Domain Name):
hostname -fThis command displays the system's fully qualified domain name (FQDN).
Display the Alias Names:
hostname -aThis command displays the alias names of the host.
Display the IP Address:
hostname -iThis command displays the IP address associated with the hostname.
Display the DNS Domain Name:
hostname -dThis command displays the DNS domain name of the system.
Display All Hostnames:
hostname -AThis command displays all configured hostnames.
Example Usage
Check the Current Hostname:
$ hostname myhostnameChange the Hostname Temporarily:
$ sudo hostname newhostnameThis change will last until the next reboot.
Set the Hostname Permanently: To set the hostname permanently, you need to edit the
/etc/hostnamefile and the/etc/hostsfile.$ sudo nano /etc/hostnameChange the content to the new hostname and save the file.
$ sudo nano /etc/hostsUpdate the line that starts with
127.0.1.1to match the new hostname:127.0.1.1 newhostnameAfter making these changes, apply them by running:
$ sudo systemctl restart systemd-logind.service
Practical Applications
Network Configuration: Setting a hostname is essential for identifying machines in a network, facilitating easier communication and management.
Server Management: Hostnames help in managing multiple servers and services, making it easier to identify and configure them.
Troubleshooting: Knowing the hostname can assist in diagnosing network issues and identifying the source of problems.
Conclusion
The hostname command is a straightforward yet essential tool for managing and configuring the system's network identity. For detailed information, you can refer to the man page:
man hostnamehelp
Last updated