{
    "content": [
        {
            "type": "text",
            "text": "# random(4) (man)\n\n**Summary:** random, urandom - kernel random number source devices\n\n**Synopsis:** #include <linux/random.h>\nint ioctl(fd, RNDrequest, param);\n\n## See Also\n\n- mknod(1)\n- getrandom(2)\n\n## Section Outline\n\n- **NAME** (2 lines)\n- **SYNOPSIS** (4 lines)\n- **DESCRIPTION** (45 lines) — 4 subsections\n  - Usage (13 lines)\n  - Configuration (45 lines)\n  - /proc interfaces (35 lines)\n  - ioctl(2) interface (35 lines)\n- **FILES** (3 lines)\n- **NOTES** (3 lines)\n- **BUGS** (3 lines)\n- **SEE ALSO** (4 lines)\n- **COLOPHON** (7 lines)\n\n## Full Content\n\n### NAME\n\nrandom, urandom - kernel random number source devices\n\n### SYNOPSIS\n\n#include <linux/random.h>\n\nint ioctl(fd, RNDrequest, param);\n\n### DESCRIPTION\n\nThe character special files /dev/random and /dev/urandom (present since Linux 1.3.30) provide\nan interface to the kernel's random number generator.  The file /dev/random has major  device\nnumber  1 and minor device number 8.  The file /dev/urandom has major device number 1 and mi‐\nnor device number 9.\n\nThe random number generator gathers environmental noise from device drivers and other sources\ninto an entropy pool.  The generator also keeps an estimate of the number of bits of noise in\nthe entropy pool.  From this entropy pool, random numbers are created.\n\nLinux 3.17 and later provides the simpler and safer getrandom(2) interface which requires  no\nspecial files; see the getrandom(2) manual page for details.\n\nWhen read, the /dev/urandom device returns random bytes using a pseudorandom number generator\nseeded from the entropy pool.  Reads from this device do not block  (i.e.,  the  CPU  is  not\nyielded), but can incur an appreciable delay when requesting large amounts of data.\n\nWhen  read during early boot time, /dev/urandom may return data prior to the entropy pool be‐\ning initialized.  If this is of concern in your application, use getrandom(2) or  /dev/random\ninstead.\n\nThe  /dev/random  device  is  a legacy interface which dates back to a time where the crypto‐\ngraphic primitives used in the implementation of /dev/urandom were not  widely  trusted.   It\nwill  return  random bytes only within the estimated number of bits of fresh noise in the en‐\ntropy pool, blocking if necessary.  /dev/random is suitable for applications that  need  high\nquality randomness, and can afford indeterminate delays.\n\nWhen  the  entropy pool is empty, reads from /dev/random will block until additional environ‐\nmental noise is gathered.  If open(2) is called for /dev/random with the ONONBLOCK  flag,  a\nsubsequent  read(2)  will  not  block if the requested number of bytes is not available.  In‐\nstead, the available bytes are returned.  If no byte is available, read(2) will return -1 and\nerrno will be set to EAGAIN.\n\nThe  ONONBLOCK  flag  has no effect when opening /dev/urandom.  When calling read(2) for the\ndevice /dev/urandom, reads of up to 256 bytes will return as many bytes as are requested  and\nwill  not be interrupted by a signal handler.  Reads with a buffer over this limit may return\nless than the requested number of bytes or fail with the error EINTR,  if  interrupted  by  a\nsignal handler.\n\nSince  Linux  3.16,  a  read(2)  from /dev/urandom will return at most 32 MB.  A read(2) from\n/dev/random will return at most 512 bytes (340 bytes on Linux kernels before version 2.6.12).\n\nWriting to /dev/random or /dev/urandom will update the entropy pool with  the  data  written,\nbut  this will not result in a higher entropy count.  This means that it will impact the con‐\ntents read from both files, but it will not make reads from /dev/random faster.\n\n#### Usage\n\nThe /dev/random interface is considered a legacy interface, and /dev/urandom is preferred and\nsufficient in all use cases, with the exception of applications which require randomness dur‐\ning early boot time; for these applications, getrandom(2) must be used  instead,  because  it\nwill block until the entropy pool is initialized.\n\nIf  a seed file is saved across reboots as recommended below, the output is cryptographically\nsecure against attackers without local root access as soon as it is reloaded in the boot  se‐\nquence, and perfectly adequate for network encryption session keys.  (All major Linux distri‐\nbutions have saved the seed file across reboots since  2000  at  least.)   Since  reads  from\n/dev/random  may  block, users will usually want to open it in nonblocking mode (or perform a\nread with timeout), and provide some sort of user notification if the desired entropy is  not\nimmediately available.\n\n#### Configuration\n\nIf  your  system does not have /dev/random and /dev/urandom created already, they can be cre‐\nated with the following commands:\n\nmknod -m 666 /dev/random c 1 8\nmknod -m 666 /dev/urandom c 1 9\nchown root:root /dev/random /dev/urandom\n\nWhen a Linux system starts up without much operator interaction, the entropy pool may be in a\nfairly  predictable state.  This reduces the actual amount of noise in the entropy pool below\nthe estimate.  In order to counteract this effect, it helps to carry entropy pool information\nacross shut-downs and start-ups.  To do this, add the lines to an appropriate script which is\nrun during the Linux system start-up sequence:\n\necho \"Initializing random number generator...\"\nrandomseed=/var/run/random-seed\n# Carry a random seed from start-up to start-up\n# Load and then save the whole entropy pool\nif [ -f $randomseed ]; then\ncat $randomseed >/dev/urandom\nelse\ntouch $randomseed\nfi\nchmod 600 $randomseed\npoolfile=/proc/sys/kernel/random/poolsize\n[ -r $poolfile ] && bits=$(cat $poolfile) || bits=4096\nbytes=$(expr $bits / 8)\ndd if=/dev/urandom of=$randomseed count=1 bs=$bytes\n\nAlso, add the following lines in an appropriate script which is run during the  Linux  system\nshutdown:\n\n# Carry a random seed from shut-down to start-up\n# Save the whole entropy pool\necho \"Saving random seed...\"\nrandomseed=/var/run/random-seed\ntouch $randomseed\nchmod 600 $randomseed\npoolfile=/proc/sys/kernel/random/poolsize\n[ -r $poolfile ] && bits=$(cat $poolfile) || bits=4096\nbytes=$(expr $bits / 8)\ndd if=/dev/urandom of=$randomseed count=1 bs=$bytes\n\nIn the above examples, we assume Linux 2.6.0 or later, where /proc/sys/kernel/random/poolsize\nreturns the size of the entropy pool in bits (see below).\n\n#### /proc interfaces\n\nThe files in the directory /proc/sys/kernel/random (present since 2.3.16) provide  additional\ninformation about the /dev/random device:\n\nentropyavail\nThis  read-only  file  gives the available entropy, in bits.  This will be a number in\nthe range 0 to 4096.\n\npoolsize\nThis file gives the size of the entropy pool.  The semantics of this file vary  across\nkernel versions:\n\nLinux 2.4:\nThis  file  gives  the  size of the entropy pool in bytes.  Normally, this file\nwill have the value 512, but it is writable, and can be changed  to  any  value\nfor  which  an  algorithm is available.  The choices are 32, 64, 128, 256, 512,\n1024, or 2048.\n\nLinux 2.6 and later:\nThis file is read-only, and gives the size of the entropy  pool  in  bits.   It\ncontains the value 4096.\n\nreadwakeupthreshold\nThis file contains the number of bits of entropy required for waking up processes that\nsleep waiting for entropy from /dev/random.  The default is 64.\n\nwritewakeupthreshold\nThis file contains the number of bits of entropy below which we wake up processes that\ndo  a  select(2)  or  poll(2)  for  write  access to /dev/random.  These values can be\nchanged by writing to the files.\n\nuuid and bootid\nThese       read-only       files       contain       random       strings        like\n6fd5a44b-35f4-4ad4-a9b9-6b9be13e1fe9.   The  former is generated afresh for each read,\nthe latter was generated once.\n\n#### ioctl(2) interface\n\nThe following ioctl(2) requests are defined on file descriptors connected to either /dev/ran‐\ndom  or  /dev/urandom.   All requests performed will interact with the input entropy pool im‐\npacting both /dev/random and /dev/urandom.  The CAPSYSADMIN capability is required for  all\nrequests except RNDGETENTCNT.\n\nRNDGETENTCNT\nRetrieve the entropy count of the input pool, the contents will be the same as the en‐\ntropyavail file under proc.  The result will be stored in the int pointed to  by  the\nargument.\n\nRNDADDTOENTCNT\nIncrement  or decrement the entropy count of the input pool by the value pointed to by\nthe argument.\n\nRNDGETPOOL\nRemoved in Linux 2.6.9.\n\nRNDADDENTROPY\nAdd some additional entropy to the input pool, incrementing the entropy  count.   This\ndiffers  from  writing  to  /dev/random or /dev/urandom, which only adds some data but\ndoes not increment the entropy count.  The following structure is used:\n\nstruct randpoolinfo {\nint    entropycount;\nint    bufsize;\nu32  buf[0];\n};\n\nHere entropycount is the value added to (or subtracted from) the entropy  count,  and\nbuf is the buffer of size bufsize which gets added to the entropy pool.\n\nRNDZAPENTCNT, RNDCLEARPOOL\nZero  the  entropy count of all pools and add some system data (such as wall clock) to\nthe pools.\n\n### FILES\n\n/dev/random\n/dev/urandom\n\n### NOTES\n\nFor an overview and comparison of the various interfaces that can be used to  obtain  random‐\nness, see random(7).\n\n### BUGS\n\nDuring early boot time, reads from /dev/urandom may return data prior to the entropy pool be‐\ning initialized.\n\n### SEE ALSO\n\nmknod(1), getrandom(2), random(7)\n\nRFC 1750, \"Randomness Recommendations for Security\"\n\n### COLOPHON\n\nThis page is part of release 5.10 of the Linux  man-pages  project.   A  description  of  the\nproject,  information about reporting bugs, and the latest version of this page, can be found\nat https://www.kernel.org/doc/man-pages/.\n\n\n\nLinux                                        2017-09-15                                    RANDOM(4)\n\n"
        }
    ],
    "structuredContent": {
        "command": "random",
        "section": "4",
        "mode": "man",
        "summary": "random, urandom - kernel random number source devices",
        "synopsis": "#include <linux/random.h>\nint ioctl(fd, RNDrequest, param);",
        "flags": [],
        "examples": [],
        "see_also": [
            {
                "name": "mknod",
                "section": "1",
                "url": "https://www.chedong.com/phpMan.php/man/mknod/1/json"
            },
            {
                "name": "getrandom",
                "section": "2",
                "url": "https://www.chedong.com/phpMan.php/man/getrandom/2/json"
            }
        ],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 4,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 45,
                "subsections": [
                    {
                        "name": "Usage",
                        "lines": 13
                    },
                    {
                        "name": "Configuration",
                        "lines": 45
                    },
                    {
                        "name": "/proc interfaces",
                        "lines": 35
                    },
                    {
                        "name": "ioctl(2) interface",
                        "lines": 35
                    }
                ]
            },
            {
                "name": "FILES",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "BUGS",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 4,
                "subsections": []
            },
            {
                "name": "COLOPHON",
                "lines": 7,
                "subsections": []
            }
        ]
    }
}