Host Information

Every Unix system (also known as a host) has a host name, whether it's connected to a network or not. In its simplest form, as used before computer networks were an issue, it's just a word like `chicken'. But any system attached to the Internet or any network like it conforms to a more rigorous naming convention as part of the Domain Name System (DNS). In DNS, every host name is composed of two parts:

  1. hostname
  2. domain name

You will note that “hostname” looks a lot like “host name”, but is not the same thing, and that people often incorrectly refer to entire host names as “domain names.”

In DNS, the full host name is properly called the FQDN (Fully Qualified Domain Name) and consists of the hostname, then a period, then the domain name. The domain name itself usually has multiple components separated by periods. So for example, a system's hostname may be `chicken' and its domain name might be `ai.mit.edu', so its FQDN (which is its host name) is `chicken.ai.mit.edu'. Adding to the confusion, though, is that DNS is not the only name space in which a computer needs to be known. Another name space is the NIS (aka YP) name space. For NIS purposes, there is another domain name, which is called the NIS domain name or the YP domain name. It need not have anything to do with the DNS domain name. Confusing things even more is the fact that in DNS, it is possible for multiple FQDNs to refer to the same system. However, there is always exactly one of them that is the true host name, and it is called the canonical FQDN.


#include <unistd.h>

int gethostname(char *name, size_t size)

This function returns the host name of the system on which it is called, in the array name. The size argument specifies the size of this array, in bytes. Note that this is not the DNS hostname. If the system participates in DNS, this is the FQDN (see above).

Platform Type Identification

You can use the uname function to find out some information about the type of computer your program is running on.


#include <sys/utsname.h>

int uname(struct utsname *name)

The uname function writes host information into the structure pointed to by the name parameter. The utsname structure, defined in sys/utsname.h contains at least the following members:

utsname MemberDescription
char sysname[]The name of the operating system in use.
char nodename[]The host name
char release[]This is the current release level of the operating system implementation.
char version[]This is the current version level within the release of the operating system.
char machine[]This is a description of the type of hardware that is in use.

References

Neil Matthew and Richard Stone, Beginning Linux Programming, Third Edition,
Wrox, 2004. ISBN 0-7645-4497-7. p 158-160.

Host Identification (from the GNU C Library Reference Manual).


Maintained by John Loomis, last updated 4 September 2006