hostname Command in Linux
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
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.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
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.
Turn Learning into Career Growth
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.




