In this article, you will learn what an inode is, how to check an inode and its size in Linux, and their roles in soft/hard links and system updates.
What is Inode in Linux?
In your Linux filesystem, an inode (or index node) is a table of records that stores metadata information for all files and directories except for the file name and data.
The inode stores the following information about a file or directory:
- The file’s type (e.g., regular file, directory, symbolic link, etc.)
- The file’s permissions (e.g., owner, group, and read/write/execute permissions)
- The file’s size
- The file’s timestamps (e.g., creation, modification, and access times)
- Pointers to the blocks on the disk where the file’s data is stored
Every file in the specified directory is an entry with the filename and inode number. The rest of the information about the referenced file is pulled from the inode table using the inode number.
The inode table is located at the beginning of a partition, and the inode number used to refer to file information in the inode table is unique at the partition level.
It means that two files with the same inode number cannot exist on the same partition unless they are on separate partitions.
Let’s see how to check the inode number of files and directories in a Linux system.
How to Check Inode in Linux?
You can easily list the inodes number assigned to files and directories in the current working directory by using the ls command:
$ ls -li
Whenever you create a new file, a unique integer number is generated in sequence and assigned to the file. The generated number is nothing more than a pointer pointing towards the file metadata in the inode table.
But keep in mind that the generated number should be less than the inode size that was set when the partition was created.
How to Find the Inode Size in Linux?
The inode number assigned to files and directories is limited to the inode size that was set when the partition was created. Once you reach the end, your system will be unable to create a new file, even if you have free space.
When you issued the “
ls -li” command, it displayed the file name, and the remaining file information, like user, group, file permissions, size, etc., is retrieved from the inode table using the inode number.
Run the following command to list the inode information for each filesystem on your Linux system:
$ df -hi
How Is the Size of an Inode Determined?
As you learned, the inode size is determined when the partition is created. For most users, the default inode size is more than enough. But as a Linux user, you should know how this inode size is determined at the time of partition creation.
The default setting used to determine the inode size on the filesystem is “1 inode = 2048 bytes (2 Kbytes)“, considering each file should be greater than 2 Kbytes.
Most of the files will be larger than 2 kilobytes; however, if your system has many soft links or you are running a mail server that stores a huge amount of tiny files, they can lower the average file size.
A normal user will not run out of inodes, but it is possible. If that happens, the user won’t be able to add a new file to the system; however, modern filesystems like Btrfs, JFS, and XFS use dynamic inodes to automatically fit inode sizes based on the requirements.
Role of the Inodes in Soft/Hard Link
Symlinks, or “soft and hard” links, are one of the most important subjects in Linux computing. Check out our detailed article about them.
First, let’s talk about the soft links.
Working of Inodes with Soft Link
In UNIX/Linux systems, whenever a soft link is created, a unique inode number is assigned to the generated soft link.
For example, there is an original “
file.txt” file located in my home directory, and inside “
~/Documents” I have a soft link to “
file.txt“, as shown.
Now, using the ls command, you can see that the inodes number for both of these files are different.
This result declares that each soft link occupies one inode number in your filesystem, and separate metadata information is stored in the inode table for every soft link you create.
Working of Inodes with Hard Link
I will use the same example as a soft link, except this time “
file.txt” in the “
~/Documents” directory is a hard link.
The following is the inode number of the original “
file.txt” and the hard link.
As you can clearly see, the original file and the hard link have the same inode number. Basically, whenever you create a hard link for any file, you only create a new filename that is pointing towards the same inode number.
Any changes made to a hard link, like metadata or file modification, will directly reflect on the original file.
Even if you delete the original file (basically, you are deleting the filename pointing towards the inode number), you will still be able to access the file data using the hard link. The data will stay until all of the names that can be linked to an inode number have been removed.
Role of the Inodes in System Update
When you update your Linux system, the running process uses the old library files with a different inode number, while the old version is replaced with the new version using a different inode number.
This way, the process kept running using the old library files while they updated to the new version using the separate inode number.
Why We Need Inodes is First Place?
The answer to this question is already present in this beloved article, but I understand that the inode numbers can be heavy for a beginner’s chest, so I will not leave it as something like an Easter egg.
So, to return to the original question, if you’re wondering why the inodes is so important and why we can’t simply attach the file metadata information to the file at the time of creation,
Then keep in mind that inodes are critical to allowing you to use the same file or data with multiple filenames and to allowing you to continue using your system even after a system update.
Many users become aware of inodes when they have enough disk space but are unable to create a new file. Although, inode number is not something that you directly interact with, it should be known to an informed Linux user.
I hope this article provides a clear picture of inodes in Linux; if anything is missing that should be included, then let us know in the comments section.