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

825 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

all the /dev files are interfaces to the kernel, and represent different functions. The interface is designed that they with like files: you can read and write to them, set permissions…
/dev/null is a simple interface where anything written to it is simply discarded. /dev/zero is an read-only interface when read returns only zeros.
the/dev/random and /dev/urandom return random numbers when read.

Anonymous 0 Comments

/dev/null is end of file when read and discards data when written. It’s convenient to redirect input for programs that otherwise expect a keyboard, and discard unwanted output like directory errors from “grep <something * 2>/dev/null”.

/dev/random and /dev/urandom are the same driver, which collects entropy (randomness) from activity on the system. /dev/random will delay until it has collected enough random bits for the request, /dev/urandom will “make up” random numbers (using a random number generator) if necessary, so it won’t block. /random provides stronger numbers for things like cryptography, the /urandom is good enough for games and similar programs.

/dev/zero discards written data, and returns just zeros. I’ve used it to rapidly (and only mildly securely) overwrite files, and a source of data to create files of fixed size.

Anonymous 0 Comments

If your system has documentation installed, ‘man 4 zero’ and etc should give you much pleasure.

Anonymous 0 Comments

Null anything you write vanishes – used when you don’t want to record or look at the result of a command.
Zero produces binary zeroes – use it to erase files.
Random produces random binary numbers, attempting to produce high quality output, so reads can be slow.
Urandom produces fast random numbers.
Edit: use them by shell redirection, or with ‘dd’ and ‘cat’. dd count=512 if=/dev/zero of=/filefullofzeroes

Anonymous 0 Comments

Everything in /dev/ is not a real file on a disk, but a sort of pretend file — you can read from them and/or write to them, but their contents or effects live only in software, and are not written down on a disk.

Anonymous 0 Comments

Whenever you read from or write to a file, you don’t directly access the file. Instead, the operating system does that for you.

These three files are special in the sense that when you use them, the OS pretends they are real files when in fact they are “pretend” files.

When you try to write to /dev/null, the OS simply discards your input.

When you read from /dev/zero, the OS just gives you a bunch of zeroes.

And when you read from /dev/random, the OS just gives you a bunch of random numbers.

Anonymous 0 Comments

In general, files in /Dev are not real files, they are pointers to memory where one can interact with the hardware or virtual hardware simulated on software..

Null for example will return null/eof on read and write will just be ignored.

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.