[ELI5] /dev/null, /dev/random, /dev/urandom and /dev/zero

849 views

I’ve been reading about this files and also I have tried to inspect by myself about this files (file, ll, Stat and some other commands) but I just don’t understand them.

Can someone please explain me what are these files and how can I used them?

In: Technology

8 Answers

Anonymous 0 Comments

These are not regular files, like a file you save on disk. They’re virtual, which means there’s a piece of code in the operating system that makes them behave in a special way.

Basically, doing e.g. “echo Hello > file” causes your shell to open file named “file” and writing the bytes “Hello” into it. What happens if you replace “file” with “/dev/null”? Well, shell will do the same thing, but the kernel knows that /dev/null is a special device and the bytes the shell tells it to write will be simply discarded. This is also the idea of that special file: to make everything you write to it just disappear. Why is it useful? Well, there’re many programs that expect being able to write some data *into a file*. If you give them /dev/null instead, they’ll behave like usual, telling kernel to “write some bytes there” – which kernel will accept and then just drop.

Usual use case for /dev/null is redirecting output from a command to prevent it from cluttering your terminal with some useless messages.

Opposite to that are files like /dev/zero, /dev/random and /dev/urandom. They’re also special files, but their speciality makes them behave different when being *read from* (not *written to* like in case of /dev/null). /dev/zero, when being read from, just produces infinite sequence of “zero”-bytes, so bytes with 0 as their value. “infinite” here means “as long as you continue reading, you’ll always get zeros”. How is it useful? Again, some programs want some input, and feeding them with zeros is sometimes useful to make them run and not complain.

/dev/random and /dev/urandom are similar to /dev/zero, but instead of 0-bytes, they deliver random bytes. This is more useful – if you have a program, that somehow reacts on the input data, you can make it to behave randomly by feeding it with random bytes. Many programs use random bytes, e.g. in games they’re used to make the play unpredictable. Even more important is their usage in security-related domains, although it’s a big topic on its own.

Why two of them? Well, there’s something called “pseudorandomness quality” – since computer can’t directly generate random numbers (it’s a perfectly predictable machine), it tries to gather random noise from various sources (like devices it has on board) to make the generated bytes “more random”.

How it’s related to the files? Well, using /dev/urandom you’ll get infinite stream of bytes, but there’s no guarantee of them being of a good “pseudorandomness quality”. For some uses it’s not that important, e.g. games, but security-related stuff usually require highly random values. This is why /dev/random is useful – it’ll only deliver random bytes if system determines they’re of a good quality, but not necessarily as fast as you can read – so you may have to wait a bit for your random bytes.

So, all this files are only “virtual” – they aren’t store anywhere persistently, they just use “file metaphor” to implement some useful, special behaviors.

You are viewing 1 out of 8 answers, click here to view all answers.