hostname Command in Linux
Overview
The hostname command in Linux is a powerful tool used to display or set the system's host name. The host name or system name is a label assigned to a device connected to a computer network, serving as a means to identify it in various forms of electronic communication such as networking protocols, email, or website hosting. With the hostname command in Linux, you can fetch the host name or even alter it for system administration purposes.
Syntax of hostname Command in Linux
The basic syntax of the hostname command in Linux is as follows:
Where:
- options: These are optional and used to modify the behavior of the command. Options include -s, -i, -f, etc.
- new_host_name: This is also optional. If specified, it will set the system's hostname to this new value.
Options in hostname Command in Linux
-
-s, --short: Displays the short host name. This is the host name cut at the first dot.
For example -
Output:
This command will display the short hostname, which is 'my-host' in this case.
-
-i, --ip-address: Displays the network address(es) of the host name.
For example -
Output:
This command will display the IP address of the host, which is '192.168.1.10' in this case.
-
-F, --file: Reads the host name from the given file.
For example -
This command will set the hostname of the system to the content of the file /etc/my_hostname.
Example Usages
-
Displaying the host name:
Output:
Explanation: This command displays the full host name of the system.
-
Setting the host name:
Explanation: This command changes the hostname to 'new-host-name'. Note that you may need appropriate permissions to execute this command.
Tips
-
Use 'sudo' before the hostname command if you're making changes and don't have the necessary permissions.
-
Changes made using the hostname command without modifying the configuration files might not persist after a system reboot.
Advanced Use Cases of hostname Command in Linux
-
Displaying the network node hostname:
Output:
Explanation: This command displays the network node hostname 'nodename' which is set in the kernel.
-
Displaying the domain of the system:
Output:
Explanation: This command displays the domain name of the system.
-
Displaying help for the hostname command:
Output:
Explanation: This command displays the help message for the hostname command, showing its syntax and options.
Conclusion
-
The hostname command in Linux is a vital tool for system and network administration.
-
It allows you to display or change the system's hostname.
-
The command also provides several options to display information like the IP address, short host name, and more.
-
While changing the hostname, ensure you have the necessary permissions and modify configuration files to make the change persistent.