gethostname(2)
๐ gethostname(2) โ bestie edition ๐
okay so. welcome to the gethostname(2) page, which lives in the System Calls Manual section of your friendly neighborhood man pages, and honestly? it's giving short but iconic. we're talking about the hostname of your machine, the little name it goes by, and how you read it or change it. grab a snack ๐, this one's a quick read.
๐ธ NAME ๐ธ
gethostname, sethostname โ get/set hostname. yes bestie, that's literally it. one gets, one sets. the duality ๐
๐ LIBRARY ๐
Standard C library (libc, -lc). the classic. the blueprint. the one you link without thinking about it ๐ซถ
โจ SYNOPSIS โจ
#include <unistd.h>
int gethostname(char *name, size_t size);
int sethostname(const char *name, size_t size);
๐ Feature Test Macro Requirements for glibc (see feature_test_macros(7)) ๐
okay so this part is the "do I even get to use this function" vibe check. glibc is picky, and you have to define the right macro or it simply will not let you in:
gethostname():
_XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
|| /* glibc 2.19 and earlier */ _BSD_SOURCE
sethostname():
Since glibc 2.21:
_DEFAULT_SOURCE
In glibc 2.19 and 2.20:
_DEFAULT_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
Up to and including glibc 2.19:
_BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
not me dropping a whole boolean expression on you ๐ญ but like, #define it and move on, you got this.
๐ฆ DESCRIPTION ๐ฆ
So these system calls are how you access or change the system hostname. More precisely (and precision is the whole point, no cap), they operate on the hostname that's associated with the calling process's UTS namespace. yes, the namespace. we love a namespaced girly ๐
sethostname()โ sets the hostname to the value you handed it in the character arrayname. thesizeargument tells it exactly how many bytes are inname. (so like โnamedoes not need a terminating null byte. shocking, I know. it's giving "trust me, I counted." ๐ฅ)gethostname()โ hands you back the null-terminated hostname in the character arrayname, which issizebytes big. but here's the twist ๐: if the null-terminated hostname is too big to fit, the name gets truncated and no error is returned (though peep VERSIONS below, bestie, it's important). and POSIX.1 says that when this truncation happens, it is unspecified whether the returned buffer even includes a terminating null byte. so like... undefined. chaotic. do not rely on it ๐
ngl, the truncation thing is a real "read the fine print" moment. size your buffer correctly and you never have to think about it again.
๐ RETURN VALUE ๐
- on success, zero is returned. slay.
- on error,
-1is returned, anderrnois set to tell you what went wrong. ๐
๐ ERRORS ๐
EFAULTโnameis an invalid address. like girl, that pointer is not pointing anywhere real. ๐EINVALโsizeis negative or, forsethostname(),sizeis larger than the maximum allowed size.ENAMETOOLONGโ (glibcgethostname())sizeis smaller than the actual size. (and fun history fact: before glibc 2.1, glibc usedEINVALfor this case. the more you know โจ)EPERMโ forsethostname(), the caller did not have theCAP_SYS_ADMINcapability in the user namespace associated with its UTS namespace (seenamespaces(7)). translation: you need the keys to the kingdom before you go renaming things ๐
๐ง VERSIONS ๐ง
- SUSv2 guarantees that "Host names are limited to 255 bytes".
- POSIX.1 guarantees that "Host names (not including the terminating null byte) are limited to
HOST_NAME_MAXbytes". - on Linux,
HOST_NAME_MAXis defined with the value64, which has been the limit since Linux 1.0 (and earlier kernels imposed a limit of 8 bytes. EIGHT. ancient times fr fr ๐ญ).
๐ฅ C library/kernel differences ๐ฅ
okay this one's the plot twist and you need to know it. 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 done 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. and in that case, a terminating null byte is not included in the returned name. so the userspace version is actually stricter than the raw syscall story. the way that glibc said "I'll handle this myself" ๐
๐ STANDARDS ๐
gethostname()โ POSIX.1-2024.sethostname()โ None. bestie is unstandardized and thriving ๐ธ
๐ HISTORY ๐
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
nodenamewas greater than or equal tosizedifferently: nothing is copied intonameand the function returns-1witherrnoset toENAMETOOLONG. so on old glibc: no partial copy, just a straight-up rejection. periodt.
๐ SEE ALSO ๐
and that's the whole page, bestie ๐ซถ go forth and name your machines responsibly.
Linux man-pages 6.20, dated 2026-02-08 โ ty for coming to my TED talk, mwah ๐โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1993, Rickard E. Faith <faith@cs.unc.edu>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms