Neural Nets

Linux Filesystem Architecture

/ 9 min read

Download As PDF

The root directory (/) forms the apex of the Linux filesystem, a hierarchical tree structure where all other directories and files reside. This structure is vital for organizing and managing the system, and understanding it is crucial for system administration, software development, and general Linux proficiency.

Linux Filesystem Hierarchy
📁 Root Directory (/)
🔐 Security Context
🔗 Important Links

📁 The Root Directory (/)

The root directory is the ultimate parent of all other directories and files. It’s denoted by a single forward slash (/). Navigating from the root allows access to every part of the system.

Core Directories and Their Functions

The Filesystem Hierarchy Standard (FHS) defines the layout of the Linux filesystem. While distributions might have minor variations, the core structure remains consistent. Here’s a detailed look at key directories:

🗂️ /bin - Essential User Binaries

/bin houses essential command-line utilities accessible to all users, regardless of their privileges. These commands are fundamental for basic system operation and are crucial during the boot process and for single-user mode.

  • Examples:
    • bash: The Bourne Again Shell, a common default command interpreter. It provides a powerful interface for interacting with the system.
    • ls: Lists directory contents, a fundamental tool for navigation.
    • cp: Copies files and directories.
    • mv: Moves or renames files and directories.
    • rm: Removes files and directories. Caution is advised!
    • cat: Displays file contents.
    • echo: Prints text to the terminal.
    • grep: Searches for text patterns within files.
    • ps: Displays information about running processes.
    • kill: Sends signals to processes, often used to terminate them.
    • ln: Creates hard and symbolic links.

🛠️ /boot - Boot Loader and Kernel Files

/boot contains the files necessary for booting the operating system. This includes the kernel image, bootloader configuration, and initial RAM disk (initrd).

  • Key Files:
    • vmlinuz: The compressed Linux kernel image. This is the core of the operating system.
    • initrd.img (or initramfs.img): The initial RAM disk, a temporary filesystem containing essential drivers and modules needed to load the full root filesystem.
    • grub/ (or other bootloader directory): Contains configuration files for the GRUB bootloader, responsible for loading the kernel and initiating the boot process. grub.cfg is the primary configuration file.
    • config-*: Kernel configuration files for different kernel versions. These files detail how the kernel was compiled.
    • System.map: A symbol table that maps kernel symbols to their memory addresses, useful for debugging.

🖥️ /dev - Device Files

/dev holds special files representing devices. These are not regular files but rather interfaces to hardware components. The kernel uses these files to interact with devices.

  • Device Types:
    • Character devices: Provide unbuffered, direct access to the device. Examples include terminals (/dev/tty), serial ports (/dev/ttyS0), and random number generators (/dev/random).
    • Block devices: Provide buffered access to devices, allowing for reading and writing data in blocks. Hard drives (/dev/sda), SSDs, and partitions are represented as block devices.
  • Examples:
    • /dev/sda: Typically the first SATA hard drive.
    • /dev/sda1: The first partition on the first SATA hard drive.
    • /dev/null: A special device that discards all data written to it and returns an end-of-file (EOF) when read.
    • /dev/tty: The controlling terminal for the current process.
    • /dev/random: A source of random numbers generated from environmental noise.

⚙️ /etc - System-Wide Configuration Files

/etc stores system-wide configuration files for various applications and services. These files control the behavior of the system and individual programs.

  • Key Areas:
    • System Configuration: Files like /etc/hostname, /etc/resolv.conf (DNS settings), /etc/fstab (filesystem table), and /etc/passwd (user accounts) are crucial for system operation.
    • Network Configuration: The /etc/network/ directory (or distribution-specific locations) holds network interface configuration files.
    • Service Configuration: Configuration files for services like Apache (/etc/apache2/), SSH (/etc/ssh/), and systemd (/etc/systemd/) reside here.
  • Importance: Modifying files in /etc can significantly impact system behavior. Changes should be made with care and understanding.

🏠 /home - User Home Directories

/home contains the personal directories of each user on the system. Each user has their own subdirectory within /home, providing a private space for storing files, configuring applications, and running personal processes.

  • User Isolation: /home provides a level of isolation between users, preventing them from interfering with each other’s files and settings.
  • Structure: Within each user’s home directory, you’ll often find subdirectories like Documents, Downloads, Pictures, Music, Videos, and .config (for user-specific application configurations).
  • Hidden Files: Files and directories starting with a dot (.) are hidden by default. These often contain configuration files and settings for various applications.

📚 /lib - Shared Libraries

/lib houses shared libraries essential for the execution of programs. These libraries contain reusable code that multiple programs can utilize, saving disk space and improving efficiency.

  • Dynamic Linking: Shared libraries are dynamically linked to programs at runtime, meaning the library code isn’t embedded directly into the program’s executable file.
  • Versioning: Libraries are often versioned to maintain compatibility between different software releases.
  • Subdirectories: /lib often contains subdirectories for different architectures (e.g., /lib/x86_64-linux-gnu).

💾 /media - Removable Media Mount Point

/media is the standard location for automatically mounting removable media such as USB drives, CD-ROMs, and external hard drives. When you insert a USB drive, it will typically be mounted under /media/<username>/<device_name>. This makes it easy to access the contents of the removable device.

📂 /mnt - Temporary Mount Point

/mnt is intended as a temporary mount point for manually mounting filesystems. While it can be used for any filesystem, it’s often used for temporary mounts like network shares or other devices that aren’t automatically mounted under /media.

🧩 /opt - Optional Software

/opt is designed for installing optional or third-party software packages that are not part of the standard system distribution. Each package typically resides in its own subdirectory under /opt (e.g., /opt/google/chrome). This keeps optional software separate from the core system files.

🔍 /proc - Process Information (Virtual Filesystem)

/proc is a virtual filesystem that provides a window into the kernel and running processes. It doesn’t contain real files but rather dynamic information generated by the kernel.

  • Process Information: Each running process has a directory under /proc (e.g., /proc/1234), containing information about its memory usage, open files, environment variables, and more.
  • System Information: /proc also contains files with system-wide information like CPU details (/proc/cpuinfo), memory statistics (/proc/meminfo), uptime (/proc/uptime), and loaded modules (/proc/modules).

👑 /root - Root User’s Home Directory

/root is the home directory of the root user (uid 0). It is separate from /home to protect the root user’s files and configurations from accidental modification by other users.

⚡ /run - Runtime Data

/run is a tmpfs (temporary filesystem in RAM) that holds runtime data for processes and services. Data in /run is volatile and is lost when the system reboots. It’s typically used for things like process IDs (PIDs), lock files, and sockets.

🛠️ /sbin - System Binaries

/sbin contains system administration binaries, essential for system management and maintenance. These commands are typically used by the root user or system administrators.

  • Examples:
    • shutdown: shuts down the system.
    • reboot: reboots the system.
    • fdisk: manages disk partitions.
    • mkfs: creates filesystems.
    • ifconfig: configures network interfaces (largely deprecated in favor of ip).
    • iptables: configures the firewall.

🌐 /srv - Service Data

/srv is intended for storing data served by the system, such as website content for web servers, data for FTP servers, or repositories for version control systems. This separation helps organize data based on the services that use it.

📊 /sys - System Information (Virtual Filesystem)

/sys is a virtual filesystem that provides a detailed view of the kernel’s view of the system’s hardware and drivers. It is more structured and comprehensive than /proc for hardware information.

🗑️ /tmp - Temporary Files

/tmp is a directory for storing temporary files created by programs and users. Files in /tmp are often deleted on reboot or by system cleanup processes.

🛒 /usr - User Programs and Libraries (Multi-Purpose)

/usr is a large and important directory containing a wide range of user-facing software, libraries, documentation, and shared resources. It is often a separate partition for easier management and upgrades.

  • Key Subdirectories:
    • /usr/bin: Contains user binaries, similar to /bin but typically for less essential commands.
    • /usr/lib: Contains libraries used by user programs.
    • /usr/local: A dedicated location for locally compiled or installed software, allowing for separation from distribution-provided software. This keeps system upgrades cleaner.
    • /usr/share: Contains architecture-independent data like documentation, icons, and locale information.
    • /usr/include: Contains header files for C/C++ development.

🗄️ /var - Variable Data

/var holds files whose size is expected to change over time, such as log files, databases, spool directories, and cache files. It is often a separate partition to prevent it from filling up the root partition.

  • Key Subdirectories:
    • /var/log: Contains system and application log files, crucial for troubleshooting and monitoring.
    • /var/mail: Stores user mailboxes.
    • /var/spool: Contains spool files for services like printing and email.
    • /var/cache: Holds application cache data.
    • /var/lib: Contains dynamic data used by applications and services, like databases and package management data.

Security Context and File Permissions

Linux implements a robust security model based on file permissions and ownership.

🔐 File Permissions

Every file and directory has associated permissions that determine who can access it and in what way. Permissions are defined for three categories: owner, group, and others.

  • Read (r): Allows viewing the contents of a file or listing the contents of a directory.
  • Write (w): Allows modifying a file or creating/deleting files within a directory.
  • Execute (x): Allows running a file as a program or accessing files within a directory.

🛡️ Special Permissions

  • SUID (Set User ID - s): When set on an executable file, it causes the file to run with the permissions of the file’s owner, regardless of who executes it. This is often used for programs that need elevated privileges temporarily.
  • SGID (Set Group ID - s): Similar to SUID, but the file runs with the group permissions of the file. This is useful for sharing resources within a group.
  • Sticky Bit (t): When set on a directory, it prevents users from deleting or renaming files within that directory unless they own the file, own the directory, or are the root user. This is often used for shared directories like /tmp.

File Ownership

Every file and directory has an owner and a group associated with it. The chown command is used to change ownership, while chgrp changes group ownership.

Symbolic links (or soft links) act as pointers to other files or directories. They provide a way to create shortcuts or aliases without duplicating the actual file content.

  • Creating Symbolic Links: Use the ln -s <target> <link_name> command.

Conclusion

A thorough grasp of the Linux filesystem hierarchy is essential for effectively administering, navigating, and utilizing the power of a Linux system. This comprehensive guide provides a solid foundation for understanding the structure, purpose, and security aspects of the Linux filesystem, empowering users to confidently manage their systems and troubleshoot issues.