1GETHOSTNAME(2) Linux Programmer's Manual GETHOSTNAME(2)
2
3
4
6 gethostname, sethostname - get/set hostname
7
9 #include <unistd.h>
10
11 int gethostname(char *name, size_t len);
12 int sethostname(const char *name, size_t len);
13
14 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16 gethostname():
17 Since glibc 2.12: _BSD_SOURCE || _XOPEN_SOURCE >= 500
18 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200112L
19 sethostname():
20 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
21
23 These system calls are used to access or to change the hostname of the
24 current processor.
25
26 sethostname() sets the hostname to the value given in the character
27 array name. The len argument specifies the number of bytes in name.
28 (Thus, name does not require a terminating null byte.)
29
30 gethostname() returns the null-terminated hostname in the character
31 array name, which has a length of len bytes. If the null-terminated
32 hostname is too large to fit, then the name is truncated, and no error
33 is returned (but see NOTES below). POSIX.1-2001 says that if such
34 truncation occurs, then it is unspecified whether the returned buffer
35 includes a terminating null byte.
36
38 On success, zero is returned. On error, -1 is returned, and errno is
39 set appropriately.
40
42 EFAULT name is an invalid address.
43
44 EINVAL len is negative or, for sethostname(), len is larger than the
45 maximum allowed size.
46
47 ENAMETOOLONG
48 (glibc gethostname()) len is smaller than the actual size.
49 (Before version 2.1, glibc uses EINVAL for this case.)
50
51 EPERM For sethostname(), the caller did not have the CAP_SYS_ADMIN
52 capability.
53
55 SVr4, 4.4BSD (these interfaces first appeared in 4.2BSD).
56 POSIX.1-2001 specifies gethostname() but not sethostname().
57
59 SUSv2 guarantees that "Host names are limited to 255 bytes".
60 POSIX.1-2001 guarantees that "Host names (not including the terminating
61 null byte) are limited to HOST_NAME_MAX bytes". On Linux,
62 HOST_NAME_MAX is defined with the value 64, which has been the limit
63 since Linux 1.0 (earlier kernels imposed a limit of 8 bytes).
64
65 Glibc notes
66 The GNU C library does not employ the gethostname() system call;
67 instead, it implements gethostname() as a library function that calls
68 uname(2) and copies up to len bytes from the returned nodename field
69 into name. Having performed the copy, the function then checks if the
70 length of the nodename was greater than or equal to len, and if it is,
71 then the function returns -1 with errno set to ENAMETOOLONG; in this
72 case, a terminating null byte is not included in the returned name.
73
74 Versions of glibc before 2.2 handle the case where the length of the
75 nodename was greater than or equal to len differently: nothing is
76 copied into name and the function returns -1 with errno set to ENAMEā
77 TOOLONG.
78
80 getdomainname(2), setdomainname(2), uname(2)
81
83 This page is part of release 3.53 of the Linux man-pages project. A
84 description of the project, and information about reporting bugs, can
85 be found at http://www.kernel.org/doc/man-pages/.
86
87
88
89Linux 2010-09-26 GETHOSTNAME(2)