Node:Host Identification, Next:Platform Type, Up:System Management
This section explains how to identify the particular system on which your program is running. First, let's review the various ways computer systems are named, which is a little complicated because of the history of the development of the Internet.
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:
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.
In some contexts, the host name is called a "node name."
For more information on DNS host naming, See Host Names.
Prototypes for these functions appear in unistd.h
.
The programs hostname
, hostid
, and domainname
work
by calling these functions.
int gethostname (char *name, size_t size) | Function |
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).
The return value is
On some systems, there is a symbol for the maximum possible host name
length:
|
int sethostname (const char *name, size_t length) | Function |
The sethostname function sets the host name of the system that
calls it to name, a string with length length. Only
privileged processes are permitted to do this.
Usually Be sure to set the host name to the full host name, not just the DNS hostname (see above). The return value is
|
int getdomainnname (char *name, size_t length) | Function |
The specifics of this function are analogous to |
int setdomainname (const char *name, size_t length) | Function |
The specifics of this function are analogous to |
long int gethostid (void) | Function |
This function returns the "host ID" of the machine the program is
running on. By convention, this is usually the primary Internet IP address
of that machine, converted to a long int . However, on some
systems it is a meaningless but unique number which is hard-coded for
each machine.
This is not widely used. It arose in BSD 4.2, but was dropped in BSD 4.4. It is not required by POSIX. The proper way to query the IP address is to use |
int sethostid (long int id) | Function |
The sethostid function sets the "host ID" of the host machine
to id. Only privileged processes are permitted to do this. Usually
it happens just once, at system boot time.
The proper way to establish the primary IP address of a system
is to configure the IP address resolver to associate that IP address with
the system's host name as returned by See The return value is
|