Logo

The hostname Command in Linux: 15 Practical Examples

Try this guide with our instant dedicated server for as low as 40 Euros

hostname command in linux

Imagine trying to remember and use a long string of numbers to identify every system on a network. 

Fortunately, instead of simply relying on IP addresses, sysadmins assign device hostnames. Every machine on the network (even the one you are working on) has a unique name (called hostname) that identifies it. 

Almost all Linux distributions provide the hostname command that you can use while adding new nodes to the network, configuring machines, or troubleshooting network issues. The command offers a simple yet versatile way of identifying devices on a network. 

The hostname command in Linux lets users see or change the system’s domain and hostname or determine the system’s IP address.

In this tutorial, we will discuss the hostname command and its use cases. We will start with an introduction to the hostname command and then go into the details of the use cases.

Table Of Contents

  1. What is the hostname Command?
  2. 15 Use Cases of the hostname Command
    1. Use Case #1: Display Hostname
    2. Use Case #2: Display Abbreviated Hostname
    3. Use Case #3: Display Alias
    4. Use Case #4: Search for Hostnames
    5. Use Case #5: Force Default Hostname
    6. Use Case #6: Display Domain Name
    7. Use Case #7: Display FQDN
    8. Use Case #8: Display All FQDNs
    9. Use Case #9: Display NIS Domain
    10. Use Case #10: Change the NIS Domain Name
    11. Use Case #11: Display All Network Addresses
    12. Use Case #12: Display Related Network Addresses
    13. Use Case #13: Change Hostname Permanently
    14. Use Case #14: Change Hostname Until Next Reboot
    15. Use Case #15: Additional hostname Options
  3. Conclusion
  4. FAQs

What is the hostname Command?

The hostname command is a fundamental utility in Unix-like operating systems that allows users to view or set the hostname of a machine. 

The hostname is a unique identifier assigned to a device on a network, making it easier to recognize and communicate with specific systems.

By default, the hostname of a system is set during the installation of the OS. Even if a virtual machine is installed, it is dynamically assigned a hostname by the system. 

The Basic Syntax

The basic syntax of the hostname is as follows:

# hostname [options] [new_hostname]

[options]: These are optional flags or parameters that modify the behavior of the command.

[new_hostname]: Represents the new hostname you want to set it up.

Options

The following table summarizes some of the options used in the hostname command.

examples of hostname command

Now that you have a basic understanding of the hostname command, let us understand some of its use cases. However, before that, let us take a quick look at the prerequisites. 

The Prerequisites

Before you dive into the use cases, ensure you have the following.

  • A system running a mainstream Linux distribution.
  • A user account with sudo or administrative privilege.

15 Use Cases of the hostname Command

Linux hostname command allows users to set and view the hostname of the system. 

Some of the use cases of hostname command involve using the command to discover the system name and related information.

Use Case #1: Display Hostname

If you execute the hostname command without any options, you will see the current hostname.

# hostname

 

hostname

Use Case #2: Display Abbreviated Hostname 

We recommend using the -s or –short option to view the hostname without the domain name portion. This is a quick way of viewing the hostname of the machine:

# hostname -s

# hostname --short

hostname -s

Use Case #3: Display Alias

Use the -a or –alias option to show the alias (substitute hostname) of the host (if it has one). Note that if this value hasn’t been set, you may not see any output.

We do not recommend this option as hostname aliasing isn’t a widely used practice in modern Linux systems.

# hostname -a

# hostname --alias

hostname -a

Use Case #4: Search for Hostnames

The hostname command offers the -F or –file option to set your system’s hostname based on the contents of a specified file. 

However, we do not recommend this approach as modifying the hostname can affect network configurations and potentially introduce security risks. 

# sudo hostname -F [file path]

# sudo hostname --file [file path]

Replace [file path] with the actual path to the file you want to read.

sudo hostname -F

Note: If you plan to use this option, make sure you verify the contents of the files and their implications.

Use Case #5: Force Default Hostname

If you prefer not to use a specific file to store the hostname or need to keep the file empty, use the -b or –boot option. Until you choose to change it, the command forces your system to use the default hostname.

The specific default hostname can vary depending on your Linux distribution. It’s often localhost, but it could also be something more generic like ubuntu or debian.

# hostname -b

# hostname --boot

hostname -b

Use Case #6: Display Domain Name

We recommend you to use the -d or –domain  to display the domain name portion of the system’s Fully Qualified Domain Name (FQDN).

An FQDN is the complete hierarchical name assigned to a system on a network. It typically consists of three parts:

  • Hostname: The unique name of the computer itself.
  • Domain name: The domain or subdomain the computer belongs to.
  • Top-Level Domain (TLD): The suffix that denotes the category of the domain.

# hostname -d

# hostname –domain

Use Case #7: Display FQDN

As mentioned earlier, an FQDN is the complete hierarchical name assigned to a system on a network.

To view the Fully Qualified Domain Name (FQDN) of a system, use -f or –fqdn or –long option.

# hostname -f

# hostname --fqdn

# hostname --long

Note that both the DNS domain name and the short hostname are part of a FQDN.

Use Case #8: Display All FQDNs

The -A or –all-fqdns option lists all possible FQDNs associated with a system. However, you should consider that these options can display duplicate entries.

A system can have multiple network interfaces (e.g., Wi-Fi, Ethernet). Each interface usually has a different IP address. The -A or –all-fqdns flag attempts to translate each IP address to a hostname (if possible) and then builds the corresponding FQDN using the domain name.

To list all FQDNs, run one of the following commands:

# hostname -A

# hostname --all-fqdns

It is important to note that this option ignores any addresses it is unable to translate and displays all of your network addresses together with their DNS domain names.

Note: Different network addresses might translate to the same DNS domain names, which can lead to duplicate entries when using the -A or –all-fqdns options.

Use Case #9: Display NIS Domain

We recommend the -y, –yp, or –nis option to display the Network Information Service (NIS) domain name associated with your system. NIS is a useful protocol that allows centralized management of user accounts and other system information across multiple machines on a network.

You can run one of these commands to display NIS domain information:

# hostname -y

# hostname --yp

# hostname --nis

hostname -y

Note: This example is only relevant if your system is specifically configured to use NIS for authentication and resource sharing.

Use Case #10: Change the NIS Domain Name

To modify the name of your NIS domain, use one of the following commands:

# sudo hostname -y [NIS hostname]

# sudo hostname --yp [NIS hostname]

# sudo hostname --nis [NIS hostname]

sudo hostname -y redswitches

Use Case #11: Display All Network Addresses

If you need to identify all IP addresses associated with your system for network configuration or troubleshooting or want a more comprehensive overview of your system’s network presence, use the -I or –all-ip-addresses option. This is often a faster solution than -i as it does not rely on hostname resolution.

You can run one of these commands in the terminal:

# hostname -I

# hostname --all-ip-addresses

hostname -I

Use Case #12: Display Related Network Addresses

If you need to display the IP address associated with your system’s hostname, use -i or –ip-address option. The syntax for these commands are as follows:

# hostname -i

# hostname --ip-address

This command will only work if the hostname can be looked up or resolved.

hostname --ip-address

Use Case #13: Change Hostname Permanently

Changing a hostname is a straightforward process, but it has significant implications for network management, security, and usability. Ensuring that hostnames are meaningful, unique, and compliant with organizational standards is crucial for maintaining an efficient and secure computing environment.

We recommend following this process:

Open the /etc/hostname file in your favorite text editor. We will run the following command to open it in Nano:

# sudo nano /etc/hostname

Change the host name and save the file.

Next, open the /hosts file and change the values accordingly:

# sudo nano /etc/hosts

Save the changes (Ctrl+O) and exit the editor (Ctrl+X).

Alternatively, we suggest the following hostnamectl command syntax to alter the hostname permanently:

# sudo hostnamectl set-hostname [new hostname]

sudo hostnamectl set-hostname

Use Case #14: Change Hostname Until Next Reboot

The hostname command offers a straightforward way to modify your system’s hostname, but the change is temporary and persists only until the next reboot.

You can use the following command to change the hostname temporarily:

# sudo hostname [new hostname]

sudo hostname [new hostname]

Use Case #15: Additional hostname Options

The hostname command provides a few helpful options beyond displaying or changing the hostname.

To see the version of hostname service software package on your Linux system, use the -V or –version option:

# hostname -V

# hostname --version

hostname -V

If you need assistance with any of the commands, use the -h or –help option to display a help message:

# hostname -h

# hostname --help

hostname -h

Conclusion

The hostname command in Linux is a versatile tool for viewing and changing your system’s hostname and domain information. By using the alternative methods available, you can perform a wide range of tasks, from displaying the actual hostname to listing all network addresses. 

You can administer your Linux system more successfully if you know how to use this command. With the examples provided, you should now be equipped to use the hostname command confidently in your own Linux environment.

FAQs

Q. What is the hostname command used for?
The hostname command is used to view or change your Linux system’s hostname.

Q. How do I find the current hostname of my system?
Use the hostname command without any options to display the current hostname.

Q. How can I change the hostname temporarily?
Use hostname new_hostname to change the hostname temporarily until the next reboot.

Q. Where is the hostname stored for permanent changes?
The hostname is stored in configuration files like /etc/hostname and /etc/hosts.

Q. How do I display the short version of the hostname?
Use the hostname –short command to display the short version of the hostname.

Q. What is the command to see all network addresses?
Use hostname -I to display all network interface addresses.

Q. How can I check the fully qualified domain name (FQDN)?
Use hostname –fqdn to display the fully qualified domain name.

Q. What is the role of the sudo command in changing the hostname?
Use the sudo command with hostnamectl to change the hostname, as it requires administrative privileges.

Q. How do I check the version of the hostname software package?
Use hostname -V to display the version of the hostname software package.

Q. What is a persistent hostname?
A persistent hostname is a hostname that remains the same even after the system reboots, typically set in the hostname file and other configuration files.

Try this guide with our instant dedicated server for as low as 40 Euros