man-pages/man2/gethostname.2.html
2021-03-31 01:06:50 +01:00

287 lines
5.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETHOSTNAME</TITLE>
</HEAD><BODY>
<H1>GETHOSTNAME</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-10-10<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
gethostname, sethostname - get/set hostname
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<P>
<B>int gethostname(char *</B><I>name</I><B>, size_t </B><I>len</I><B>);</B>
<BR>
<B>int sethostname(const char *</B><I>name</I><B>, size_t </B><I>len</I><B>);</B>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>gethostname</B>():
<DL COMPACT><DT id="1"><DD>
Since glibc 2.12:
_BSD_SOURCE || _XOPEN_SOURCE&nbsp;&gt;=&nbsp;500
<BR>
|| /* Since glibc 2.12: */ _POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200112L
</DL>
<BR>
<B>sethostname</B>():
<PRE>
Since glibc 2.21:
_DEFAULT_SOURCE
In glibc 2.19 and 2.20:
_DEFAULT_SOURCE || (_XOPEN_SOURCE &amp;&amp; _XOPEN_SOURCE&nbsp;&lt;&nbsp;500)
Up to and including glibc 2.19:
_BSD_SOURCE || (_XOPEN_SOURCE &amp;&amp; _XOPEN_SOURCE&nbsp;&lt;&nbsp;500)
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
These system calls are used to access or to change the system hostname.
More precisely, they operate on the hostname associated with the calling
process's UTS namespace.
<P>
<B>sethostname</B>()
sets the hostname to the value given in the character array
<I>name</I>.
The
<I>len</I>
argument specifies the number of bytes in
<I>name</I>.
(Thus,
<I>name</I>
does not require a terminating null byte.)
<P>
<B>gethostname</B>()
returns the null-terminated hostname in the character array
<I>name</I>,
which has a length of
<I>len</I>
bytes.
If the null-terminated hostname is too large to fit,
then the name is truncated, and no error is returned (but see NOTES below).
POSIX.1 says that if such truncation occurs,
then it is unspecified whether the returned buffer
includes a terminating null byte.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, zero is returned.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="2"><B>EFAULT</B>
<DD>
<I>name</I>
is an invalid address.
<DT id="3"><B>EINVAL</B>
<DD>
<I>len</I>
is negative
or, for
<B>sethostname</B>(),
<I>len</I>
is larger than the maximum allowed size.
<DT id="4"><B>ENAMETOOLONG</B>
<DD>
(glibc <B>gethostname</B>())
<I>len</I>
is smaller than the actual size.
(Before version 2.1, glibc uses
<B>EINVAL</B>
for this case.)
<DT id="5"><B>EPERM</B>
<DD>
For
<B>sethostname</B>(),
the caller did not have the
<B>CAP_SYS_ADMIN</B>
capability in the user namespace associated with its UTS namespace (see
<B><A HREF="/cgi-bin/man/man2html?7+namespaces">namespaces</A></B>(7)).
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
SVr4, 4.4BSD (these interfaces first appeared in 4.2BSD).
POSIX.1-2001 and POSIX.1-2008 specify
<B>gethostname</B>()
but not
<B>sethostname</B>().
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
SUSv2 guarantees that &quot;Host names are limited to 255 bytes&quot;.
POSIX.1 guarantees that &quot;Host names (not including
the terminating null byte) are limited to
<B>HOST_NAME_MAX</B>
bytes&quot;.
On Linux,
<B>HOST_NAME_MAX</B>
is defined with the value 64, which has been the limit since Linux 1.0
(earlier kernels imposed a limit of 8 bytes).
<A NAME="lbAI">&nbsp;</A>
<H3>C library/kernel differences</H3>
The GNU C library does not employ the
<B>gethostname</B>()
system call; instead, it implements
<B>gethostname</B>()
as a library function that calls
<B><A HREF="/cgi-bin/man/man2html?2+uname">uname</A></B>(2)
and copies up to
<I>len</I>
bytes from the returned
<I>nodename</I>
field into
<I>name</I>.
Having performed the copy, the function then checks if the length of the
<I>nodename</I>
was greater than or equal to
<I>len</I>,
and if it is, then the function returns -1 with
<I>errno</I>
set to
<B>ENAMETOOLONG</B>;
in this case, a terminating null byte is not included in the returned
<I>name</I>.
<P>
Versions of glibc before 2.2
handle the case where the length of the
<I>nodename</I>
was greater than or equal to
<I>len</I>
differently: nothing is copied into
<I>name</I>
and the function returns -1 with
<I>errno</I>
set to
<B>ENAMETOOLONG</B>.
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+hostname">hostname</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+getdomainname">getdomainname</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+setdomainname">setdomainname</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+uname">uname</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+uts_namespaces">uts_namespaces</A></B>(7)
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="6"><A HREF="#lbAB">NAME</A><DD>
<DT id="7"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="8"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="9"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="10"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="11"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="12"><A HREF="#lbAH">NOTES</A><DD>
<DL>
<DT id="13"><A HREF="#lbAI">C library/kernel differences</A><DD>
</DL>
<DT id="14"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="15"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:32 GMT, March 31, 2021
</BODY>
</HTML>