← 返回命令列表

Linux command

hostname 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Display the current hostname

hostname

Display the fully qualified domain name

hostname -f

Display the short hostname

hostname -s

Display all IP addresses

hostname -I

Display the DNS domain name

hostname -d

Set the hostname

sudo hostname [new_hostname]

Set hostname from a file

sudo hostname -F [/etc/hostname]

Display the NIS/YP domain name

hostname -y

说明

hostname displays or sets the system's host name. The host name is used by many network programs to identify the machine. The command can show various name components including the short name, FQDN, domain name, and IP addresses. When called without arguments, it displays the current hostname as returned by the gethostname system call. When called with an argument, it sets the hostname (requires root privileges). Related commands domainname, dnsdomainname, and nisdomainname are often symbolic links to hostname. The -I option is particularly useful for scripts as it lists all configured IP addresses without relying on DNS resolution. The FQDN (-f) combines the short hostname with the DNS domain.

参数

-a, --alias
Display the alias name of the host
-d, --domain
Display the DNS domain name
-f, --fqdn, --long
Display the Fully Qualified Domain Name (FQDN)
-A, --all-fqdns
Display all FQDNs of the machine
-i, --ip-address
Display the IP address for the hostname
-I, --all-ip-addresses
Display all network addresses of the host (excludes loopback and IPv6 link-local)
-s, --short
Display the short hostname (up to the first dot)
-y, --yp, --nis
Display or set the NIS/YP domain name
-F, --file _FILE_
Read hostname from the specified file
-b, --boot
Always set a hostname; if none specified, use default
-v, --verbose
Be verbose about actions taken
-h, --help
Display help message
-V, --version
Display version information

FAQ

What is the hostname command used for?

hostname displays or sets the system's host name. The host name is used by many network programs to identify the machine. The command can show various name components including the short name, FQDN, domain name, and IP addresses. When called without arguments, it displays the current hostname as returned by the gethostname system call. When called with an argument, it sets the hostname (requires root privileges). Related commands domainname, dnsdomainname, and nisdomainname are often symbolic links to hostname. The -I option is particularly useful for scripts as it lists all configured IP addresses without relying on DNS resolution. The FQDN (-f) combines the short hostname with the DNS domain.

How do I run a basic hostname example?

Run `hostname` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -a, --alias do in hostname?

Display the alias name of the host