Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How can I use /dev/(u)random on Ubuntu or any *nix sistems?

I tried this but it says permission denied.
Note : I also tried as root.

/dev/(u)random
sudo /dev/(u)random
sudo -s; /dev/(u)random
share|improve this question

2 Answers 2

up vote 7 down vote accepted

It's a file like device, so you can do things like cat it or copy from it. For instance:

dd if=/dev/urandom of=~/urandom_test count=4 bs=1024

Creates a file containing 4K of random bytes.

cat /dev/urandom > ~/urandom_test2 

Will continue to write random bytes to that file until you hit Ctrl-C. Don't do this on a low performing system...

head -30 /dev/urandom > ~/urandom_test3

Will write 30 lines of random bytes

share|improve this answer
    
Feel free to upvote then! –  aychedee Sep 23 '12 at 19:36
1  
I would if my rep > 15 –  Caner Korkmaz Sep 23 '12 at 19:38
    
Note : Don't do cat /dev/urandom > ~/urandom_test2 on low-performance systems -> that freezes the system –  Caner Korkmaz Sep 23 '12 at 19:44
    
Ah, true. I'll change my second example. –  aychedee Sep 23 '12 at 19:50
    
The 30 lines will be of random lenght too, I guess? (It's not really lines of random bytes) –  Volker Siegel May 10 at 4:36

Get random bytes

If you need a certain number of random bytes, read that number of bytes from /dev/urandom.
It is a "special file" that is made to be like a file to read random numbers from.

Using cat to read from /dev/urandom is a bad idea, because it will try to read /dev/urandom to the end - but it does not end.

You can use head. But take care to read by byte, not by line - because lines would be randomly separated by random newline bytes.

So, to read 30 random bytes into a file random.bytes, use:

head -c 30 /dev/urandom > random.bytes

You can read from it as a normal user.

Leave alone /dev/random

Normally, you want to use /dev/urandom, not /dev/random.

The problem is that /dev/random is hard to use in the right way - and easy to use in a wrong way. Using it wrong works at first, but creates strange - even random - performance problems later. Sometimes.

When you use /dev/urandom, it makes use of /dev/random internally, taking care of the tricky parts.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.