Linux hostname Command
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:
- Static hostname: Stored in the configuration file, remains unchanged after a system reboot
- Transient hostname: Temporarily set at runtime, becomes invalid after reboot
- 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
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
Example:
Example
$ 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
Example:
Example
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:
Edit the
/etc/hostnamefile:sudo nano /etc/hostname
Delete the original content and write in only the new hostname
Update the
/etc/hostsfile, ensuring there is a line mapping the hostname to 127.0.0.1:127.0.0.1 localhost new-hostname
Apply the changes (no reboot required):
sudo systemctl restart systemd-hostnamed
Viewing Different Types of Hostnames
View the Static Hostname
Example
# or
cat /etc/hostname
View the Transient Hostname
Example
# or
hostnamectl --transient
View the Pretty Hostname
Example
View the FQDN (Fully Qualified Domain Name)
Example
# or
hostnamectl --fqdn
Practical Application Examples
Example 1: View the Complete Host Information of the Current System
Example
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
Example 3: Obtain Hostname Information via the Network
Example
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:
- Make sure to also update the
/etc/hostsfile - Check whether other services (such as NetworkManager) are overriding the hostname settings
- 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:
- Restart the related network services:
sudo systemctl restart networking
- Clear the DNS cache (if applicable)
- Ensure the DNS server has been updated (if it is a server on the network)
Best Practice Suggestions
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
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
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)
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:
- How to view and set different types of hostnames
- The correct process for permanently modifying the hostname
- Solutions to common issues related to hostnames
- 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.
Other Extensions