Linux Filesystem

Posted by Lukas Hajdu on Wed, Oct 2, 2019

To maintain Linux filesystem it’s important to know where the files are located. For this reason, Linux provides tools to help with this. One of these tools is standardised filesystem.

Linux consists of one big filesystem tree and draws heavily on Unix. Inconsistency between various Linux distributions created a need for introducing standards which would unify structure. Unified filesystem makes it easier for program developers and system administrator to work with the system.

The first of these standards was Filesystem Standard (FSSTND) but because of its limitation it was replaced with the new Filesystem Hierarchy Standard (FHS)

The FHS is recommended structure but it’s not always followed by all distributions (e.g. experimenting). Additional directories in root folder do not violate FHS but it does violate to have components in directories other than in standard.

The FHS makes an important distinction between shareable files and unshareable files and attempts to fit each important directory into one cell.

FSH directory distinction

Root (/)

The root partition must contain files and utilities to boot the system, restore the system from backup and recover/repair system.

All other directories branch from the root directory. It’s often on specially dedicated partition and no application or package should create subdirectories in the folder according to FHS. Critical directories like /etc and /sbin must reside on the same partition.

/boot

The directory contains files needed to boot a system. These files are static and unshareable. The essential files are:

  • vmlinoz - compressed kernel
  • initramfs - initial RAM filesystem (mounted before root filesystem)

Higher-level startup and configuration files reside in /etc. The /boot directory can contain multiple kernel versions. The choice between these kernels is made by GRUB at boot time.

Other files besides the essential ones are:

  • config - kernel compilation configuration file
  • System.map - kernel symbol table (useful for debugging)

/boot directory

/bin

The directory contains critical executable programs needed by sysadmins or users, such as ls, cp and mount. The /bin directory contains static files and is rarely shared.

The programs are available before the filesystem is mounted and can be used indirectly by other scripts. Nonessential binaries are placed in /usr/bin.

/dev

The directory contains special device files (device nodes; pseudo-files), which are essential for the system to function properly. Need for this location is because Linux treats most hardware devices as if they were files.

It contains a large number of files that function as hardware interfaces.

/dev directory

/etc

The directory contains unshareable and static system-wide configuration files and some startup scripts. There should not be executable binaries in this directory.

Linux distributions often add configuration files here. For example, some systemd configuration files (if the distribution uses it) are stored in /etc/systemd. It also contains skeleton for creating home directories in /etc/skel and System V initialisation scripts in /etc/init.d.

/home

The directory contains users’ data, and it’s shareable and variable. User directories are conventionally placed under /home, as in /home/lukas, etc.

All personal configurations and executables are placed in this directory hierarchy. The /home directory often resides on its partition and it can be usually anywhere on a corporate network (NFS server), and then mounted automatically upon use.

The root user makes an exception in the structure as it is always found under /root directory.

/lib and /lib64

The directory contains program libraries necessary to execute binaries in /bin and /sbin. Libraries consist of code that’s shared across many programs and stored in separate files to save disk space.

These libraries are important for booting the system and executing commands in the root filesystem. The /lib/modules subdirectory contains kernel modules (drivers).

Systems supporting both 32-bit and 64-bit binaries must keep both kinds of libraries on the system. That’s why there are separate directories for 32-bit (/lib) and 64-bit (/lib64) libraries on some systems.

/media

The directory is used as a mounting point for removable media such as CDs, USB drives, etc. It’s an optional part of the FHS.

Modern distributions use /media subdirectories to mount the removable filesystems dynamically upon insertion. The udev automatically creates subdirectories with names defined in udev configurations and once the media is removed, the created subdirectory disappears.

/mnt

The directory is used to temporarily mount a filesystem when needed by sysadmin. Network filesystems (NFS, Samba, CIFS, etc.) are a common use case.

Older distributions used the /mnt directory to mount removable media similar to the /media directory.

/opt

The directory is intended for ready-made software packages that don’t ship with the system (unbundled packages) and that wish to keep their files in one isolated place, rather than scatter over the system in other directories. It can contain commercial word processors, games, etc.

This makes easier to install and uninstall software and determine the nature of each file within the package.

/proc

The directory is a mount point for a pseudo-filesystem. This special directory holds all the details about the Linux system, including its kernel, processes, and configuration parameters. It’s created dynamically by Linux.

The entries in /proc are usually zero-length files (when viewed) neither binary nor text but they can contain a large amount of information.

/proc directory

/sys

The directory is a mount point for the sysfs pseudo-filesystem. The /sys directory is not empty only if the system is running. It’s used to gather information about the system.

/root

This directory is a root user home directory. The root account’s home directory may be determined by the developer or local preference, but this is the recommended default location.

/sbin

The directory contains additional binaries to those in the /bin directory. These binaries are essential for booting, restoring, recovering and repairing system and must be able to mount other filesystems.

/sbin directory

/srv

The directory contains site-specific data which is served by this system for protocols such as ftp, rsync, www, etc.

The /srv directory should always exist on FHS compliant systems and should be used as the default location for such data (but it’s not used too much).

/tmp

The directory is used to store temporary files and can be accessed by any user or application.

Some distributions remove the contents of /tmp by every reboot (Ubuntu), some run periodic jobs (RHEL 6) and some use it as a temporary mounting point for a virtual filesystem which is removed upon system reboot (Fedora).

/usr

The directory is the second major section of the filesystem, is shareable and contains read-only data. It’s used for files which are not needed for system booting.

1/usr/
2├── bin         # Most user commands; non-essential binaries and scripts
3├── games       # Games and education binaries (optional)
4├── include     # Header files used to compile application; used by C programs
5├── lib         # Libraries
6├── local       # Local hierarchy; third level hierarchy
7├── sbin        # Non-vital system binaries;
8├── share       # Read-only architecture-independent data
9└── src         # Source code for Linux kernel (optional)

/var

The directory contains variable data files which can change during system operations. This includes spool directories and files, administrative and logging data, and transient and temporary files.

It’s a good idea to mount /var as a separate filesystem. If the directory gets filled up (e.g. logging data), it should not lock up the system. If the directory can’t be on a separate partition, it’s preferable to have it on the /usr partition rather than the /root partition.

 1/var
 2├── backups
 3├── cache               # Application cache data
 4├── crash               # System crash dump (optional)
 5├── lib                 # Variable state information
 6├── local               # Variable data for /usr/local
 7├── lock -> /run/lock   # Lock files
 8├── log                 # Log files and directories
 9├── mail                # User mailboxes (optional)
10├── opt                 # Variable data for /opt
11├── run -> /run         # Data relevant to running processes
12├── spool               # Application spool data
13├── tmp                 # Temporary files  
14└── www                 # Root for website hierarchies (optional)

/run

The directory contains run-time variable data. The purpose is to store files that contain runtime information. The directory is proposed by FHS, but it’s not formally accepted by distributions.

/run directory



comments powered by Disqus