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 array name. the size argument tells it exactly how many bytes are in name. (so like โ€” name does 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 array name, which is size bytes 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, -1 is returned, and errno is set to tell you what went wrong. ๐Ÿ‘‘

๐Ÿ’… ERRORS ๐Ÿ’…

  • EFAULT โ€” name is an invalid address. like girl, that pointer is not pointing anywhere real. ๐Ÿ’€
  • 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. (and fun history fact: before glibc 2.1, glibc used EINVAL for this case. the more you know โœจ)
  • 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)). 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_MAX bytes".
  • on Linux, HOST_NAME_MAX is defined with the value 64, 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 nodename was greater than or equal to size differently: nothing is copied into name and the function returns -1 with errno set to ENAMETOOLONG. 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms