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

DESCRIPTION

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

RETURN VALUE

45       On success, zero is returned.  On error, -1 is returned, and  errno  is
46       set to indicate the error.
47

ERRORS

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

CONFORMING TO

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

NOTES

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

SEE ALSO

89       hostname(1), getdomainname(2),  setdomainname(2),  uname(2),  uts_name‐
90       spaces(7)
91

COLOPHON

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