Solved really basic organization question.

I understand how everything is a file in FreeBSD and all the different directories like /usr/ and /home/ and /bin/ etc. Can someone explain to me what type of files are in each directory? Is /usr/ just for the users files and /bin is for executables? Knowing how everything is organized will really help me out. Is there a directory for all the hardware like the modem and usb devices?

thanks.
 
Shamelessly stolen from

https://www.reddit.com/r/BSD/comments/2szofc/eli5_why_is_separating_binaries_into_bin_sbin/

This answer may mostly be relevant to OpenBSD, since I'm not very familiar with other BSD-derived systems.)

I use the terms "dynamically linked" and "statically linked". A statically linked executable is independent: it does not load a separate C library, instead, the executable itself contains copies of code it uses from the C library, and interfaces with the kernel entirely by itself through syscalls. A dynamically linked executable loads an external library from a file and calls functions in it.

bin is for binaries which are useful for users without elevated privileges. /bin contains statically-linked binaries which are "fundamental to both single and multi-user environments" according to hier(7). They may be used in the tiny installer ramdisk. Most things in /usr/bin are dynamically linked and all of them are inaccessible in the installer.

sbin is for superuser binaries and daemons, i.e. things not useful to users without elevated privileges. Everything in /sbin is statically linked and accessible in the installer. Most things in /usr/sbin are dynamically linked and all of them are inaccessible in the installer.

Basically, the executables not in /usr are a basic set of statically-linked programs which are essential for installing, updating, and rescuing OpenBSD in the event of disaster.

OpenBSD encourages the use of multiple partitions for large parts of the filesystem for security reasons (among other things), and the statically-linked binaries can be used with only one of those partitions mounted, /, which may act as a failsafe in the event of other partitions such as /usr not being available. /usr is mounted with nodev, unlike / (see mount(8)).

sbin may serve a less technical purpose - organization. If you're not a superuser, you can safely ignore everything in here, because it doesn't concern you. You might not even have the necessary permissions to look at it in detail. binis less cluttered as a result, containing only things which are relevant to regular users.

/usr/local contains software installed from packages (which includes software installed from ports, since ports on OpenBSD uses the packaging system for installation after the stuff is fetched, patched, and built). It's on a separate partition. If you lose /usr/local or the disk that holds /usr/local, you still have a perfectly usable base system.

tl;dr: It helps with separation of concerns and system maintenance, since the less-essential binaries can go on separate partitions. When these fail, the system will be either restricted to software in base or single-user mode, but still usable. sbin, being only useful to superusers, may have different permissions to bin or safely ignored by unprivileged users. /usr may be mounted with nodev, unlike /.
 
The single most important distinction you have to understand to figure this out is the difference between root and normal (unprivileged) users. For the most part, unprivileged users are the ones that actually use the computer to get their work done (the computer and its OS serves them). The root user is needed to manage and maintain the computer; they don't use the computer to get their work done, but they work to keep the computer functioning. (There are exceptions to this rule: sometimes one has to be root to use the computer, for example when using low-level hardware.)

Is /usr/ just for the users files
What the hier(7) page doesn't explain: /usr is not actually for normal user's files. On the contrary, the files in there are not modified by normal users. Normal user files go into their home directories, which are typically in /usr/home or in /home (both traditions exist, as do a few others).

and /bin is for executables?
Yes, /bin does contain executables. But there are many other directories that also do, including: /sbin, /usr/bin and /usr/sbin, /libexec and /usr/libexec, and so on.

Is there a directory for all the hardware like the modem and usb devices?
At one level, a modem and a USB device is a device, and therefore will have a device entry in the /dev file system. Those device entries are files in a directory (as you said above, nearly everything in Unix is a file), but they are not regular files whose content is stored in the file system itself. Instead, they are special files (a category that also includes a few other things like named pipes), which simply tell the kernel that when a user accesses this file, to instead use the underlying hardware device. In modern Unixes (including FreeBSD), the entries in /dev don't have to be manually created and deleted by the system administrator (like we used to do 15 or 25 years ago), but automatically come into existence as the kernel discovers devices.

At another level, a USB storage device (the typical memory stick) is not just a device with a device entry (typically called /dev/da0s1), but also contains a file system. That file system can be mounted, either manually or by an auto-mounter, typically on the /media mount point. So another concept one has to understand is the distinction between a device, and the file system contained on the device.

And then finally there is one more concept, probably the fuzziest distinction of them all: The difference between software that is installed as part of the basic operating system distribution, which goes into a whole bunch of directories, with the bulk in /usr, but never in /usr/local, and other stuff:
  • Optional software packages that are installed after the base operating system, for example using the port and package system in FreeBSD for freely available software, and similar distribution mechanisms (such as pip for Python), which goes into /usr/local, to be clearly distinguished from the main operating system installation.
  • Locally developed software, which typically also goes into /usr/local
  • And in the System 5 tradition (from which Linux feeds), some commercial software gets put into /opt, which was initially intended for optional software. There are other common places for installing commercial software, such as /usr/lpp.
These categories are fuzzy and may sometimes seem not very logical: Why are vi and sh in /usr/bin, but emacs and bash in /usr/local/bin? There are logical answers to that question, but you need to understand history to know them. Let's not get bogged down in these details.

Where it can get really confusing is: If a Unix system is used by a single user to develop software, and the OS installation itself is considered disposable, the distinction between the user's /home directory, /usr/local and the OS itself may become fuzzy, as is the distinction between running as root and as an unprivileged user. But it is a very good idea to be really neat there, do as much work as possible as an unprivileged user with the data in /home, only perform actions as root is absolutely necessary (like to install software, or to test with real-world devices), and clearly distinguish between a development sandbox in /home and deployed software in /usr/local.
 
Thanks for all the info. I have a couple books on FreeBSD and unix and have tried to go through the handbook. Sometimes I have a hard time when things aren't specifically told how they work and interact with other things on a low level.
 
Your problem is that the man pages and books typically talk about the details (like exactly which options to use on the gpart(8) command). Better books also have an introductory section, which explains things like files, file systems, users, privileges, root, file permissions, devices, and such. Unfortunately, there is a giant gap between the superficial introduction to concepts, and the fine details. You're trying to straddle that gap. Good luck!
 
Your problem is that the man pages and books typically talk about the details (like exactly which options to use on the gpart(8) command). Better books also have an introductory section, which explains things like files, file systems, users, privileges, root, file permissions, devices, and such. Unfortunately, there is a giant gap between the superficial introduction to concepts, and the fine details. You're trying to straddle that gap. Good luck!

I just got Advanced programming with the unix environment and it seems to be a very detailed book so I think it will help a lot. The others I have like freebsd toolkit and absolute freebsd and one or two others don't seem to be as detailed.
 
Back
Top