1GETHOSTNAME(2)             Linux Programmer's Manual            GETHOSTNAME(2)
2
3
4

NAME

6       gethostname, sethostname - get/set hostname
7

SYNOPSIS

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(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
17       sethostname(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
18

DESCRIPTION

20       These  system calls are used to access or to change the hostname of the
21       current processor.
22
23       sethostname() sets the hostname to the value  given  in  the  character
24       array  name.   The  len argument specifies the number of bytes in name.
25       (Thus, name does not require a terminating null byte.)
26
27       gethostname() returns the null-terminated  hostname  in  the  character
28       array  name,  which  has a length of len bytes.  If the null-terminated
29       hostname is too large to fit, then the name is truncated, and no  error
30       is  returned  (but  see  NOTES  below).  POSIX.1-2001 says that if such
31       truncation occurs, then it is unspecified whether the  returned  buffer
32       includes a terminating null byte.
33

RETURN VALUE

35       On  success,  zero is returned.  On error, -1 is returned, and errno is
36       set appropriately.
37

ERRORS

39       EFAULT name is an invalid address.
40
41       EINVAL len is negative or, for sethostname(), len is  larger  than  the
42              maximum allowed size.
43
44       ENAMETOOLONG
45              (glibc  gethostname())  len  is  smaller  than  the actual size.
46              (Before version 2.1, glibc uses EINVAL for this case.)
47
48       EPERM  For sethostname(), the caller did  not  have  the  CAP_SYS_ADMIN
49              capability.
50

CONFORMING TO

52       SVr4,   4.4BSD    (these   interfaces   first   appeared   in  4.2BSD).
53       POSIX.1-2001 specifies gethostname() but not sethostname().
54

NOTES

56       SUSv2  guarantees  that  "Host  names  are  limited  to   255   bytes".
57       POSIX.1-2001 guarantees that "Host names (not including the terminating
58       null  byte)  are  limited   to   HOST_NAME_MAX   bytes".    On   Linux,
59       HOST_NAME_MAX  is  defined  with the value 64, which has been the limit
60       since Linux 1.0 (earlier kernels imposed a limit of 8 bytes).
61
62   Glibc Notes
63       The GNU C library  does  not  employ  the  gethostname()  system  call;
64       instead,  it  implements gethostname() as a library function that calls
65       uname(2) and copies up to len bytes from the  returned  nodename  field
66       into  name.  Having performed the copy, the function then checks if the
67       length of the nodename was greater than or equal to len, and if it  is,
68       then  the  function  returns -1 with errno set to ENAMETOOLONG; in this
69       case, no null-terminator is included in the returned name.
70
71       Versions of glibc before 2.2 handle the case where the  length  of  the
72       nodename  was  greater  than  or  equal  to len differently: nothing is
73       copied into name and the function returns -1 with errno set  to  ENAMEā€
74       TOOLONG.
75

SEE ALSO

77       getdomainname(2), setdomainname(2), uname(2)
78

COLOPHON

80       This  page  is  part of release 3.22 of the Linux man-pages project.  A
81       description of the project, and information about reporting  bugs,  can
82       be found at http://www.kernel.org/doc/man-pages/.
83
84
85
86Linux                             2008-11-27                    GETHOSTNAME(2)
Impressum