78
read /dev/urandom 3

The above is not working..How can I read random bytes from /dev/urandom in bash?

linux
  • 1,343
  • 4
  • 13
  • 16

8 Answers8

72
head -c 500 /dev/urandom | tr -dc 'a-zA-Z0-9~!@#$%^&*_-' | fold -w 3 | head -n 1

(If you want literal dash characters, the dash character must go at the end of the string as done above, as opposed to *-_).

And to explain what gets done due to the above set of commands:

  1. head -c 500 /dev/urandom: Obtain the first 500 characters (bytes) from /dev/urandom.
  2. tr -dc 'a-zA-Z0-9~!@#$%^&*_-': Remove all but the characters specified in 'a-zA-Z0-9~!@#$%^&*_-' from the output of the first command.
  3. fold -w 3: Format the output of the second command such that it has 3 characters per line.
  4. head -n 1: Display the first line of the result of the third command on stdout.
ADM
  • 1,413
68
random="$(dd if=/dev/urandom bs=3 count=1)"

if specifies the input file, bs the block size (3 bytes), and count the number of blocks (1 * 3 = 3 total bytes)

Flimzy
  • 2,512
  • 18
  • 26
45

Please check man od.

You can use, for example

od -vAn -N4 -tu4 < /dev/urandom

to generate unsigned decimal 4 bytes random numbers.

Ekrem Aksoy
  • 451
  • 4
  • 2
30

Here's one that creates base64 strings, note that even though they are limited to base64 strings, the padding is removed from them, so you can't decode them, you probably won't need it anyway.

 cat /dev/urandom | base64 | head -c 5

Replace 5 with the number of chars you'd like.

If you however need to decode them for some reason, move base64 to the end of the pipe. It will then collect 5 chars from urandom and base64 encode it with the right padding, but the final string may be longer than what you wanted due to padding.

cat /dev/urandom | head -c 5 | base64
15

The easiest solution would be as simple as:

$ head -cN /dev/urandom
MadHatter
  • 81,580
Spack
  • 1,626
11

Try this: dd if=/dev/urandom bs=1 count=3

If you want to put the result in $variable:

variable=`dd if=/dev/urandom bs=1 count=3`

Do note that it'll probably not be printable.

1

The following command generates random characters

</dev/urandom tr -dc 'A-Za-z0-9!"#$%&\(\)*+,./: ;<=>?@\[\]^_`{|}~-' | head -c 15  ; echo

from all the ASCII Printable characters and only takes 15 head characters (or any number that you specifies) from it.

0

Here's a tiny one-liner that generates a random 32-bit integer in hexadecimal. It's POSIX shell script too.

tr -dC '[:xdigit:]' </dev/urandom | head -c8

To generate a random 32-bit decimal, use arithmetic (which all shells support too):

echo $((0x$(tr -dC '[:xdigit:]' </dev/urandom | head -c8)))

Contrary to misinformation spread above, have no fear of this ever running out of entropy! It's /dev/urandom, not /dev/random, and employs a chacha20 cipher arbitrarily re-seeded from /dev/random when true randomness is available from network timing, keyboard/mouse interrupts, cpu temperature sensors, etc. Do not ever use /dev/random directly from a high level language like a bash script. The existence of /dev/random is, if anything, a historical artifact to when /dev/urandom used weak barely-cryptographic ciphers and is far more often misused than used correctly. For example, piping from the script via </dev/urandom immediately reads 4096 bytes from urandom to fill the buffer, which should take microseconds.

Jack G
  • 101