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 system hostname.
29       More precisely, they operate on the hostname associated with the  call‐
30       ing process's UTS namespace.
31
32       sethostname()  sets  the  hostname  to the value given in the character
33       array name.  The len argument specifies the number of  bytes  in  name.
34       (Thus, name does not require a terminating null byte.)
35
36       gethostname()  returns  the  null-terminated  hostname in the character
37       array name, which has a length of len bytes.   If  the  null-terminated
38       hostname  is too large to fit, then the name is truncated, and no error
39       is returned (but see NOTES below).  POSIX.1 says that if  such  trunca‐
40       tion  occurs,  then  it  is  unspecified  whether  the  returned buffer
41       includes a terminating null byte.
42

RETURN VALUE

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

ERRORS

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

CONFORMING TO

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

NOTES

67       SUSv2  guarantees  that "Host names are limited to 255 bytes".  POSIX.1
68       guarantees that "Host names (not including the terminating  null  byte)
69       are  limited  to  HOST_NAME_MAX  bytes".   On  Linux,  HOST_NAME_MAX is
70       defined with the value 64, which has been the  limit  since  Linux  1.0
71       (earlier kernels imposed a limit of 8 bytes).
72
73   C library/kernel differences
74       The  GNU  C  library  does  not  employ  the gethostname() system call;
75       instead, it implements gethostname() as a library function  that  calls
76       uname(2)  and  copies  up to len bytes from the returned nodename field
77       into name.  Having performed the copy, the function then checks if  the
78       length  of the nodename was greater than or equal to len, and if it is,
79       then the function returns -1 with errno set to  ENAMETOOLONG;  in  this
80       case, a terminating null byte is not included in the returned name.
81
82       Versions  of  glibc  before 2.2 handle the case where the length of the
83       nodename was greater than or  equal  to  len  differently:  nothing  is
84       copied  into  name and the function returns -1 with errno set to ENAME‐
85       TOOLONG.
86

SEE ALSO

88       hostname(1), getdomainname(2), setdomainname(2),  uname(2),  uts_names‐
89       paces(7)
90

COLOPHON

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