Intro To 'hostname' Command In Linux
2024-04-21 - By Robert Elder
I use the 'hostname' command, to print the hostname of the current machine:
hostname
robert-ThinkPad-P15v-Gen-1
A hostname is the name that's used to identify a particular machine on a computer network.
Usually, this hostname will also be associated with an IP address. You can use commands like the 'dig' command to check what this IP address is:dig +short $(hostname)
127.0.1.1
Multiple Versions Of 'hostname' Command
The 'hostname' command has multiple different common implementations, and this can lead to confusing results. For example, if I use the version of 'hostname' that's built-into my system on Ubuntu:
hostname -i
I'll see this result:
127.0.1.1
However, if I try to use the version of 'hostname' from Core Utils:
./coreutils-9.4/src/hostname -i
I'll see this result:
./coreutils-9.4/src/hostname: invalid option -- 'i'
Try './coreutils-9.4/src/hostname --help' for more information.
GNU Core Utils 'hostname' Command
The GNU Core Utils implementation of the 'hostname' command is very simple and only supports reading or writing a machine's host name:
info hostname
Next: hostid invocation, Prev: uname invocation, Up: System context
21.5 ‘hostname’: Print or set system name
=========================================
With no arguments, ‘hostname’ prints the name of the current host
system. With one argument, it sets the current host name to the
specified string. You must have appropriate privileges to set the host
name. Synopsis:
hostname [NAME]
The only options are ‘--help’ and ‘--version’. *Note Common
options::.
‘hostname’ is not installed by default, and other packages also
supply a ‘hostname’ command, so portable scripts should not rely on its
existence or on the exact behavior documented above.
An exit status of zero indicates success, and a nonzero value
indicates failure.
Net Tools 'hostname' Command
For the reasons noted above, a different implementation of the 'hostname' command also exists in the 'net-tools' package, which supports many more flags and options:
hostname --help
Usage: hostname [-b] {hostname|-F file} set host name (from file)
hostname [-a|-A|-d|-f|-i|-I|-s|-y] display formatted name
hostname display host name
{yp,nis,}domainname {nisdomain|-F file} set NIS domain name (from file)
{yp,nis,}domainname display NIS domain name
dnsdomainname display dns domain name
hostname -V|--version|-h|--help print info and exit
Program name:
{yp,nis,}domainname=hostname -y
dnsdomainname=hostname -d
Program options:
-a, --alias alias names
-A, --all-fqdns all long host names (FQDNs)
-b, --boot set default hostname if none available
-d, --domain DNS domain name
-f, --fqdn, --long long host name (FQDN)
-F, --file read host name or NIS domain name from given file
-i, --ip-address addresses for the host name
-I, --all-ip-addresses all addresses for the host
-s, --short short host name
-y, --yp, --nis NIS/YP domain name
Description:
This command can get or set the host name or the NIS domain name. You can
also get the DNS domain or the FQDN (fully qualified domain name).
Unless you are using bind or NIS for host lookups you can change the
FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
part of the FQDN) in the /etc/hosts file.
Check Which Version Of 'hostname' You Have
On my Ubuntu 20 machine, the 'info' pages show documentation for the Core Utils version of hostname, while the 'man' pages show documentation for the 'net-tools' version of hostname. On my machine, invoking the 'hostname' command appears to run the 'net-tools' version. This will likely be different on your distribution. You can investigate which version you likely have by running 'hostname' with the '--version' flag:
hostname --version
or, by checking the 'man' and 'info' pages:
man hostname
info hostname
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|