Understanding and Using the hostname Command in Ubuntu


 

Understanding and Using the hostname Command in Ubuntu

Welcome, Ubuntu users! Today, we're diving into a simple yet powerful command: hostname. This command allows you to view and change your computer's hostname, which is essentially its name on a network. Let's break it down.


What is a Hostname?

Think of your hostname as your computer's "nickname." It helps identify your machine on a network, making it easier to communicate with other devices. By default, Ubuntu assigns a hostname during installation, but you can easily change it.


Viewing Your Hostname

To see your current hostname, simply open your terminal and type:

Bash
hostname

This will display the current hostname.


Changing Your Hostname (Temporarily)

To change your hostname temporarily (meaning it will revert after a reboot), use:

Bash
sudo hostname new-hostname


Replace new-hostname with your desired name. For example:

Bash
sudo hostname my-ubuntu-machine


Making Changes Permanent

To make your hostname change permanent, you'll need to edit the /etc/hostname file.

  1. Open the file:

    Bash
    sudo nano /etc/hostname
    
  2. Replace the existing hostname with your desired new hostname.

  3. Save the file (Ctrl+X, Y, Enter).

  4. Update /etc/hosts:

    Bash
    sudo nano /etc/hosts
    

    Find the line that looks like:

    127.0.0.1 your-old-hostname
    

    Change your-old-hostname to your new-hostname. It should look like this:

    127.0.0.1 your-new-hostname
    

    Also verify that the line containing your public IP address also has your new hostname listed.

  5. Save the file (Ctrl+X, Y, Enter).

  6. Reboot your system to apply the changes:

    Bash
    sudo reboot
    


Useful hostname Options

The hostname command also provides some useful options:

  • -i or --ip-address: Displays the IP address(es) associated with the hostname.

    Bash
    hostname -i
    
  • -I or --all-ip-addresses: Displays all IP addresses of all network interfaces. This is useful when you have multiple network connections.

    Bash
    hostname -I
    
  • -f or --fqdn: Displays the Fully Qualified Domain Name (FQDN).

    Bash
    hostname -f
    
  • -s or --short: Displays the short hostname (the part before the first dot).

    Bash
    hostname -s
    


Summary Table

ExampleDescription
hostnameDisplays the current hostname.
hostname -iDisplays the IP address(es) associated with the hostname.
hostname -IDisplays all IP addresses of all network interfaces.
hostname -fDisplays the Fully Qualified Domain Name (FQDN).
hostname -sDisplays the short hostname.


In Conclusion

The hostname command is a simple but essential tool for managing your Ubuntu system's identity on a network. By understanding how to view and change your hostname, you can better manage your system and network connections. Happy Ubuntu-ing!


Need Ubuntu Expertise?

If you need help with your Ubuntu projects or have any questions, feel free to reach out to us!

Email us at: info@pacificw.com


Image:  Gemini

Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison

The Reasoning Chain in DeepSeek R1: A Glimpse into AI’s Thought Process