1gethostid(3) Library Functions Manual gethostid(3)
2
3
4
6 gethostid, sethostid - get or set the unique identifier of the current
7 host
8
10 Standard C library (libc, -lc)
11
13 #include <unistd.h>
14
15 long gethostid(void);
16 int sethostid(long hostid);
17
18 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20 gethostid():
21 Since glibc 2.20:
22 _DEFAULT_SOURCE || _XOPEN_SOURCE >= 500
23 Up to and including glibc 2.19:
24 _BSD_SOURCE || _XOPEN_SOURCE >= 500
25
26 sethostid():
27 Since glibc 2.21:
28 _DEFAULT_SOURCE
29 In glibc 2.19 and 2.20:
30 _DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
31 Up to and including glibc 2.19:
32 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
33
35 gethostid() and sethostid() respectively get or set a unique 32-bit
36 identifier for the current machine. The 32-bit identifier was intended
37 to be unique among all UNIX systems in existence. This normally resem‐
38 bles the Internet address for the local machine, as returned by geth‐
39 ostbyname(3), and thus usually never needs to be set.
40
41 The sethostid() call is restricted to the superuser.
42
44 gethostid() returns the 32-bit identifier for the current host as set
45 by sethostid().
46
47 On success, sethostid() returns 0; on error, -1 is returned, and errno
48 is set to indicate the error.
49
51 sethostid() can fail with the following errors:
52
53 EACCES The caller did not have permission to write to the file used to
54 store the host ID.
55
56 EPERM The calling process's effective user or group ID is not the same
57 as its corresponding real ID.
58
60 For an explanation of the terms used in this section, see at‐
61 tributes(7).
62
63 ┌────────────┬───────────────┬─────────────────────────────────────────┐
64 │Interface │ Attribute │ Value │
65 ├────────────┼───────────────┼─────────────────────────────────────────┤
66 │gethostid() │ Thread safety │ MT-Safe hostid env locale │
67 ├────────────┼───────────────┼─────────────────────────────────────────┤
68 │sethostid() │ Thread safety │ MT-Unsafe const:hostid │
69 └────────────┴───────────────┴─────────────────────────────────────────┘
70
72 In the glibc implementation, the hostid is stored in the file
73 /etc/hostid. (Before glibc 2.2, the file /var/adm/hostid was used.)
74
75 In the glibc implementation, if gethostid() cannot open the file
76 containing the host ID, then it obtains the hostname using
77 gethostname(2), passes that hostname to gethostbyname_r(3) in order to
78 obtain the host's IPv4 address, and returns a value obtained by bit-
79 twiddling the IPv4 address. (This value may not be unique.)
80
82 gethostid()
83 POSIX.1-2008.
84
85 sethostid()
86 None.
87
89 4.2BSD; dropped in 4.4BSD. SVr4 and POSIX.1-2001 include gethostid()
90 but not sethostid().
91
93 It is impossible to ensure that the identifier is globally unique.
94
96 hostid(1), gethostbyname(3)
97
98
99
100Linux man-pages 6.05 2023-07-20 gethostid(3)