Linux hostname command
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:
- Identify the local computer
- Identify devices in network communication
- Affect the operation of certain network services
- Display in the terminal prompt
Host names are generally divided into three types:
- Static host name: Stored in a configuration file, remains unchanged after system reboot
- Transient host name: Temporarily set at runtime, invalid after reboot
- 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:
/etc/hostnamefile- The system's static host name
3. Manually edit the configuration file
You can also directly edit the host name configuration file:
-
Editing
/etc/hostnameFile:
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-hostname3.
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:
- Ensure that it is also updated at the same time
/etc/hostsfile - Check whether other services (such as NetworkManager) have overridden the host name setting
- Try rebooting the system or running:
sudo systemctl restart systemd-hostnamed
Problem 2: The host name contains illegal characters
Solution:
- A host name can only contain:
- Letters a-z
- Digits 0-9
- Hyphen (-)
- Cannot start or end with a hyphen
- Length is usually limited to 63 characters
Problem 3: Network services cannot recognize the new host name
Solution:
- Restart the relevant 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 Recommendations
-
Naming Convention:
-
Multiple host name environments:
/etc/hosts Add aliases for important servers-
Automated management:
-
Documentation:
Summary
hostname is a fundamental but important concept in Linux system administration. Through this article, you should have mastered:
- How to view and set different types of host names
- The correct procedure for permanently changing the host name
- Solutions to common problems related to host names
- 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