uname -n vs hostname
There is no difference. hostname
and uname -n
output the same information. They both obtain it from the uname()
system call.
One difference is that the hostname
command can be used to set the hostname as well as getting it. uname
cannot do that. (Normally this is done only once, early in the boot process!)
Like others have said, uname -n
is your friend, and has an advantage in that it is a POSIX/SUSv4-compliant command, whereas hostname
is not.
Note that, however, uname -n
is a simple wrapper around the gethostname(2)
system call. In particular, it does not consult the name service to convert the name returned by gethostname(2)
into a fully-qualified domain name, which can be achieved by hostname -f
on Linux. (BSD/Apple hostname -f
is identical to uname -n
.)