]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man4/random.4
Various pages: [GPL-1.0-or-later] Use SPDX-License-Identifier
[thirdparty/man-pages.git] / man4 / random.4
CommitLineData
fea681da
MK
1.\" Copyright (c) 1997 John S. Kallal (kallal@voicenet.com)
2.\"
89e3ffe9 3.\" %%%LICENSE_START(GPLv2+_DOC_ONEPARA)
fea681da
MK
4.\" This is free documentation; you can redistribute it and/or
5.\" modify it under the terms of the GNU General Public License as
6.\" published by the Free Software Foundation; either version 2 of
7.\" the License, or (at your option) any later version.
8f8359d8 8.\" %%%LICENSE_END
fea681da
MK
9.\"
10.\" Some changes by tytso and aeb.
11.\"
8deb0f0d 12.\" 2004-12-16, John V. Belmonte/mtk, Updated init and quit scripts
704a18f0 13.\" 2004-04-08, AEB, Improved description of read from /dev/urandom
9ed0f081
MK
14.\" 2008-06-20, George Spelvin <linux@horizon.com>,
15.\" Matt Mackall <mpm@selenic.com>
8deb0f0d 16.\"
1d767b55 17.TH RANDOM 4 2021-03-22 "Linux" "Linux Programmer's Manual"
fea681da
MK
18.SH NAME
19random, urandom \- kernel random number source devices
5a933be0 20.SH SYNOPSIS
c7db92b9 21.nf
5a933be0 22#include <linux/random.h>
68e4db0a 23.PP
5a933be0 24.BI "int ioctl(" fd ", RND" request ", " param ");"
c7db92b9 25.fi
fea681da 26.SH DESCRIPTION
c13182ef 27The character special files \fI/dev/random\fP and
8478ee02 28\fI/dev/urandom\fP (present since Linux 1.3.30)
c13182ef 29provide an interface to the kernel's random number generator.
af0b0990 30The file
11bda441 31.I /dev/random
af0b0990
MK
32has major device number 1 and minor device number 8.
33The file
34.I /dev/urandom
35has major device number 1 and minor device number 9.
dd3568a1 36.PP
c13182ef
MK
37The random number generator gathers environmental noise
38from device drivers and other sources into an entropy pool.
39The generator also keeps an estimate of the
fea681da 40number of bits of noise in the entropy pool.
82f0a1f9 41From this entropy pool, random numbers are created.
dd3568a1 42.PP
724d21bb 43Linux 3.17 and later provides the simpler and safer
f64f220c 44.BR getrandom (2)
724d21bb
MK
45interface which requires no special files;
46see the
47.BR getrandom (2)
48manual page for details.
2d444feb 49.PP
f64f220c
MK
50When read, the
51.I /dev/urandom
52device returns random bytes using a pseudorandom
53number generator seeded from the entropy pool.
6a0c1add 54Reads from this device do not block (i.e., the CPU is not yielded),
e6e25836 55but can incur an appreciable delay when requesting large amounts of data.
2d444feb 56.PP
daaeba95 57When read during early boot time,
bb02bcef 58.IR /dev/urandom
daaeba95 59may return data prior to the entropy pool being initialized.
8888079c
MK
60.\" This is a real problem; see
61.\" commit 9b4d008787f864f17d008c9c15bbe8a0f7e2fc24
67b7fcba 62If this is of concern in your application, use
f64f220c 63.BR getrandom (2)
67b7fcba 64or \fI/dev/random\fP instead.
2d444feb 65.PP
67b7fcba
NM
66The \fI/dev/random\fP device is a legacy interface which dates back to
67a time where the cryptographic primitives used in the implementation
f64f220c
MK
68of \fI/dev/urandom\fP were not widely trusted.
69It will return random bytes only within the estimated number of
70bits of fresh noise in the entropy pool, blocking if necessary.
c5d010eb 71\fI/dev/random\fP is suitable for applications that need
67b7fcba 72high quality randomness, and can afford indeterminate delays.
2d444feb 73.PP
c13182ef 74When the entropy pool is empty, reads from \fI/dev/random\fP will block
fea681da 75until additional environmental noise is gathered.
17344691
HS
76If
77.BR open (2)
78is called for
79.I /dev/random
724d21bb
MK
80with the
81.BR O_NONBLOCK
82flag, a subsequent
17344691
HS
83.BR read (2)
84will not block if the requested number of bytes is not available.
e8cc3c09 85Instead, the available bytes are returned.
792bb5ad 86If no byte is available,
17344691 87.BR read (2)
0a23e9aa 88will return \-1 and
17344691
HS
89.I errno
90will be set to
91.BR EAGAIN .
2d444feb 92.PP
724d21bb 93The
17344691 94.B O_NONBLOCK
724d21bb 95flag has no effect when opening
17344691
HS
96.IR /dev/urandom .
97When calling
98.BR read (2)
792bb5ad
MK
99for the device
100.IR /dev/urandom ,
dbf63eed
MK
101reads of up to 256 bytes will return as many bytes as are requested
102and will not be interrupted by a signal handler.
103Reads with a buffer over this limit may return less than the
104requested number of bytes or fail with the error
4818990d
MK
105.BR EINTR ,
106if interrupted by a signal handler.
2d444feb 107.PP
866fa681
MK
108Since Linux 3.16,
109.\" commit 79a8468747c5f95ed3d5ce8376a3e82e0c5857fc
110a
111.BR read (2)
112from
113.IR /dev/urandom
c4b7e5ac 114will return at most 32\ MB.
44da0e24
MK
115A
116.BR read (2)
117from
118.IR /dev/random
119will return at most 512 bytes
120.\" SEC_XFER_SIZE in drivers/char/random.c
121(340 bytes on Linux kernels before version 2.6.12).
2d444feb 122.PP
5a933be0
EDB
123Writing to \fI/dev/random\fP or \fI/dev/urandom\fP will update the
124entropy pool with the data written, but this will not result in a
ece967c9
MK
125higher entropy count.
126This means that it will impact the contents
127read from both files, but it will not make reads from
5a933be0 128\fI/dev/random\fP faster.
9ed0f081 129.SS Usage
67b7fcba 130The
9ed0f081 131.IR /dev/random
67b7fcba 132interface is considered a legacy interface, and
724d21bb 133.IR /dev/urandom
67b7fcba
NM
134is preferred and sufficient in all use cases, with the exception of
135applications which require randomness during early boot time; for
f64f220c
MK
136these applications,
137.BR getrandom (2)
138must be used instead,
139because it will block until the entropy pool is initialized.
2d444feb 140.PP
136b7eaf
MK
141If a seed file is saved across reboots as recommended below,
142the output is
9ed0f081
MK
143cryptographically secure against attackers without local root access as
144soon as it is reloaded in the boot sequence, and perfectly adequate for
145network encryption session keys.
136b7eaf
MK
146(All major Linux distributions have saved the seed file across reboots
147since 2000 at least.)
e7dfa844 148Since reads from
9ed0f081 149.I /dev/random
ff40dbb3 150may block, users will usually want to open it in nonblocking mode
e7dfa844
MK
151(or perform a read with timeout),
152and provide some sort of user notification if the desired
9ed0f081 153entropy is not immediately available.
2e1f8bfa 154.\"
8eb40c9c 155.SS Configuration
fea681da 156If your system does not have
c13182ef 157\fI/dev/random\fP and \fI/dev/urandom\fP created already, they
fea681da 158can be created with the following commands:
2d444feb 159.PP
9c40f2b9
MK
160.in +4n
161.EX
162mknod \-m 666 /dev/random c 1 8
163mknod \-m 666 /dev/urandom c 1 9
164chown root:root /dev/random /dev/urandom
165.EE
166.in
2d444feb 167.PP
c13182ef 168When a Linux system starts up without much operator interaction,
fea681da 169the entropy pool may be in a fairly predictable state.
c13182ef
MK
170This reduces the actual amount of noise in the entropy pool
171below the estimate.
172In order to counteract this effect, it helps to carry
173entropy pool information across shut-downs and start-ups.
3f4a2adb 174To do this, add the lines to an appropriate script
c13182ef 175which is run during the Linux system start-up sequence:
2d444feb 176.PP
9c40f2b9
MK
177.in +4n
178.EX
179echo "Initializing random number generator..."
d064d41a
MK
180random_seed=/var/run/random\-seed
181# Carry a random seed from start\-up to start\-up
9c40f2b9
MK
182# Load and then save the whole entropy pool
183if [ \-f $random_seed ]; then
184 cat $random_seed >/dev/urandom
185else
186 touch $random_seed
187fi
188chmod 600 $random_seed
189poolfile=/proc/sys/kernel/random/poolsize
190[ \-r $poolfile ] && bits=$(cat $poolfile) || bits=4096
191bytes=$(expr $bits / 8)
192dd if=/dev/urandom of=$random_seed count=1 bs=$bytes
193.EE
194.in
2d444feb 195.PP
c13182ef 196Also, add the following lines in an appropriate script which is
fea681da 197run during the Linux system shutdown:
2d444feb 198.PP
9c40f2b9
MK
199.in +4n
200.EX
d064d41a 201# Carry a random seed from shut\-down to start\-up
9c40f2b9
MK
202# Save the whole entropy pool
203echo "Saving random seed..."
d064d41a 204random_seed=/var/run/random\-seed
9c40f2b9
MK
205touch $random_seed
206chmod 600 $random_seed
207poolfile=/proc/sys/kernel/random/poolsize
208[ \-r $poolfile ] && bits=$(cat $poolfile) || bits=4096
209bytes=$(expr $bits / 8)
210dd if=/dev/urandom of=$random_seed count=1 bs=$bytes
211.EE
212.in
2d444feb 213.PP
3f4a2adb
MK
214In the above examples, we assume Linux 2.6.0 or later, where
215.IR /proc/sys/kernel/random/poolsize
216returns the size of the entropy pool in bits (see below).
388ee0f4
MK
217.\"
218.SS /proc interfaces
fea681da
MK
219The files in the directory
220.I /proc/sys/kernel/random
388ee0f4 221(present since 2.3.16) provide additional information about the
8478ee02 222.I /dev/random
388ee0f4
MK
223device:
224.TP
fea681da 225.I entropy_avail
05babf32
MK
226This read-only file gives the available entropy, in bits.
227This will be a number in the range 0 to 4096.
388ee0f4 228.TP
fea681da 229.I poolsize
388ee0f4 230This file
c13182ef 231gives the size of the entropy pool.
da84883c
MK
232The semantics of this file vary across kernel versions:
233.RS
388ee0f4 234.TP
da84883c
MK
235Linux 2.4:
236This file gives the size of the entropy pool in
237.IR bytes .
238Normally, this file will have the value 512, but it is writable,
239and can be changed to any value for which an algorithm is available.
240The choices are 32, 64, 128, 256, 512, 1024, or 2048.
241.TP
0f1e6153 242Linux 2.6 and later:
da84883c
MK
243This file is read-only, and gives the size of the entropy pool in
244.IR bits .
245It contains the value 4096.
246.RE
388ee0f4 247.TP
fea681da 248.I read_wakeup_threshold
388ee0f4 249This file
fea681da
MK
250contains the number of bits of entropy required for waking up processes
251that sleep waiting for entropy from
31e9a9ec 252.IR /dev/random .
fea681da 253The default is 64.
388ee0f4 254.TP
fea681da 255.I write_wakeup_threshold
388ee0f4 256This file
fea681da
MK
257contains the number of bits of entropy below which we wake up
258processes that do a
5e21af3a 259.BR select (2)
fea681da 260or
5e21af3a 261.BR poll (2)
fea681da 262for write access to
31e9a9ec 263.IR /dev/random .
fea681da 264These values can be changed by writing to the files.
388ee0f4
MK
265.TP
266.IR uuid " and " boot_id
267These read-only files
fea681da
MK
268contain random strings like 6fd5a44b-35f4-4ad4-a9b9-6b9be13e1fe9.
269The former is generated afresh for each read, the latter was
270generated once.
388ee0f4 271.\"
5a933be0
EDB
272.SS ioctl(2) interface
273The following
274.BR ioctl (2)
275requests are defined on file descriptors connected to either \fI/dev/random\fP
ece967c9
MK
276or \fI/dev/urandom\fP.
277All requests performed will interact with the input
5a933be0
EDB
278entropy pool impacting both \fI/dev/random\fP and \fI/dev/urandom\fP.
279The
280.B CAP_SYS_ADMIN
281capability is required for all requests except
ece967c9 282.BR RNDGETENTCNT .
5a933be0
EDB
283.TP
284.BR RNDGETENTCNT
285Retrieve the entropy count of the input pool, the contents will be the same
286as the
287.I entropy_avail
288file under proc.
289The result will be stored in the int pointed to by the argument.
290.TP
291.BR RNDADDTOENTCNT
ece967c9
MK
292Increment or decrement the entropy count of the input pool
293by the value pointed to by the argument.
5a933be0
EDB
294.TP
295.BR RNDGETPOOL
296Removed in Linux 2.6.9.
297.TP
298.BR RNDADDENTROPY
ece967c9
MK
299Add some additional entropy to the input pool,
300incrementing the entropy count.
301This differs from writing to \fI/dev/random\fP or \fI/dev/urandom\fP,
302which only adds some
303data but does not increment the entropy count.
304The following structure is used:
5a933be0 305.IP
010c75a2
MK
306.in +4n
307.EX
308struct rand_pool_info {
309 int entropy_count;
310 int buf_size;
311 __u32 buf[0];
312};
313.EE
314.in
5a933be0
EDB
315.IP
316Here
317.I entropy_count
988bd008 318is the value added to (or subtracted from) the entropy count, and
5a933be0
EDB
319.I buf
320is the buffer of size
321.I buf_size
322which gets added to the entropy pool.
323.TP
324.BR RNDZAPENTCNT ", " RNDCLEARPOOL
325Zero the entropy count of all pools and add some system data (such as
326wall clock) to the pools.
f2910c72 327.SH FILES
323e1feb 328.I /dev/random
f2910c72 329.br
323e1feb 330.I /dev/urandom
379df7a0
MK
331.SH NOTES
332For an overview and comparison of the various interfaces that
333can be used to obtain randomness, see
334.BR random (7).
f64f220c 335.SH BUGS
debb291e 336During early boot time, reads from
f64f220c
MK
337.I /dev/urandom
338may return data prior to the entropy pool being initialized.
dc919d09
MK
339.\" .SH AUTHOR
340.\" The kernel's random number generator was written by
341.\" Theodore Ts'o (tytso@athena.mit.edu).
47297adb 342.SH SEE ALSO
59732c2a 343.BR mknod (1),
379df7a0
MK
344.BR getrandom (2),
345.BR random (7)
2d444feb 346.PP
331da7c3 347RFC\ 1750, "Randomness Recommendations for Security"