1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Modified 1995-07-22 by Michael Chastain <mec@duracef.shout.net>:
26 .\" 'gethostname' is real system call on Linux/Alpha.
27 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
28 .\" Modified 2000-06-04, 2001-12-15 by aeb
29 .\" Modified 2004-06-17 by mtk
30 .\" Modified 2008-11-27 by mtk
32 .TH GETHOSTNAME 2 2010-09-20 "Linux" "Linux Programmer's Manual"
34 gethostname, sethostname \- get/set hostname
36 .B #include <unistd.h>
38 .BI "int gethostname(char *" name ", size_t " len );
40 .BI "int sethostname(const char *" name ", size_t " len );
43 Feature Test Macro Requirements for glibc (see
44 .BR feature_test_macros (7)):
52 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
54 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200112L
59 _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE\ <\ 500)
64 These system calls are used to access or to change the hostname of the
68 sets the hostname to the value given in the character array
72 argument specifies the number of bytes in
76 does not require a terminating null byte.)
79 returns the null-terminated hostname in the character array
84 If the null-terminated hostname is too large to fit,
85 then the name is truncated, and no error is returned (but see NOTES below).
86 POSIX.1-2001 says that if such truncation occurs,
87 then it is unspecified whether the returned buffer
88 includes a terminating null byte.
90 On success, zero is returned.
91 On error, \-1 is returned, and
98 is an invalid address.
103 .\" Can't occur for gethostbyname() wrapper, since 'len' has an
104 .\" unsigned type; can occur for the underlying system call.
108 is larger than the maximum allowed size.
111 .RB "(glibc " gethostname ())
113 is smaller than the actual size.
114 (Before version 2.1, glibc uses
121 the caller did not have the
125 SVr4, 4.4BSD (these interfaces first appeared in 4.2BSD).
126 POSIX.1-2001 specifies
131 SUSv2 guarantees that "Host names are limited to 255 bytes".
132 POSIX.1-2001 guarantees that "Host names (not including
133 the terminating null byte) are limited to
138 is defined with the value 64, which has been the limit since Linux 1.0
139 (earlier kernels imposed a limit of 8 bytes).
141 The GNU C library does not employ the
143 system call; instead, it implements
145 as a library function that calls
149 bytes from the returned
153 Having performed the copy, the function then checks if the length of the
155 was greater than or equal to
157 and if it is, then the function returns \-1 with
161 in this case, a terminating null byte is not included in the returned
164 Versions of glibc before 2.2
165 .\" At least glibc 2.0 and 2.1, older versions not checked
166 handle the case where the length of the
168 was greater than or equal to
170 differently: nothing is copied into
172 and the function returns \-1 with
177 .BR getdomainname (2),
178 .BR setdomainname (2),