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

NAME

6       gethostname, sethostname - get/set host name
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

DESCRIPTION

15       These system calls are used to access or to change the host name of the
16       current processor.  The gethostname() system call returns a null-termi‐
17       nated  hostname  (set  earlier by sethostname()) in the array name that
18       has a length of len bytes.  In case the null-terminated  hostname  does
19       not  fit,  no  error  is returned, but the hostname is truncated. It is
20       unspecified whether the truncated hostname will be null-terminated.
21

RETURN VALUE

23       On success, zero is returned.  On error, -1 is returned, and  errno  is
24       set appropriately.
25

ERRORS

27       EFAULT name is an invalid address.
28
29       EINVAL len  is  negative  or, for sethostname(), len is larger than the
30              maximum allowed size, or, for gethostname() on  Linux/i386,  len
31              is  smaller  than the actual size.  (In this last case glibc 2.1
32              uses ENAMETOOLONG.)
33
34       EPERM  For sethostname(), the caller did  not  have  the  CAP_SYS_ADMIN
35              capability.
36

CONFORMING TO

38       SVr4,   4.4BSD    (these   interfaces   first   appeared   in  4.2BSD).
39       POSIX.1-2001 specifies gethostname() but not sethostname().
40

NOTES

42       SUSv2  guarantees  that  `Host  names  are  limited  to   255   bytes'.
43       POSIX.1-2001 guarantees that `Host names (not including the terminating
44       null byte) are limited to HOST_NAME_MAX bytes'.
45

GLIBC NOTES

47       The GNU C library implements gethostname() as a library  function  that
48       calls  uname(2)  and  copies up to len bytes from the returned nodename
49       field into name.  Having performed the copy, the function  then  checks
50       if  the length of the nodename was greater than or equal to len, and if
51       it is, then the function returns -1 with  errno  set  to  ENAMETOOLONG.
52       Versions  of  glibc  before 2.2 handle the case where the length of the
53       nodename was greater than or  equal  to  len  differently:  nothing  is
54       copied  into  name and the function returns -1 with errno set to ENAME‐
55       TOOLONG.
56

SEE ALSO

58       getdomainname(2), setdomainname(2), uname(2)
59
60
61
62Linux 2.6.7                       2004-06-17                    GETHOSTNAME(2)
Impressum