Linux Filesystem¶
Overview¶
The Linux filesystem is the organized structure that manages how data is stored, retrieved, and organized on storage devices. Understanding the filesystem means understanding where Linux keeps everything — and why.
Filesystem Hierarchy Standard (FHS)¶
Inodes¶
Every file in a Linux filesystem is represented by an inode (index node) — a data structure that stores all metadata about a file except its name.
graph TD
A["Directory Entry<br/>(name → inode number)"] -->|"inode number"| B["Inode<br/>- permissions<br/>- owner/group<br/>- timestamps<br/>- size<br/>- block pointers"]
B --> C1["Data Block 1"]
B --> C2["Data Block 2"]
B --> C3["Data Block N..."]
style B fill:#0077b6,color:#fff
style A fill:#1a2236,color:#e6edf3 What's in an Inode¶
| Field | Description |
|---|---|
mode | File type + permissions (rwxrwxrwx) |
uid / gid | Owner user and group IDs |
nlinks | Hard link count |
size | File size in bytes |
atime | Last access time |
mtime | Last modification time |
ctime | Last status change time |
blocks | Number of 512-byte blocks allocated |
block[15] | Pointers to data blocks (direct + indirect) |
What is NOT in an inode?
The filename! Filenames live in directory entries (dentries), which map names to inode numbers. This is why hard links work — multiple names can point to the same inode.
Hard Links vs Soft Links¶
- Both point to the same inode
- Deleting one does not delete data (data removed when link count reaches 0)
- Cannot span filesystems
- Cannot link directories (usually)
- Verified:
ls -lishows same inode number
VFS — Virtual Filesystem¶
The VFS (Virtual File System) is a kernel abstraction layer that provides a unified interface to different filesystem types.
graph TD
App["Application<br/>open(), read(), write()"] --> VFS["VFS Layer<br/>(kernel)"]
VFS --> ext4["ext4"]
VFS --> xfs["XFS"]
VFS --> btrfs["btrfs"]
VFS --> proc["/proc<br/>(procfs)"]
VFS --> sys["/sys<br/>(sysfs)"]
VFS --> nfs["NFS<br/>(network)"]
style VFS fill:#0077b6,color:#fff
style App fill:#06d6a0,color:#0d1117 Permissions¶
Understanding Permission Bits¶
| Characters | Meaning |
|---|---|
- | File type: regular file |
rw- | Owner: read + write, no execute |
r-- | Group (shadow): read only |
--- | Others: no permissions |
Special Permissions¶
| Bit | Octal | Name | Effect |
|---|---|---|---|
s on owner execute | 4000 | SUID | Executes as file owner |
s on group execute | 2000 | SGID | Executes as file group |
t on others execute | 1000 | Sticky | Only owner can delete (used on /tmp) |
# Check SUID files (potential security concern)
find / -perm -4000 -type f 2>/dev/null
# /tmp has sticky bit
ls -ld /tmp
# drwxrwxrwt 20 root root 4096 Jan 15 10:00 /tmp
Interview Questions¶
What is the difference between a hard link and a symbolic link?
A hard link creates another directory entry pointing to the same inode. Both names are equal — there is no "original." A symbolic link creates a new inode containing a path string. If the target moves, the symlink breaks. Hard links cannot cross filesystems; symlinks can.
Can you create a hard link to a directory?
Generally no, for regular users and most filesystems. The kernel prevents it to avoid circular references in the directory tree (which would break fsck, find, and path resolution). Exceptions: . and .. are hard links to directories created by the kernel.
What happens when you delete a file?
The kernel decrements the hard link count of the inode. When the count reaches 0 and no process has the file open, the data blocks are freed and the inode is marked available. If a process still has the file open, data remains until the last file descriptor is closed (used for log rotation: logrotate renames/deletes, but old daemon keeps writing to the old inode until HUP).