hostname

system administrationLinux/Unix
The hostname command is one of the most frequently used commands in Linux/Unix-like operating systems. hostname Show or set the system hostname

Quick Reference

Command Name:

hostname

Category:

system administration

Platform:

Linux/Unix

Basic Usage:

hostname [options] [arguments]

Common Use Cases

    Syntax

    hostname [options] [name]

    Options

    Option Description
    -a, --alias Display the alias name of the host (if used)
    -d, --domain Display the DNS domain name
    -f, --fqdn, --long Display the FQDN (Fully Qualified Domain Name)
    -i, --ip-address Display the IP address(es) of the host
    -I, --all-ip-addresses Display all network addresses of the host
    -s, --short Display the short hostname
    -y, --yp, --nis Display the NIS/YP domain name
    --help Display help information and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the hostname command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    hostname
    Display the current hostname.
    sudo hostname new-host-name
    Set the system hostname to "new-host-name".
    # Advanced Examples Advanced
    hostname -I Display all network addresses of the host. hostname -i Display the IP address of the hostname. hostname -f Display the FQDN (Fully Qualified Domain Name). hostname -d Display the DNS domain name. hostname -s Display the short hostname.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The hostname command displays or sets the system's host name, which is a label assigned to a device on a network that identifies it on that network. This command is essential for network configuration and identification in Unix and Linux systems. Key features of hostname: 1. System Identification: The primary function of hostname is to show the current name of the system, which is crucial for network identification and communication. 2. Hostname Setting: When run with root privileges and a name argument, hostname can change the system's host name, although this change is typically temporary and doesn't persist after a reboot unless system configuration files are updated. 3. Network Information Display: Various options allow hostname to display different aspects of the system's network identity, such as IP addresses, domain names, and FQDNs (Fully Qualified Domain Names). 4. FQDN Support: The -f option displays the system's Fully Qualified Domain Name, which includes both the hostname and the domain (e.g., server1.example.com). 5. Multiple Address Display: The -I option displays all network addresses assigned to the host, which is useful in multi-homed systems or systems with both IPv4 and IPv6 addresses. 6. Domain Extraction: Options like -d can extract just the domain portion of the FQDN, which is useful in scripts that need to determine the network domain. 7. Integration with DNS: hostname works in conjunction with the system's DNS configuration to provide complete host identification information. In modern Linux systems, persistent hostname changes are typically made by modifying system configuration files (like /etc/hostname) or using dedicated tools like hostnamectl in systemd-based distributions. The hostname command itself is still widely used for querying the current hostname and for temporary changes. The hostname is a critical part of network configuration, affecting how the system is identified on the network, how other systems can reach it, and how various services function, especially those that rely on proper hostname resolution.

    Related Commands

    These commands are frequently used alongside hostname or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the hostname command works in different scenarios.

    $ hostname
    View All Commands