gethostname(2) System Calls Manual gethostname(2) gethostname, sethostname - / C (libc, -lc) #include int gethostname(char *name, size_t len); int sethostname(const char *name, size_t len); 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) . , , UTS . sethostname() , name. len name. ( , name null.) gethostname() null name len . , null, , ( ). POSIX.1 , , , null. 0. -1, errno . EFAULT name . EINVAL len sethostname() len , . ENAMETOOLONG (glibc gethostname()) len is smaller than the actual size. (Before glibc 2.1, glibc uses EINVAL for this case.) EPERM sethostname() CAP_SYS_ADMIN , UTS ( namespaces(7)). SUSv2 , << 255- >>. POSIX.1 , << ( ) HOST_NAME_MAX >>. Linux HOST_NAME_MAX 64, Linux 1.0 ( 8 ). C GNU C library gethostname(); gethostname() , uname(2) len name nodename. , , nodename len, , -1, errno ENAMETOOLONG; name null . gethostname() POSIX.1-2008. sethostname() None. SVr4, 4.4BSD ( 4.2BSD). POSIX.1-2001 POSIX.1-2008 gethostname(), sethostname(). Versions of glibc before glibc 2.2 handle the case where the length of the nodename was greater than or equal to len 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) () Azamat Hackimov , Dmitry Bolkhovskikh , Vladislav , Yuri Kozlov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 2 2024 . gethostname(2)