I'm learning about Linux and don't quite get why things like /dev/random appear as files. What uses them, and how do programs interact with them?
What are the files in /dev actually used for?
Answers
Most entries in /dev are device nodes, not ordinary files holding data on disk. They give programs a file-like way to interact with kernel interfaces, including hardware devices and virtual devices.
A program can open a device node and read from or write to it, although what those operations mean depends on the device. For example, reading from /dev/random requests random bytes from the kernel. Libraries and utilities can use these interfaces, though programs can also obtain random bytes through system calls such as getrandom.
Reply to this answer
Some are handy shortcuts in scripts and at the command line. Reading /dev/zero gives you zero bytes, while writing to /dev/null throws the data away. Reading /dev/urandom gives you random bytes. These aren't normal files that eventually run out of content.
Reply to this answer
Device nodes can also provide access to disks, so be careful experimenting with writes. Overwriting the wrong device can destroy data. Filling a drive with zeros or random bytes is not guaranteed to securely erase every kind of drive, especially an SSD.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions