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():
17           Since glibc 2.12: _BSD_SOURCE || _XOPEN_SOURCE >= 500
18           || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200112L
19       sethostname():
20           Since glibc 2.21:
21               _DEFAULT_SOURCE
22           In glibc 2.19 and 2.20:
23               _DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
24           Up to and including glibc 2.19:
25               _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
26

DESCRIPTION

28       These  system calls are used to access or to change the hostname of the
29       current processor.
30
31       sethostname() sets the hostname to the value  given  in  the  character
32       array  name.   The  len argument specifies the number of bytes in name.
33       (Thus, name does not require a terminating null byte.)
34
35       gethostname() returns the null-terminated  hostname  in  the  character
36       array  name,  which  has a length of len bytes.  If the null-terminated
37       hostname is too large to fit, then the name is truncated, and no  error
38       is  returned  (but see NOTES below).  POSIX.1 says that if such trunca‐
39       tion occurs,  then  it  is  unspecified  whether  the  returned  buffer
40       includes a terminating null byte.
41

RETURN VALUE

43       On  success,  zero is returned.  On error, -1 is returned, and errno is
44       set appropriately.
45

ERRORS

47       EFAULT name is an invalid address.
48
49       EINVAL len is negative or, for sethostname(), len is  larger  than  the
50              maximum allowed size.
51
52       ENAMETOOLONG
53              (glibc  gethostname())  len  is  smaller  than  the actual size.
54              (Before version 2.1, glibc uses EINVAL for this case.)
55
56       EPERM  For sethostname(), the caller did  not  have  the  CAP_SYS_ADMIN
57              capability  in the user namespace associated with its UTS names‐
58              pace (see namespaces(7)).
59

CONFORMING TO

61       SVr4,  4.4BSD    (these   interfaces   first   appeared   in   4.2BSD).
62       POSIX.1-2001  and  POSIX.1-2008  specify gethostname() but not sethost‐
63       name().
64

NOTES

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
69       defined  with  the  value  64, which has been the limit since Linux 1.0
70       (earlier 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;
74       instead,  it  implements gethostname() as a library function that calls
75       uname(2) and copies up to len bytes from the  returned  nodename  field
76       into  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
81       Versions of glibc before 2.2 handle the case where the  length  of  the
82       nodename  was  greater  than  or  equal  to len differently: nothing is
83       copied into name and the function returns -1 with errno set  to  ENAME‐
84       TOOLONG.
85

SEE ALSO

87       hostname(1), getdomainname(2), setdomainname(2), uname(2)
88

COLOPHON

90       This  page  is  part of release 5.02 of the Linux man-pages project.  A
91       description of the project, information about reporting bugs,  and  the
92       latest     version     of     this    page,    can    be    found    at
93       https://www.kernel.org/doc/man-pages/.
94
95
96
97Linux                             2017-09-15                    GETHOSTNAME(2)
Impressum