HOSTNAME(1)
๐ HOSTNAME(1) ๐ โ Linux Programmer's Manual
โจ NAME โจ
okay so hostname is literally the Unix command for showing or setting the system's host name ๐ but it's got cousins too, bestie ๐ง:
domainnameโ show or set the system's NIS/YP domain nameypdomainnameโ show or set the system's NIS/YP domain namenisdomainnameโ show or set the system's NIS/YP domain namednsdomainnameโ show the system's DNS domain name
๐ SYNOPSIS ๐
hostname [-a|--alias] [-d|--domain] [-f|--fqdn|--long] [-A|--all-fqdns]
[-i|--ip-address] [-I|--all-ip-addresses] [-s|--short] [-y|--yp|--nis]
hostname [-b|--boot] [-F|--file filename] [hostname]
hostname [-h|--help] [-V|--version]
domainname [nisdomain] [-F file]
ypdomainname [nisdomain] [-F file]
nisdomainname [nisdomain] [-F file]
dnsdomainname
๐ธ DESCRIPTION ๐ธ
ngl, hostname is how you check the system's DNS name, and also how you peek at or change its hostname or NIS domain name ๐ซถ
๐ฎ GET NAME
when you call it with no arguments, it just shows you the current names, periodt โจ
hostname prints the name of the system as returned by the gethostname(2) function.
domainname prints the NIS domainname of the system. domainname uses the gethostname(2) function, while ypdomainname and nisdomainname use the getdomainname(2).
dnsdomainname prints the domain part of the FQDN (Fully Qualified Domain Name). The complete FQDN of the system comes back with hostname --fqdn (but see the warnings in section THE FQDN below) ๐
๐ฏ SET NAME
when you call it with one argument or with the --file option, these commands set the host name or the NIS/YP domain name ๐ฅ hostname uses the sethostname(2) function, while all three of domainname, ypdomainname and nisdomainname use setdomainname(2). Heads up: this is only effective until the next reboot. For a permanent change, edit /etc/hostname ๐
Only the super-user can change the names, so don't even try it as a regular user ๐
It is not possible to set the FQDN or the DNS domain name with the dnsdomainname command (see THE FQDN below) ๐ซ
The host name is usually set once at system startup in /etc/init.d/hostname.sh (normally by reading the contents of a file which contains the host name, e.g. /etc/hostname) ๐
๐ฆ THE FQDN
the FQDN (Fully Qualified Domain Name) of the system is the name that resolver(3) returns for the host name, such as ursula.example.com. It's usually the hostname followed by the DNS domain name (the part after the first dot). You can check the FQDN with hostname --fqdn, or the domain name with dnsdomainname ๐
You cannot change the FQDN with hostname or dnsdomainname.
the recommended way to set the FQDN is to make the hostname an alias for the fully qualified name using /etc/hosts, DNS, or NIS. For example, if the hostname was "ursula", you might have a line in /etc/hosts that reads:
127.0.1.1 ursula.example.com ursula
technically: the FQDN is the name getaddrinfo(3) returns for the host name returned by gethostname(2). The DNS domain name is the part after the first dot.
so how you change it depends on how your resolver is configured (usually in /etc/host.conf). The hosts file is usually parsed before DNS or NIS, so it's most common to change the FQDN in /etc/hosts ๐ง
Warning, bestie: if a machine has multiple network interfaces/addresses or is used in a mobile environment, it may have multiple FQDNs/domain names or none at all. So avoidhostname --fqdn,hostname --domainanddnsdomainname.hostname --ip-addresshas the same limitations, so avoid that one too ๐ญ
๐ OPTIONS ๐
-a,--aliasโ shows the alias name of the host (if used). This one is deprecated, so don't use it anymore ๐-A,--all-fqdnsโ shows all FQDNs of the machine. It goes through every configured network address on every configured network interface and translates them to DNS domain names. Addresses that can't be translated (like ones without a proper reverse IP entry) get skipped. Different addresses may resolve to the same name, so the output can have duplicates. Don't make any assumptions about the order of the output ๐ธ-b,--bootโ always sets a hostname. This lets the file given by-Fbe non-existent or empty, in which case the default hostnamelocalhostis used if none is set yet ๐ซถ-d,--domainโ shows the name of the DNS domain. Don't use the commanddomainnamefor this, because it shows the NIS domain name, not the DNS one. Usednsdomainnameinstead. See the warnings in section THE FQDN above, and honestly, just avoid this option ๐-f,--fqdn,--longโ shows the FQDN (Fully Qualified Domain Name). A FQDN is a short host name plus the DNS domain name. Unless you're using bind or NIS for host lookups, you can change the FQDN and the DNS domain name (which is part of the FQDN) in the/etc/hostsfile. See the warnings in section THE FQDN above, and usehostname --all-fqdnsinstead wherever possible โจ-F,--file filenameโ reads the host name from the specified file. Comments (lines starting with a#) are ignored ๐-i,--ip-addressโ shows the network address(es) of the host name. This only works if the host name can be resolved. Avoid it and usehostname --all-ip-addressesinstead ๐ซ-I,--all-ip-addressesโ shows all network addresses of the host. It goes through every configured address on every network interface. The loopback interface and IPv6 link-local addresses are left out. Unlike-i, this doesn't depend on name resolution. Again, don't assume anything about the order of the output ๐ฆ-s,--shortโ shows the short host name, which is the host name cut at the first dot ๐-V,--versionโ prints version information on standard output and exits successfully ๐-y,--yp,--nisโ shows the NIS domain name. If a parameter is given (or--file name), root can also set a new NIS domain ๐-h,--helpโ prints a usage message and exits โจ
๐ NOTES ๐
the address families hostname tries when looking up the FQDN, aliases and network addresses of the host are decided by your resolver's configuration. For instance, on GNU Libc systems, the resolver can be told to try IPv6 lookups first with the inet6 option in /etc/resolv.conf ๐ง
๐ FILES ๐
/etc/hostname โ historically this file was only supposed to hold the hostname, not the full canonical FQDN. Nowadays most software can cope with a full FQDN here. The system initialization scripts read this file at boot to set the hostname ๐ซถ
/etc/hosts โ usually this is where you set the domain name, by aliasing the host name to the FQDN ๐
๐ฏ AUTHORS ๐ฏ
Peter Tobias, <tobias@et-inf.fho-emden.de> Bernd Eckenfels, <net-tools@lina.inka.de> (NIS and manpage). Michael Meskes, <meskes@debian.org>
Bestie, that's the whole thing! ๐ net-tools, last updated 2009-09-16, and you're officially a hostname expert now ๐ โจ
๐ฆ where this page comes from
- debian trixie: hostname 3.25 ยท GPL-2.0-only
- nixpkgs unstable: hostname-debian 3.25
- ubuntu questing: hostname 3.25 ยท GPL-2.0-only
- ubuntu resolute: hostname 3.25build1 ยท GPL-2.0-only
- ubuntu stonking: hostname 3.25build1 ยท GPL-2.0-only
- ๐ girlified by anthropic/claude-haiku-5.5 on : a language model's rewrite of the original, under its terms