Debian Codegraph @

upstream/debian-unstable/man2/gethostname.2

raw
1 .\" Copyright 1993, Rickard E. Faith <[email protected]>
2 .\" Copyright, the authors of the Linux man-pages project
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH gethostname 2 2026-02-08 "Linux man-pages 6.18"
7 .SH NAME
8 gethostname, sethostname \- get/set hostname
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ,\~ \-lc )
12 .SH SYNOPSIS
13 .nf
14 .B #include <unistd.h>
15 .P
16 .BI "int gethostname(char *" name ", size_t " size );
17 .BI "int sethostname(const char *" name ", size_t " size );
18 .fi
19 .P
20 .RS -4
21 Feature Test Macro Requirements for glibc (see
22 .BR feature_test_macros (7)):
23 .RE
24 .P
25 .BR gethostname ():
26 .nf
27 _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
28 || /* glibc 2.19 and earlier */ _BSD_SOURCE
29 .\" The above is something of a simplification
30 .\" also before glibc 2.3 there was a bit churn
31 .fi
32 .P
33 .BR sethostname ():
34 .nf
35 Since glibc 2.21:
36 .\" commit 266865c0e7b79d4196e2cc393693463f03c90bd8
37 _DEFAULT_SOURCE
38 In glibc 2.19 and 2.20:
39 _DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
40 Up to and including glibc 2.19:
41 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
42 .fi
43 .SH DESCRIPTION
44 These system calls are used to access or to change the system hostname.
45 More precisely, they operate on the hostname associated with the calling
46 process's UTS namespace.
47 .P
48 .BR sethostname ()
49 sets the hostname to the value given in the character array
50 .IR name .
51 The
52 .I size
53 argument specifies the number of bytes in
54 .IR name .
55 (Thus,
56 .I name
57 does not require a terminating null byte.)
58 .P
59 .BR gethostname ()
60 returns the null-terminated hostname in the character array
61 .IR name ,
62 which has a size of
63 .I size
64 bytes.
65 If the null-terminated hostname is too large to fit,
66 then the name is truncated, and no error is returned (but see VERSIONS below).
67 POSIX.1 says that if such truncation occurs,
68 then it is unspecified whether the returned buffer
69 includes a terminating null byte.
70 .SH RETURN VALUE
71 On success, zero is returned.
72 On error, \-1 is returned, and
73 .I errno
74 is set to indicate the error.
75 .SH ERRORS
76 .TP
77 .B EFAULT
78 .I name
79 is an invalid address.
80 .TP
81 .B EINVAL
82 .I size
83 is negative
84 .\" Can't occur for gethostbyname() wrapper, since 'size' has an
85 .\" unsigned type; can occur for the underlying system call.
86 or, for
87 .BR sethostname (),
88 .I size
89 is larger than the maximum allowed size.
90 .TP
91 .B ENAMETOOLONG
92 .RB "(glibc " gethostname ())
93 .I size
94 is smaller than the actual size.
95 (Before glibc 2.1, glibc uses
96 .B EINVAL
97 for this case.)
98 .TP
99 .B EPERM
100 For
101 .BR sethostname (),
102 the caller did not have the
103 .B CAP_SYS_ADMIN
104 capability in the user namespace associated with its UTS namespace (see
105 .BR namespaces (7)).
106 .SH VERSIONS
107 SUSv2 guarantees that "Host names are limited to 255 bytes".
108 POSIX.1 guarantees that "Host names (not including
109 the terminating null byte) are limited to
110 .B HOST_NAME_MAX
111 bytes".
112 On Linux,
113 .B HOST_NAME_MAX
114 is defined with the value 64, which has been the limit since Linux 1.0
115 (earlier kernels imposed a limit of 8 bytes).
116 .SS C library/kernel differences
117 The GNU C library does not employ the
118 .BR gethostname ()
119 system call;
120 instead,
121 it implements
122 .BR gethostname ()
123 as a library function that calls
124 .BR uname (2)
125 and copies up to
126 .I size
127 bytes from the returned
128 .I nodename
129 field into
130 .IR name .
131 Having performed the copy, the function then checks if the length of the
132 .I nodename
133 was greater than or equal to
134 .IR size ,
135 and if it is, then the function returns \-1 with
136 .I errno
137 set to
138 .BR ENAMETOOLONG ;
139 in this case, a terminating null byte is not included in the returned
140 .IR name .
141 .SH STANDARDS
142 .TP
143 .BR gethostname ()
144 POSIX.1-2024.
145 .TP
146 .BR sethostname ()
147 None.
148 .SH HISTORY
149 .TP
150 .BR gethostname ()
151 4.2BSD,
152 SVr4,
153 POSIX.1-2001.
154 .TP
155 .BR sethostname ()
156 4.2BSD,
157 SVr4.
158 .P
159 Versions of glibc before glibc 2.2
160 .\" At least glibc 2.0 and glibc 2.1, older versions not checked
161 handle the case where the length of the
162 .I nodename
163 was greater than or equal to
164 .I size
165 differently: nothing is copied into
166 .I name
167 and the function returns \-1 with
168 .I errno
169 set to
170 .BR ENAMETOOLONG .
171 .SH SEE ALSO
172 .BR hostname (1),
173 .BR getdomainname (2),
174 .BR setdomainname (2),
175 .BR uname (2),
176 .BR uts_namespaces (7)