Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux hostname command

Linux hostname command

Linux Command Reference Linux Command Reference


What is hostname

hostname (host name) is the 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:

Host names are generally divided into three types:

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

hostname command basic syntax

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



hostname [选项] [新主机名]

If executed directly without any parameters, the hostname command will display the current system's host name:

Example

$

hostname

ubuntu-server


Description of common options and parameters

The hostname command supports several options to view and set different types of host names:

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

Three methods to set the host name

1. Temporarily set the host name (invalid after reboot)

Example

sudo

hostname

new-hostname

Example:

Example

$

sudo

hostname

test-server

$

hostname

test-server

Note: A host name set in this way will revert to the original host name after the system reboots.

2. Permanently set the host name (modify the configuration file)

In most modern Linux distributions (using systemd), you can permanently change the host name like this:

Example

sudo

hostnamectl set-hostname new-hostname

Example:

Example

$

sudo

hostnamectl set-hostname production-server

This command modifies both:

3. Manually edit the configuration file

You can also directly edit the host name configuration file:

  1. Editing /etc/hostname File:

sudo nano /etc/hostname

Delete the original content and write only the new host name

2.

update /etc/hosts file, ensuring there is a line mapping the host name to 127.0.0.1:

127.0.0.1   localhost new-hostname

3.

Apply the changes (no reboot required):

sudo systemctl restart systemd-hostnamed


View different types of host names

View the static host name

Example

hostnamectl

--static

or

cat

/

etc

/

hostname

View the transient host name

Example

hostname

or

hostnamectl

--transient

View the pretty host name

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 the static and pretty host names at the same time

Example

sudo

hostnamectl set-hostname

"server01"

--pretty

"Primary Web Server"

Example 3: Obtain host name information over the network

Example

$

hostname

-i

192.168.1.100

$

hostname

-I

192.168.1.100 10.0.0.100


Common Problems and Solutions

Problem 1: The host name setting does not take effect

Solution:

  1. Ensure that it is also updated at the same time /etc/hosts file
  2. Check whether other services (such as NetworkManager) have overridden the host name setting
  3. Try rebooting the system or running:
    sudo systemctl restart systemd-hostnamed

Problem 2: The host name contains illegal characters

Solution:

- Letters a-z - Digits 0-9 - Hyphen (-)

Problem 3: Network services cannot recognize the new host name

Solution:

  1. Restart the relevant 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 Recommendations

  1. Naming Convention:

  2. Use meaningful names such as "web-prod-01" and "db-backup-02"
  3. Avoid using special characters and underscores
  4. Maintain consistency, especially in server clusters
  5. - Use meaningful names such as "web-prod-01" and "db-backup-02" - Avoid using special characters and underscores - Maintain consistency, especially in server clusters
    1. Multiple host name environments:

    2. Use the static host name as the primary identifier
    3. Use the pretty host name to provide a human-readable description
    4. Pass /etc/hosts Add aliases for important servers
    5. - Use the static host name as the primary identifier - Use the pretty host name to provide a human-readable description - Add aliases for important servers via `/etc/hosts`
      1. Automated management:

      2. In cloud environments, you can use cloud-init to automatically set the host name
      3. Manage host names in bulk through configuration management tools (such as Ansible)
      4. - In cloud environments, you can use cloud-init to automatically set the host name - Manage host names in bulk through configuration management tools (such as Ansible)
        1. Documentation:

        2. Maintain documentation mapping host names to server roles
        3. Use host names as identifiers in monitoring systems
        4. - Maintain documentation mapping host names to server roles - Use host names as identifiers in monitoring systems

          Summary

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

          1. How to view and set different types of host names
          2. The correct procedure for permanently changing the host name
          3. Solutions to common problems related to host names
          4. Best practices for host name management

          Remember, before changing the host name in a production environment, be sure to assess the possible impact, especially in a cluster environment where changing the host name may affect service discovery and monitoring systems.

          Linux Command Reference Linux Command Reference