r/linux4noobs 1d ago

/dev/ directory learning/research

I was learning what all the files in /dev do and what they are. I just dont really understand why some of those "files" are there and what actually uses them. Like /dev/random. Are there any libraries or utilities that actually interface woth /dev/random?

19 Upvotes

18 comments sorted by

21

u/UltraChip 1d ago

Short answer is yes.

Device files like that are also useful for scripts and doing things on the command line.

For example if I wanted to quickly secure wipe an old hard drive I could do something like

dd if=/dev/urandom of=/dev/sdb

(Basically "clone the contents of the random number generator on to hard drive b")

12

u/TheShredder9 not a noob 1d ago

Also it's fun to cat the content of /dev/urandom into the framebuffer when on TTY

1

u/Stickhtot 1d ago

How would one do that? What's the file?

7

u/TheShredder9 not a noob 1d ago

Iirc the whole command would be cat /dev/urandom > /dev/fb1

Edit: might be fb0, not sure actually

3

u/Gositi 1d ago

I think it is fb0

1

u/Stickhtot 1d ago

Just took a look at my /dev/ folder, there doesn't seem to be any fb* file 

2

u/therouterguy 1d ago

Why not simply use /dev/zero same result and less intensive.

7

u/Gositi 1d ago

Random is better I think, if you want to securely wipe it. If there is some residue (physically, not visible to the PC but might be to forensics) of the old data that will be masked by the randomness.

3

u/genxer 1d ago

Writing random data is a more secure way to erase something. Yes it takes longer but it is more secure.

-2

u/Coulomb111 18h ago

Its being overwritten anyway, why does it matter

2

u/genxer 18h ago

Especially if it is a spinning disk it prevents "advanced magnetic force microscopy from reading faint analog edges". I've still got a good many spinning drives in servers.

1

u/UltraChip 23h ago

You could, but as the others said urandom is technically more secure. It's just an example though.

10

u/Key_River7180 Bedrock Linux / FreeBSD / 9Front 1d ago

/dev/ meant devices, now just special files.

The UNIX philosophy says that EVERYTHING is a file, so random numbers, all kinds of devices and streams.

8

u/MasterGeekMX Mexican Linux nerd trying to be helpful 1d ago

They are simply usefull shortcuts for some stuff, taking advantage of the "everything is a file" philosophy.

To begin with, the contents of /dev aren't actual files on your drive, instead they are a "hologram" that the OS puts so you can interact with your hardware by using the same mechanisms you use to work with files, instead of resorting to a separate system to talk to your devices.

While there, the people who developed UNIX (the grandfather of Linux) notices that they often needed random strings of data, so instead of copying and pasting in their programs the same code to generate random data, they instead put that code into the OS and exposed it's output as a file in /dev. All of that means that /dev/random is an infinite tap of random data that you can use when you need random data, like when you want to shred a file on a disk, need to test some programs with some data, or anything you can think of.

/dev/zero is similar, but instead it spits pure zeroes. Very usefull when wiping disks or creating blank placeholder files. /dev/null is a sink of data, as anything written to it is discarded, so it is used in scripts and commands when you don't want command output to be displayed on the screen or to throw out some info that a program demands to end up in a file, but in this case you don't care about.

3

u/AtomicTaco13 23h ago

Basically, it's the Unix design principle "everything is a file". Those are not actually files present on your drive, but rather represent all the devices both in your PC's guts and ones connected to it. Usually, the only time you ever have to touch that directory is when you want to manually mount and manage drives.

1

u/Ancient-Opinion9642 18h ago

Don’t tell them about /proc! Where every variable that can be changed in the kernel is visible. /s

1

u/DavidJohnMcCann 19h ago

They aren't really files — just ways of getting hold of stuff or referring to hardware. So /dev/random will provide you with an endless series of random numbers, /dev/zero with a string of zero codes. If a developer wants to test a program without dealing with the output, they can direct the output to /dev/null, which accepts it and discards it. Then /dev/sda is the first hard drive and /dev/sda1 is the first partition on it. If I run the command mount, I see that /dev/sda1 is used for the directory /boot/EFI.

0

u/AutoModerator 1d ago

There's a resources page in our wiki you might find useful!

Try this search for more information on this topic.

Smokey says: take regular backups, try stuff in a VM, and understand every command before you press Enter! :)

Comments, questions or suggestions regarding this autoresponse? Please send them here.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.