getrandom(2)
๐ NAME ๐
okay so bestie, meet getrandom(2) โ she's chilling in the System Calls Manual, and her whole job is to obtain a series of random bytes. it's giving entropy. it's giving cryptographic main character energy. she ate. ๐
โจ
๐ LIBRARY ๐
Standard C library (libc, -lc) โ she's right there in your libc, no extra shopping required ๐ซถ
๐ฆ SYNOPSIS ๐ฆ
#include <sys/random.h>
ssize_t getrandom(size_t size;
void buf[size], size_t size, unsigned int flags);
๐ DESCRIPTION ๐
So the getrandom() system call fills the buffer that buf points to with up to size random bytes. and what are those bytes even for?? seeding user-space random number generators, or for cryptographic purposes. literally both, and both are iconic. ๐ฅ
By default, getrandom() pulls its entropy from the urandom source โ that's the same source as the /dev/urandom device, iykyk. but you can change that behaviour with the flags argument, no cap.
Now here's the tea on guarantees โ if the urandom source has been initialized, reads of up to 256 bytes will always return as many bytes as requested, and they will not be interrupted by signals. obsessed. but for larger buffer sizes? no such guarantees apply, bestie, none at all. like, if the call gets interrupted by a signal handler, it may return a partially filled buffer, or it may just fail with the error EINTR. ๐ญ
and if the urandom source has not yet been initialized?? then getrandom() will block, unless GRND_NONBLOCK is specified in flags. she waits. she's patient. she knows what she's worth. ๐
The flags argument is a bit mask that can contain zero or more of the following values, ORed together:
GRND_RANDOMโ set this bit and the random bytes get drawn from the random source (i.e., the same source as the/dev/randomdevice) instead of the urandom source. the random source is limited based on the entropy that can be obtained from environmental noise โ she's on a budget ๐. if the number of available bytes in the random source is less than what was requested insize, the call returns just the available random bytes. and if no random bytes are available at all, the behaviour depends on whetherGRND_NONBLOCKis present in theflagsargument.GRND_NONBLOCKโ by default, when reading from the random source,getrandom()blocks if no random bytes are available, and when reading from the urandom source, it blocks if the entropy pool has not yet been initialized. but if theGRND_NONBLOCKflag is set, thengetrandom()does not block in these cases โ instead it immediately returns-1witherrnoset toEAGAIN. no waiting, no drama, just vibes. ๐ซถ
โญ RETURN VALUE โญ
On success, getrandom() returns the number of bytes that were copied to the buffer buf. and heads up: that number may be less than the number of bytes requested via size, if either GRND_RANDOM was specified in flags and insufficient entropy was present in the random source, or the system call was interrupted by a signal. ๐
On error, -1 is returned, and errno is set to indicate the error. periodt.
๐ ERRORS ๐
EAGAINโ the requested entropy was not available, andgetrandom()would have blocked if theGRND_NONBLOCKflag was not set.EFAULTโ the address referred to bybufis outside the accessible address space. not accessible, not invited. ๐ซEINTRโ the call was interrupted by a signal handler; peep the description of how interruptedread(2)calls on "slow" devices are handled with and without theSA_RESTARTflag over in thesignal(7)man page.EINVALโ an invalid flag was specified inflags. bestie. why. ๐ญENOSYSโ the glibc wrapper function forgetrandom()determined that the underlying kernel does not implement this system call. sadly, no bytes today. ๐
๐ธ STANDARDS ๐ธ
Linux. that's the whole list. she's a Linux girl and she owns it. ๐
๐ง HISTORY ๐ง
Linux 3.17, glibc 2.25. vintage but still thriving, we love a legend. ๐
๐ NOTES ๐
For an overview and comparison of the various interfaces that can be used to obtain randomness, check out random(7). โจ
Here's a fun fact: unlike /dev/random and /dev/urandom, getrandom() does not involve the use of pathnames or file descriptors. so getrandom() can be useful in cases where chroot(2) makes /dev pathnames invisible, and where an application (e.g., a daemon during start-up) closes a file descriptor for one of these files that was opened by a library. no path, no fd, no problem. ๐ฆ
๐ Maximum number of bytes returned ๐
As of Linux 3.19 the following limits apply:
- When reading from the urandom source, a maximum of
32Mi-1bytes is returned by a single call togetrandom()on systems whereinthas a size of 32 bits. - When reading from the random source, a maximum of 512 bytes is returned.
๐ Interruption by a signal handler ๐
When reading from the urandom source (GRND_RANDOM is not set), getrandom() will block until the entropy pool has been initialized (unless the GRND_NONBLOCK flag was specified, ofc). and if a request is made to read a large number of bytes โ more than 256 โ getrandom() will block until those bytes have been generated and transferred from kernel memory to buf. when reading from the random source (GRND_RANDOM is set), getrandom() will block until some random bytes become available (unless the GRND_NONBLOCK flag was specified). she waits, then she delivers. ๐
The behaviour when a call to getrandom() that is blocked while reading from the urandom source is interrupted by a signal handler depends on the initialization state of the entropy buffer and on the request size, size. so, breaking it down:
- if the entropy is not yet initialized, then the call fails with the
EINTRerror. - if the entropy pool has been initialized and the request size is large (
size > 256), the call either succeeds โ returning a partially filled buffer โ or fails with the errorEINTR. - if the entropy pool has been initialized and the request size is small (
size <= 256), thengetrandom()will not fail withEINTR. instead, it will return all of the bytes that have been requested. iconic behaviour honestly. โจ
When reading from the random source, blocking requests of any size can be interrupted by a signal handler โ the call fails with the error EINTR. ๐ญ
Using getrandom() to read small buffers (<= 256 bytes) from the urandom source is the preferred mode of usage. that's the way, bestie. ๐ซถ
The special treatment of small values of size was designed for compatibility with OpenBSD's getentropy(3), which is nowadays supported by glibc. a cute little cross-platform moment. ๐
okay real talk for a sec: the user ofgetrandom()must always check the return value, to determine whether either an error occurred or fewer bytes than requested were returned. in the case whereGRND_RANDOMis not specified andsizeis less than or equal to 256, a return of fewer bytes than requested should never happen โ but the careful programmer will check for this anyway! ๐ not optional, bestie. check it.
๐ BUGS ๐
As of Linux 3.19, the following bug exists:
- Depending on CPU load,
getrandom()does not react to interrupts before reading all bytes requested. ๐ she is locked in. perhaps too locked in.
๐ SEE ALSO ๐
and that's the whole page, bestie ๐ this was Linux man-pages 6.20, dated 2026-02-08 โ now go get your random bytes, you've got this, and remember: always check the return value. slay. ๐๐บโญ
๐ฆ 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 2014, Theodore Ts'o <tytso@mit.edu>
- Copyright 2015, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms