An inode is a core concept in the Unix/Linux file system, used to store metadata about files within the file system. Understanding the concept and usage of inodes is very helpful for better managing the file system, solving file system-related issues, and performing system maintenance and troubleshooting.
What is an inode?
An inode (index node) is a data structure in the Unix/Linux file system that stores file metadata. The metadata of a file includes information such as the file’s creator, creation date, size, and more.
As we know, files are stored on hard drives, and the smallest storage unit on a hard drive is called a “sector”. Each sector stores 512 bytes. To improve efficiency, the operating system reads multiple contiguous sectors at once, and these contiguous sectors form a “block”. This block, composed of multiple sectors, is the smallest unit for file access. Typically, a block size is 4KB, meaning eight contiguous sectors make up one block. File data is stored in blocks, while the metadata of the file resides in the inode. Each file has a corresponding inode that contains relevant information about that file, which can be thought of as the file’s “identity card”.
The complete structure of an inode
• File type (file type)
• Permissions (permissions)
• Hard link count (link count)
• Owner UID
• Group GID
• File size (size in bytes)
• Timestamps:
– atime (access time, the last time the file or directory was accessed.)
– mtime (modification time, the last time the file content or directory was modified.)
– ctime (change time, the last time the file or directory attributes were changed, such as executing chmod, chown commands.)
• Block pointers (block pointers)
• Block count (block count)
• File system specific information
We can use the stat command to view the inode information of a file.
# Use the stat command to view complete information
$ stat /etc/passwd
File: /etc/passwd
Size: 2847 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 131076 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2025-07-21 10:15:32.123456789 +0800
Modify: 2025-07-20 14:30:15.987654321 +0800
Change: 2025-07-20 14:30:15.987654321 +0800
Birth: 2025-07-15 09:00:00.000000000 +0800
Inode number
The inode number is a unique identifier for each inode in the file system. If we think of an inode as the file’s identity card, then the inode number can be understood as the “identity card number” of the file within the file system.
You can directly view the file index node number using the command ls -i.
# View the inode number of a file
$ ls -i test.txt
131076 test.txt
# ↑
# This is the inode number, 131076
You can also view the index node number in the inode information obtained by stat.
# Use the stat command to view the inode number of a file
$ stat /etc/passwd | grep Inode
Device: 801h/2049d Inode: 131076 Links: 1
We can also use the command ls -il to view more detailed information.
# Get detailed information using the ls -li command
$ ls -li /etc/passwd
131076 -rw-r--r-- 1 root root 2847 Jul 21 10:30 /etc/passwd
When we access a file, it may seem that the user opens the file by its name, but in fact, the system internally divides this process into three steps:
1. The system finds the inode number corresponding to the file name;
2. Using the inode number, it retrieves the inode information;
3. Based on the inode information, it locates the block where the file data is stored and reads the data. The system also checks the user’s access permissions based on the inode information; if the user has permission, it points to the corresponding data block; if not, it returns a permission denied error.
Size of an inode
Inodes also consume hard disk space, so when formatting, the operating system automatically divides the hard disk into two areas: one for data storage and another for inode storage. The size of each inode must be a power of 2, typically 128 bytes or 256 bytes. Modern systems recommend using a 256-byte inode size, which balances functionality and efficiency.
The total number of inodes is determined when the file system is created. After the file system is created, the inode size cannot be modified; to change it, the file system must be recreated. Therefore, each file system has a fixed number of inodes, and when the inodes run out, new files cannot be created even if there is still space on the disk. Additionally, it is important to note that the inode size cannot exceed the block size.
Check disk inode usage
$ df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 655360 45231 610129 7% /
Commands to check the inode size of the existing file system
# Method 1: Use tune2fs
$ tune2fs -l /dev/sda1 | grep "Inode size"
Inode size: 256
# Method 2: Use dumpe2fs
$ dumpe2fs -h /dev/sda1 2>/dev/null | grep "Inode size"
Inode size: 256
# Method 3: Use fsck
$ fsck.ext4 -n /dev/sda1 | head -5
Commands to create a file system with a specified inode size
Create a file system with 128-byte inodes
$ mkfs.ext4 -I 128 /dev/sdb1
Create a file system with 512-byte inodes
$ mkfs.ext4 -I 512 /dev/sdb1
Simultaneously specify the number and size of inodes
$ mkfs.ext4 -I 256 -N 500000 /dev/sdb1
View detailed information about the inodes of a specific file system
$ tune2fs -l /dev/sda1 | grep -i inode
Inode count: 655360
Free inodes: 610129
Inodes per group: 8192
Inode size: 256
We can also use debugfs to view detailed information about a specific inode. If we already know that a file’s inode number is “131076”, we can use the following command to view the specific information of this inode.
$ sudo debugfs -R "stat <131076>" /dev/sda1
Inode: 131076 Type: regular Mode: 0644 Flags: 0x80000
Generation: 3456789 Version: 0x00000000:00000001
User: 1000 Group: 1000 Size: 1024
File ACL: 0 Directory ACL: 0
Links: 1 Blockcount: 8
Fragment: Address: 0 Number: 0 Size: 0
ctime: 0x60e7a123:12345678 -- Sun Jul 21 10:30:00 2025
atime: 0x60e7a456:87654321 -- Sun Jul 21 11:00:00 2025
mtime: 0x60e7a789:11111111 -- Sun Jul 21 10:45:00 2025
crtime: 0x60e7a000:00000000 -- Sun Jul 21 10:00:00 2025
Size of extra inode fields: 32
EXTENTS:(0):524288
Hard Links and Soft Links Explained
Hard Link
$ echo "original content" > original.txt
$ ln original.txt hardlink.txt
$ ls -li original.txt hardlink.txt
131076 -rw-r--r-- 2 user user 17 Jul 21 10:35 original.txt
131076 -rw-r--r-- 2 user user 17 Jul 21 10:35 hardlink.txt
In the above commands, we first created a source file “original.txt”, then created a hard link to it, and then checked the information of the source file and the hard link. We can see that both the source file and the hard link have the same inode number, pointing to the same inode. The inode information includes a field called “link count”, which records the total number of file names pointing to that inode. After creating a hard link for a file, the link count increases by 1.
When a file is deleted, the “link count” in the inode node decreases by 1. When this value reaches 0, it indicates that no file names point to this inode, and the system will reclaim this inode number and the corresponding block area.
Characteristics of Hard Links
-
They share the same inode number as the source file, meaning all linked files and the source file point to the same inode.
-
The link count in the inode reflects the total number of the source file and linked files.
-
The source file and linked files are completely equivalent.
-
Modifying either file will update the other.
-
Hard links have a limitation: they cannot span file systems and cannot create hard links to a directory.
Soft Links (Symbolic Links)
$ ln -s /path/to/original.txt symlink.txt
$ ls -li original.txt symlink.txt
131076 -rw-r--r-- 1 user user 17 Jul 21 10:35 original.txt
131077 lrwxrwxrwx 1 user user 20 Jul 21 10:36 symlink.txt -> /path/to/original.txt
After executing the above commands, we created a symbolic link to the source file “original.txt”. However, the source file and the symbolic link have different inode numbers, which is different from hard links. When a user accesses the symbolic link file, the system automatically redirects the access to the source file. Therefore, regardless of which file is opened, the data read will ultimately come from the source file. This means that the symbolic link file depends on the existence of the source file; if the source file is deleted, opening the link file will result in an error: “No such file or directory”. Deleting the symbolic link does not affect the source file. This is the biggest difference between soft links (symbolic links) and hard links: the soft link file points to the file name of the source file, not the inode number of the source file, and the inode “link count” does not change when the soft link file is deleted or a new soft link file is added.
Characteristics of Soft Links
-
Soft links have their own inode, which is different from the source file’s inode.
-
Soft links can be created across file systems.
-
Soft links can also be created for directories.
-
When the target file is deleted, the soft link becomes invalid.
Directories and Inodes
In Unix/Linux systems, a directory is actually a special type of file, containing the file names of the files it includes, along with the corresponding inode numbers. Opening a directory is essentially opening the directory file.
When creating a directory, two directory entries are generated by default: “.” and “..”. The inode number of the former is the same as the inode number of the current directory, equivalent to a “hard link” to the current directory; the inode number of the latter is the inode number of the parent directory, equivalent to a “hard link” to the parent directory. Therefore, the total number of “hard links” for any directory is always equal to 2 plus the total number of its subdirectories (including hidden directories).
Effects of Directory Operations on Inodes
Creating a Directory
$ mkdir testdir
$ ls -lid testdir
48433326 drwxr-xr-x 2 user user 4096 Jul 21 10:40 testdir/
Hard link count of the directory
$ ls -ld testdir
drwxr-xr-x 2 user user 4096 Jul 21 10:40 testdir/# ^# Link count is 2
Creating a subdirectory within the directory
$ mkdir testdir/subdir
$ ls -ld testdir
drwxr-xr-x 3 user user 4096 Jul 21 10:41 testdir/# ^# Link count changes to 3: "current directory", "parent directory", "newly created subdirectory"
Viewing the inode number of a directory
ls -ia
48433326 . 246285 .. 48440302 subdir
# Each directory has a different inode number
This concludes the discussion on related concepts of inodes. Thank you for reading this far. If this sharing has been helpful to you in any way, feel free to like, share, and follow. In the next article, I will continue to share typical applications of inodes, so stay tuned.javascript:;
