1gethostname(2) System Calls Manual gethostname(2)
2
3
4
6 gethostname, sethostname - get/set hostname
7
9 Standard C library (libc, -lc)
10
12 #include <unistd.h>
13
14 int gethostname(char *name, size_t len);
15 int sethostname(const char *name, size_t len);
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 gethostname():
20 _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
21 || /* glibc 2.19 and earlier */ _BSD_SOURCE
22
23 sethostname():
24 Since glibc 2.21:
25 _DEFAULT_SOURCE
26 In glibc 2.19 and 2.20:
27 _DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
28 Up to and including glibc 2.19:
29 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
30
32 These system calls are used to access or to change the system hostname.
33 More precisely, they operate on the hostname associated with the call‐
34 ing process's UTS namespace.
35
36 sethostname() sets the hostname to the value given in the character ar‐
37 ray name. The len argument specifies the number of bytes in name.
38 (Thus, name does not require a terminating null byte.)
39
40 gethostname() returns the null-terminated hostname in the character ar‐
41 ray name, which has a length of len bytes. If the null-terminated
42 hostname is too large to fit, then the name is truncated, and no error
43 is returned (but see NOTES below). POSIX.1 says that if such trunca‐
44 tion occurs, then it is unspecified whether the returned buffer in‐
45 cludes a terminating null byte.
46
48 On success, zero is returned. On error, -1 is returned, and errno is
49 set to indicate the error.
50
52 EFAULT name is an invalid address.
53
54 EINVAL len is negative or, for sethostname(), len is larger than the
55 maximum allowed size.
56
57 ENAMETOOLONG
58 (glibc gethostname()) len is smaller than the actual size. (Be‐
59 fore glibc 2.1, glibc uses EINVAL for this case.)
60
61 EPERM For sethostname(), the caller did not have the CAP_SYS_ADMIN ca‐
62 pability in the user namespace associated with its UTS namespace
63 (see namespaces(7)).
64
66 SUSv2 guarantees that "Host names are limited to 255 bytes". POSIX.1
67 guarantees that "Host names (not including the terminating null byte)
68 are limited to HOST_NAME_MAX bytes". On Linux, HOST_NAME_MAX is de‐
69 fined with the value 64, which has been the limit since Linux 1.0 (ear‐
70 lier kernels imposed a limit of 8 bytes).
71
72 C library/kernel differences
73 The GNU C library does not employ the gethostname() system call; in‐
74 stead, it implements gethostname() as a library function that calls un‐
75 ame(2) and copies up to len bytes from the returned nodename field into
76 name. Having performed the copy, the function then checks if the
77 length of the nodename was greater than or equal to len, and if it is,
78 then the function returns -1 with errno set to ENAMETOOLONG; in this
79 case, a terminating null byte is not included in the returned name.
80
82 gethostname()
83 POSIX.1-2008.
84
85 sethostname()
86 None.
87
89 SVr4, 4.4BSD (these interfaces first appeared in 4.2BSD). POSIX.1-2001
90 and POSIX.1-2008 specify gethostname() but not sethostname().
91
92 Versions of glibc before glibc 2.2 handle the case where the length of
93 the nodename was greater than or equal to len differently: nothing is
94 copied into name and the function returns -1 with errno set to ENAME‐
95 TOOLONG.
96
98 hostname(1), getdomainname(2), setdomainname(2), uname(2), uts_name‐
99 spaces(7)
100
101
102
103Linux man-pages 6.04 2023-03-30 gethostname(2)