14

If you ran the following, what would happen?

# Do not run.
# cat /dev/random > ~/randomFile

Would it be written until the drive runs out of space, or would the system see a problem with this and stop it (like with an infinite symlink loop)?

tkbx
  • 10,847
  • 5
    I just wanted to add that concatenating random into devices was worth a few hours of fun in my youth. The framebuffer and sound card resulted in noise and the disk drive required me to do a low level reformat... good times. – Bob Roberts Feb 05 '13 at 16:40
  • OT: 'executable /dev/dsp? sounds like fun' – sendmoreinfo Feb 05 '13 at 21:04
  • 1
    @BobRoberts I has similar adventures, with similar outcomes. I also used to ssh into colleagues' linux workstations and cat DTMF tones to the internal speaker, then play a busy tone, so it sounded like their PC was trying to FAX something out. Ah, good times. – Tim Kennedy Feb 06 '13 at 15:59
  • @TimKennedy how do you do that? Is there a /dev/ice for the 30mm audio jack? – tkbx Feb 06 '13 at 16:41
  • @tkbx well, i'm dating myself, but back in the day you could cat a file to /dev/audio, and if it was a sound file (.wav) it would get played. – Tim Kennedy May 01 '13 at 15:24
  • If you are putting dangerous commands up, I would recommend preceding it with an octothorpe as an additional safeguard. – msouth Feb 22 '15 at 03:25

1 Answers1

26

It writes until the disk is full (usually there is still some space reserved for the root user). But as the pool of random data is limited, this could take a while.

If you need a certain amount of random data, use dd. For 1MB:

dd if=/dev/random iflag=fullblock of=$HOME/randomFile bs=1M count=1

Other possibilities are mentioned in answers to a related question.

However, in almost all cases it is better to use /dev/urandom instead. It does not block if the kernel thinks that it get out of entropy. For better understanding, you can also read myths about /dev/urandom.

Installing haveged speeds up /dev/random and also provides more entropy to /dev/urandom.

EDIT: dd needs the fullblock option as /dev/random (in opposite of /dev/urandom) can return incomplete blocks if the entropy pool is empty.

If your dd does not support units, write them out:

dd if=/dev/random iflag=fullblock of=$HOME/randomFile bs=1048576 count=1
jofel
  • 26,758
  • 8
    100 MiB of high-quality random data is a bit much... you should get a few bytes to seed some high-quality pseudo random number generator, like the Mersenne twister. If the application is critical (in a cryptographic sense) you must go and read up on the matter, and perhaps hire an expert. – vonbrand Feb 05 '13 at 14:52
  • 4
    @vonbrand No, reading 100MB from /dev/urandom is fine, there's no reason not to do it. And do not use a Mersenne twister to do crypto. And don't use /dev/random on Linux. – Gilles 'SO- stop being evil' Feb 05 '13 at 21:46
  • 3
    @jofel: No, the output of /dev/urandom is fine for cryptographic use. Do not use /dev/random. – Thomas Pornin Feb 05 '13 at 22:12
  • @Gilles, maybe I'm loosing it... I agree with everything you say, ans can't see where I said anything different. – vonbrand Feb 05 '13 at 22:13
  • @Gilles & vonbrand: Thanks for you comments, I improved my answer (now only 1MB in the example, /dev/urandom recommended). – jofel Feb 06 '13 at 10:02
  • @jofel dd: bs: illegal numeric value, 1000000 works – tkbx Feb 06 '13 at 12:30
  • @tkbx With dd from e.g. GNU coreutils, you can use units like "M". I found, that iflag=fullblock is needed. Answer is updated. – jofel Feb 06 '13 at 13:42
  • I tend to write head -c 123 /dev/urandom to read a given amount of data from a device. I'd expect this to be less affected by the incomplete block scenario. Might be less performant, though. – MvG Feb 15 '13 at 20:32