hostname Command in Linux

Learn via video courses
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

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.
Sharpen Your Fundamentals with Free Learning

Options in hostname Command in Linux

  1. -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.

  2. -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.

  3. -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.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.