gethostname
Section: System Calls (2)
Updated: 2026-02-08
Index
JM Home Page
roff page
名前
gethostname, sethostname - ホスト名の取得・設定をする
ライブラリ
標準 C ライブラリ (libc,~-lc)
書式
#include <unistd.h>
int gethostname(char *name, size_t size);
int sethostname(const char *name, size_t size);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
gethostname():
_XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
|| /* glibc 2.19 and earlier */ _BSD_SOURCE
sethostname():
glibc 2.21 以降:
_DEFAULT_SOURCE
glibc 2.19 と 2.20:
_DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
glibc 2.19 以前:
_BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
説明
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.
sethostname() sets the hostname to the value given in the character
array name. The size argument specifies the number of bytes in
name. (Thus, name does not require a terminating null byte.)
gethostname() returns the null-terminated hostname in the character
array name, which has a size of size bytes. If the null-terminated
hostname is too large to fit, then the name is truncated, and no error is
returned (but see VERSIONS below). POSIX.1 says that if such truncation
occurs, then it is unspecified whether the returned buffer includes a
terminating null byte.
返り値
成功すると、0 を返す。 エラーの場合、-1 を返し、 errno にエラーを示す値を設定する。
エラー
- EFAULT
-
name が不正なアドレスである。
- EINVAL
-
size is negative or, for sethostname(), size is larger than the
maximum allowed size.
- ENAMETOOLONG
-
(glibc gethostname()) size is smaller than the actual size. (Before
glibc 2.1, glibc uses EINVAL for this case.)
- EPERM
-
For sethostname(), the caller did not have the CAP_SYS_ADMIN
capability in the user namespace associated with its UTS namespace (see
namespaces(7)).
バージョン
SUSv2 では「ホスト名が 255 バイトに制限される」ことを保証している。 POSIX.1 では「ホスト名 (終端のヌルバイトは含まない) が
HOST_NAME_MAX バイトに制限される」ことを保証している。 Linux では、 HOST_NAME_MAX は 64
に定義されており、 Linux 1.0 以降ではこれが上限となってきた (もっと古いカーネルでは 8 バイトの上限が適用されていた)。
C ライブラリとカーネルの違い
The GNU C library does not employ the gethostname() system call;
instead, it implements gethostname() as a library function that calls
uname(2) and copies up to size bytes from the returned nodename
field into name. Having performed the copy, the function then checks if
the length of the nodename was greater than or equal to size, and if
it is, then the function returns -1 with errno set to ENAMETOOLONG; in
this case, a terminating null byte is not included in the returned name.
標準規格
- gethostname()
-
POSIX.1-2024.
- sethostname()
-
なし。
歴史
- gethostname()
-
4.2BSD, SVr4, POSIX.1-2001.
- sethostname()
-
4.2BSD, SVr4.
Versions of glibc before glibc 2.2 handle the case where the length of the
nodename was greater than or equal to size differently: nothing is
copied into name and the function returns -1 with errno set to
ENAMETOOLONG.
関連項目
hostname(1), getdomainname(2), setdomainname(2), uname(2),
uts_namespaces(7)
Index
- 名前
-
- ライブラリ
-
- 書式
-
- 説明
-
- 返り値
-
- エラー
-
- バージョン
-
- C ライブラリとカーネルの違い
-
- 標準規格
-
- 歴史
-
- 関連項目
-
This document was created by
man2html,
using the manual pages.