- What is Inode number in Linux?
- What is inode number in Linux
- Why do we need inodes?
- Check Inodes on a Filesystem
- Find Inode number of File
- Inode Changes with Copy, Move, and Delete
- What to do in case you run out of Inode numbers?
- Wrapping up
- What is inode in Linux?
- What is Inode in Linux?
- How to Check Inode in Linux?
- How to Find the Inode Size in Linux?
- How Is the Size of an Inode Determined?
- Role of the Inodes in Soft/Hard Link
- Working of Inodes with Soft Link
- Working of Inodes with Hard Link
- Role of the Inodes in System Update
- Why We Need Inodes is First Place?
- Final Tips!
What is Inode number in Linux?
Data blocks and inodes are the two elements of a file system in Linux. Once the file is created, you cannot change the number of blocks. Inodes are allocated to files written in Linux filesystems. The filesystem’s database employs these unique Identification numbers to keep track of the files. They handle a file’s information and are critical components of Linux architecture. In this article, we will study inode numbers in detail.
What is inode number in Linux
In Linux, whenever a new file is created, it is given a file name and an inode number. This number works as the unique identifier for that file. As a user, you will use the file name to access the file but Linux will first map that filename with an Inode number in a database to access the file.
In basic words, an Inode number is just like an index number of a book. You may quickly locate the chapters you wish to read by browsing the index page. You won’t be able to discover the proper information without an index page, and you’ll have to go through the entire book to find a certain subject, which is a complete waste of time. The same applies to inodes in Linux. An inode is a data structure that points refer to the individual blocks, making the file. All of the administrative data required to read a file is included in the inode. The metadata for each file is kept in inodes in a table structure and this data includes:
- File types ( executable, block special etc )
- Permissions ( read, write etc )
- UID ( Owner )
- GID ( Group )
- FileSize
- Time stamps including last access, last modification and last inode number change.
- File deletion time
- Number of links ( soft/hard )
- Location of ile on harddisk.
- Some other metadata about file.
One thing to keep in mind is that except for the file name and the actual contents, they hold all of the information connected with a file. Inodes are located at the start of the partition. When you refer to a file by name, the system searches for the appropriate inode in the directory entry file where it resides. This provides your system with the necessary information and file data to run any operation.
Why do we need inodes?
Data is saved on your hard drive in fixed-size blocks. If you save a file that is larger than a normal block, your computer will look for the next available segment to save the remainder of the file. That may become difficult and confusing over time. And inodes can help you here.
While they do not store any of the file’s actual data, they do save its metadata, which includes all of the storage blocks where the file’s contents may be located. Filenames have no effect on inodes. That means you may duplicate a file, rename it, and it will still refer to the same original inode.
Check Inodes on a Filesystem
You can find a total number of inodes on disk by using ‘-i‘ option with df command.
Output:Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda1 1536000 138846 1397154 10% /
The above command shows the total number of Inodes on /dev/sda1 file system. Also provides the details about used and free inodes.
Each filesystem must-have free inodes available to create a new file. If the inodes are full, the system will not allow you to create a new file.
Find Inode number of File
You can find the inode number of any file using -i command line parameter with the ls command.
The first field in output is an inode number of the file.
Output:1150561 -rw-r--r-- 1 root root 0 Mar 10 01:06 myfile.txt
You can also search file with an inode number using find command. For example:
find /home/rahul -inum 1150561
Output:/home/rahul/myfile.txt
Inode Changes with Copy, Move, and Delete
What happens with the inode number when you copy, move or delete a file on the filesystem.
- Copy file: cp allocates a free inode number and create a new entry into the inode table.
### Check inode of existing file ls -il myfile.txt
1150561 -rw-r--r-- 1 root root 0 Mar 10 01:06 myfile.txt### Copy file with new name cp myfile.txt myfile_new.txt
### Check inode number of new file. Its changed ls -il myfile_new.txt
1150562 -rw-r--r-- 1 root root 0 Mar 10 01:09 myfile_new.txt
### Check inode of existing file ls -il myfile.txt
1150561 -rw-r--r-- 1 root root 0 Mar 10 01:06 myfile.txt### Moved file to another directory mv myfile.txt /opt/
### Check inode number of moved file. No change in inode ls -il /opt/myfile.txt
1150561 -rw-r--r-- 1 root root 0 Mar 10 01:06 /opt/myfile.txt
What to do in case you run out of Inode numbers?
Sometimes, your system may run out of inode numbers, and you may encounter difficulties like no space to write new data, frequent restart, data loss, application freeze, and not being able to log in to the system. In this case, it is recommended to delete the unused files.
Wrapping up
In this article, we learned about inode numbers in Linux and also performed different operations on inodes.
What is inode in Linux?
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:
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:
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.
Note : The inode size cannot be resized after the partition is created.
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.
Final Tips!
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.