What's the difference between `hostname` and `uname -n`
On my server, these both return the host name:
$ ubuntu@mt-solo:~$ uname -n
myserver
$ ubuntu@mt-solo:~$ hostname
myserver
Is this always the case?
Solution 1:
On Linux, they are equivalent. One is just baked into the uname
utility and the other into the hostname
utility.
uname -n
Prints the network node hostname or "node name".
hostname
Without any arguments, prints the name of the system as returned by the gethostname() function.
The uname()
function documentation includes the following information:
Note that there is no standard that says that the hostname set by sethostname(2) is the same string as the nodename field of the struct returned by uname() (indeed, some systems allow a 256-byte hostname and an 8-byte nodename), but this is true on Linux. The same holds for setdomainname(2) and the domainname field.
(Note that Linux's sethostname()
limits hostnames to 64 bytes)
Further evidence that on GNU/Linux, they are exactly the same comes from the gethostname()
function documentation:
The GNU C library does not employ the gethostname() system call; instead, it implements gethostname() as a library function that calls uname(2) and copies up to len bytes from the returned nodename field into name.
Basically, hostname
includes an internal call to uname
.