Linux hostname Command

Linux 命令大全Complete Linux Command Reference


What is hostname

A hostname is a unique name that identifies a computer on a network. Just as every person has their own name, every computer also needs a unique name to be identified on the network.

In Linux systems, hostname is used to:

  • Identify the local computer
  • Identify devices during network communication
  • Affect the operation of certain network services
  • Display in the terminal prompt

Hostnames are generally divided into three types:

  1. Static hostname: Stored in the configuration file, remains unchanged after a system reboot
  2. Transient hostname: Temporarily set at runtime, becomes invalid after reboot
  3. Pretty hostname: A descriptive name that can contain special characters (such as "My Laptop")

Basic Syntax of the hostname Command

The basic usage format of the hostname command is as follows:

hostname [选项] [新主机名]

If executed directly without any parameters, thehostnamecommand will display the hostname of the current system:

Examples

$ hostname
ubuntu-server

Common Options and Parameter Descriptions

The hostname command supports multiple options to view and set different types of hostnames:

Option Description
-a, --alias Display the alias of the host (if one has been set)
-A, --all-fqdns Display the FQDN (Fully Qualified Domain Name) of all network interfaces
-b, --boot If the hostname is not set, use the default hostname
-d, --domain Display the DNS domain name
-f, --fqdn, --long Display the FQDN (Fully Qualified Domain Name)
-F, --file Read the hostname from the specified file
-i, --ip-address Display the IP address of the host
-I, --all-ip-addresses Display the IP addresses of all network interfaces
-s, --short Display the short hostname (with the domain part removed)
-y, --yp, --nis Display the NIS domain name

Three Ways to Set the Hostname

1. Temporarily Set the Hostname (becomes invalid after reboot)

Example

sudo hostname new-hostname

Example:

Example

$ sudo hostname test-server
$ hostname
test-server

Note: The hostname set by this method will revert to the original hostname after a system reboot.

2. Permanently Set the Hostname (modify the configuration file)

On most modern Linux distributions (using systemd), you can permanently modify the hostname like this:

Example

sudo hostnamectl set-hostname new-hostname

Example:

Example

$ sudo hostnamectl set-hostname production-server

This command will modify both:

  • /etc/hostnamethe file
  • the static hostname of the system

3. Manually Edit the Configuration File

You can also directly edit the hostname configuration file:

  1. Edit the/etc/hostnamefile:

    sudo nano /etc/hostname

    Delete the original content and write in only the new hostname

  2. Update the/etc/hostsfile, ensuring there is a line mapping the hostname to 127.0.0.1:

    127.0.0.1   localhost new-hostname
    
  3. Apply the changes (no reboot required):

    sudo systemctl restart systemd-hostnamed

Viewing Different Types of Hostnames

View the Static Hostname

Example

hostnamectl --static
# or
cat /etc/hostname

View the Transient Hostname

Example

hostname
# or
hostnamectl --transient

View the Pretty Hostname

Example

hostnamectl --pretty

View the FQDN (Fully Qualified Domain Name)

Example

hostname -f
# or
hostnamectl --fqdn

Practical Application Examples

Example 1: View the Complete Host Information of the Current System

Example

$ hostnamectl
   Static hostname: ubuntu-server
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 1a2b3c4d5e6f7g8h9i0j
           Boot ID: 1k2l3m4n5o6p7q8r9s0t
    Virtualization: kvm
  Operating System: Ubuntu 20.04 LTS
            Kernel: Linux 5.4.0-42-generic
      Architecture: x86-64

Example 2: Set Both the Static and Pretty Hostnames

Example

sudo hostnamectl set-hostname "server01" --pretty "Primary Web Server"

Example 3: Obtain Hostname Information via the Network

Example

$ hostname -i
192.168.1.100

$ hostname -I
192.168.1.100 10.0.0.100

Common Issues and Solutions

Issue 1: The Hostname Does Not Take Effect After Being Set

Solution:

  1. Make sure to also update the/etc/hostsfile
  2. Check whether other services (such as NetworkManager) are overriding the hostname settings
  3. Try restarting the system or running:
    sudo systemctl restart systemd-hostnamed

Issue 2: The Hostname Contains Illegal Characters

Solution:

  • A hostname can only contain:
    • Letters a-z
    • Digits 0-9
    • Hyphens (-)
  • Cannot start or end with a hyphen
  • The length is usually limited to 63 characters

Issue 3: Network Services Cannot Recognize the New Hostname

Solution:

  1. Restart the related network services:
    sudo systemctl restart networking
  2. Clear the DNS cache (if applicable)
  3. Ensure the DNS server has been updated (if it is a server on the network)

Best Practice Suggestions

  1. Naming Conventions:

    • Use meaningful names, such as "web-prod-01" or "db-backup-02"
    • Avoid using special characters and underscores
    • Maintain consistency, especially in server clusters
  2. Multi-hostname Environments:

    • Use the static hostname as the primary identifier
    • Use the pretty hostname to provide a human-readable description
    • Use/etc/hoststo add aliases for important servers
  3. Automated Management:

    • In cloud environments, you can use cloud-init to automatically set hostnames
    • Manage hostnames in bulk through configuration management tools (such as Ansible)
  4. Documentation:

    • Maintain documentation that maps hostnames to server roles
    • Use hostnames as identifiers in monitoring systems

Summary

hostname is a basic but important concept in Linux system administration. Through this article, you should have learned:

  1. How to view and set different types of hostnames
  2. The correct process for permanently modifying the hostname
  3. Solutions to common issues related to hostnames
  4. Best practices for hostname management

Remember, before modifying the hostname of a production environment, be sure to evaluate the possible impact. Especially in cluster environments, hostname changes may affect service discovery and monitoring systems.

Linux 命令大全Complete Linux Command Reference

Other Extensions