Inodes in Linux: Key Concepts and How to Master Them

  • Inodes are data structures that store the metadata and location of files, but not their name or content.
  • The total number of inodes is defined when the file system is created and can be exhausted even if there is free disk space remaining.
  • Commands such as stat, ls -i, df -i and find allow you to inspect and control inode usage on Linux systems.
  • Regularly cleaning up small files, temporary files, and caches is key to avoiding serious problems caused by a lack of inodes.

Inodes in Linux

If you work with Linux servers, sooner or later you'll run into inodes, and it's best to do so before the system starts displaying the dreaded message. “No space left on device” despite having a free diskInodes are a key part of the file system and, although they may sound like pure theory at first, understanding them can save you from service outages, data loss, and major production scares.

In this article we'll take a close look at what an inode is, what it stores, how it's organized on disk, what happens when you run out of them, and, most importantly, How to control and optimize inode usage in Linux with practical commands. The idea is to go from the theoretical to the practical part, but with accessible language and without unnecessary embellishments.

What is an inode and what role does it play in Linux?

An inode (from English index node) is an internal file system data structure that stores all the metadata of a file or directory, except for its name and its contents. Every file and every directory in a Unix-like file system (Linux, macOS, etc.), and even in Windows environments with WSL2, has exactly one inode associated with it, identified by an integer known as the inode number.

Quick methods to identify file type in Windows and Linux
Related article:
Quick methods to identify file type in Windows and Linux

Within the same file system, each inode number is uniqueHowever, the same number can be repeated on other file systems mounted on the same machine. The true identity of a file is defined by combining the file system identifier and its inode number; this pair is what makes each element globally unique within the system.

It is important to emphasize that the filename is not part of the inodeFile names are stored in directory entries, which are simply lists that link a file name to an inode number. Therefore, in Linux, files are actually referenced by their inode, and the name is just a label that points to it.

What information does an inode store: detailed metadata?

The inode stores all the information necessary for the file system to know how to handle that file or directory, without needing to know its name. Among the common metadata stored by an inode in Linux systems are:

  • Type of file (regular, directory, symbolic link, device, etc.).
  • Access permits (reading, writing, execution for user, group and others).
  • Owner UID (user number of file owner).
  • GID of the group (number of the group to which it belongs).
  • File size in bytes.
  • Number of hard links that point to that inode.
  • Number of data blocks that the file occupies on the disk.
  • Storage device where it resides (Device ID) when applicable.
  • Time stamps: last read (atime), last content modification (mtime) and last metadata change (ctime); in some systems also the creation date.
  • Addresses of the data blocks where the file content is stored.
  • Indirect block addresses (redirection blocks) when the file is too large to reference using only direct pointers.
  • Version number or other internal fields depending on the file system.

In short, the inode concentrates Everything the system needs to know about a file, except its name and its contentsThe actual content lives in the disk's data blocks; the name, in the directory entries.

Block code, how inodes work internally

Inodes in Linux

Unix and Linux systems manage hard drives dividing them into fixed-size data blocksNot in clusters like FAT. When you save a file, its contents are spread across one or more free blocks of the file system. If the file is larger than a single block, it is fragmented into as many blocks as necessary.

The inode acts as a kind of "table of contents": It contains pointers to the physical disk blocks where the file resides.If the file fits in a few blocks, the inode points directly to them using direct addressing entries. When the file grows too large, indirect addressing tables come into play.

Direct and indirect routing tables

In file systems like ext4, each inode is usually reserved 15 addressing entries for its data blocks. The first 12 entries are direct pointers: each one points directly to a data block. This allows relatively small files to be managed very efficiently.

When the 12 direct inputs are no longer sufficient, the system resorts to the indirect routing:

  • Entry 13 is usually a simple indirect pointer: points to a block that does not contain file data, but a table with addresses of more data blocks.
  • Entry 14 is used as doubly indirect pointer: points to a block that in turn contains addresses of blocks that contain lists of data blocks.
  • Entrance 15 serves as triply indirect pointer: a three-level chain of tables before reaching the final blocks with the data.

This stepped mechanism allows a single inode to reference very large files without the internal structure becoming excessively large, at the cost of adding some complexity to access when the file is very large.

Linux file attributes
Related article:
What is MIME in Linux and how to use it in scripts and servers

Where are the inodes stored on the disk?

Inodes are not files that you can normally "see"; These are data structures that the file system reserves in specific areas of the diskIn ext2/ext3/ext4 systems, for example, when the file system is created, inode tables organized into groups are generated beforehand.

In ext4, the partition is internally divided into several groups of blocks. At the beginning of each group, a table of inodes is reserved. and other metadata. The inodes in that group typically reference blocks of data that are physically close, which reduces head movement on mechanical disks and improves performance.

Since each inode has a fixed size (for example, 256 bytes by default in ext4), and disk blocks are usually 4096 bytes, Each block can hold 16 inodesThe total number of inodes is defined when the file system is formatted and, in general, cannot be changed without recreating it.

How inodes are created, copied, and moved

When you create a new file, the file system assigns it a free inode and a unique inode number within that partition. If you copy a file within the same file system, the copy receives a different inode, with its own number, even if the data content is identical.

When moving a file within the same partition (for example with mv), the inode does not change: Only the directory entries that link the filename to that inode number are updated.That's why moving a large file within the same file system is so fast: no data is copied, only names are updated.

If you move a file between different file systems (for example, from / to another partition mounted in /mnt), the underlying process is actually a copy followed by a deletion. In that case, the inode number does change because the "new" file belongs to another file system with its own set of inodes.

Inodes and hard bonds: why they are so powerful

As we have discussed, in Linux Files are identified internally by their inode number, not by their nameThis allows several different names to point to the same inode and, therefore, to the same file content. This is called a hard link.hard link).

When you create a hard link with the command ln, you are adding a new directory entry that points to the same inode number as the original fileNo data is duplicated, and no extra space is used for content. As long as there is at least one name pointing to that inode, the data remains on disk.

This also explains why Deleting a file does not necessarily remove its data immediately.What is being removed is a name entry. Only when the inode's hard link counter reaches zero does the file system release the associated data blocks.

Total number of inodes and their limit

Each file system has a maximum number of inodes it can handle. Theoretically, for many systems, the limit is around 2^32 inodes, that is, about 4,3 billionIn practice, the number available is much smaller and depends on how the file system was created.

In ext2/ext3/ext4 systems, a heuristic rule is usually used: approximately one inode for every 16 KB of disk capacityThis means that if you create a file system of several hundred gigabytes with the default configuration, you will have millions of inodes, but not infinite ones.

The key fact is that The total number of inodes is set at the time of formatting. with tools like mkfsThere you can adjust parameters such as bytes per inode (option -iThis affects how many inodes will be created. Modifying it later involves recreating the file system or repartitioning, with the consequent risk of data loss if something goes wrong.

Why you can run out of inodes even if you have disk space

Running out of inodes means that There are no more structures available to represent new files or directoriesEven though the disk's data blocks still have plenty of space. It's a less common situation than running out of raw space, but perfectly possible.

Some typical scenarios that trigger the use of inodes are:

  • Generate huge amounts of very small files (for example, logs, individual emails on disk, temporary sessions).
  • Intensive use of containerizationwhere files and directories for layers, images, and containers proliferate.
  • Create file systems with very small blocks (for example, ext3 with reduced block size) that encourage the generation of many tiny files.
  • Caches, TMP, and session directories that accumulate old files for years if they are not cleaned up.

In these cases you may encounter the error “No space left on device"when creating new files, although when doing a df -h You still see gigabytes of free space. The problem isn't raw storage, but rather that The stock of available inodes has run out..

Symptoms and risks of running out of toilets

When a system reaches its limit with the anodes, a wide variety of failures begin to appear. Some of the More common problems when there are no free inodes are:

  • Inability to create new files or directorieseven with available disk space.
  • App crashes and freezes that attempt to write logs, temporary files, or persistent data.
  • Unexpected server restarts or operating system failures if critical processes cannot write.
  • Data loss or corruption when writes fail mid-operation.
  • Scheduled tasks (cron jobs) that do not run correctly because they cannot create temporary or output files.

In production environments, all of this translates into a clear risk: If you don't monitor the inodes, you can shut down entire services due to "lack" of space when the disk is still half empty..

Checking the number of inodes and their usage in Linux

Linux offers several commands for inspect specific inodes and monitor overall inode usage in the systemLet's look at the most useful ones.

What is the Linux superblock and how does it work?
Related article:
The superblock in Linux: what it is, what it stores, and how to repair it

View complete inode information with stat

The command stat muestra all the metadata that the system knows about a file or directory, including its inode number, permits, owner, and timestamps.

For example:

root@equipo:~# stat /var/log/lastlog
File: /var/log/lastlog
Size: 292292 Blocks: 96 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 17381397 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 0/ root) Gid: ( 22/ utmp)
Access: 2022-01-12 11:28:19.900058928 +0100
Modify: 2022-01-12 11:28:19.900058928 +0100
Change: 2022-01-12 11:28:19.900058928 +0100
Birth: 2021-06-25 17:40:57.254208200 +0200

At this exit you can easily identify the inode number (Inode field), the number of links, the size, and relevant dates.

View the inode number with ls -i

If you're only interested in the inode number of a file or directory without too many details, you can use ls with the option -i. This command List the files showing their inode in the first column.

Example using a file:

root@equipo:~# ls -i /var/log/lastlog
17381397 /var/log/lastlog

And for a directory, adding some more options to view the entry itself:

root@equipo:~# ls -idl /var/log
16813380 drwxr-xr-x. 18 root root 4096 Jun 6 12:33 /var/log

Check inode usage at the file system level with df -i

The command df It is used to view space usage in file systems, and with the option -i It goes on to show Information on total, used and free inodes.

For example:

root@equipo:~# df -i /dev/sda1
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 524288 379 523909 1% /boot

This way you can see the total number of inodes, how many are in use, how many are free, and the percentage usedIt's a quick way to check if you're approaching the limit.

Count how many inodes (files) are in a directory

If you want to get an idea of ​​how many files and directories are under a specific path, you can combine find with wc -l, in as much as each file or directory consumes one inode:

root@equipo:~# find /var/log | wc -l
120

This number tells you approximately How many inodes are being used in that directory tree?It is very useful for locating areas of the system that generate thousands of small files.

View inode characteristics of a partition

To inspect the inode configuration on an ext2/ext3/ext4 partition you can use tune2fsThe following command shows the total number of inodes, how many there are per group, and the size of each inode:

root@equipo:~# tune2fs -l /dev/sda1 | grep Inode
Inode count: 2031616
Inodes per group: 8192
Inode blocks per group: 512
Inode size: 256

With this information you can estimate How many inodes do you have available and how many fit per block?For example, with 256-byte inodes and 4096-byte blocks, we can accommodate 16 inodes per block.

Differences between file systems: ext, XFS, Btrfs and others

Inodes in Linux

Not all file systems manage inodes in the same way. In the ext2/ext3/ext4 family, as we have seen, Inode tables are created statically when the file system is initializedInodes occupy a fixed, reserved space that cannot be used for anything else, and their number cannot grow dynamically.

In more advanced systems such as XFS or BtrfsThe approach is different. Instead of pre-booking a large table, Inodes are created on demand. when they are needed to represent new files. This allows for greater flexibility, although the internal management logic is more complex.

It is also necessary to distinguish between Disk Inodes (the inodes stored on the physical disk) and the In-Core Inodes (the memory structures that the kernel uses to work with them while the files are open). The concept is the same, but the support and some implementation details change.

Practical case: using df -i and reading its output

When you run df -i You get a table with several columns. Understanding what each one means helps to interpret the health status of your inodes:

  • File system: the mounted device or volume (for example, /dev/sda1 o /dev/disk1s2 in macOS).
  • Inodes / iused / ifree: total inodes, used inodes and free inodes.
  • %iused: percentage of inodes in use.
  • mounted on: file system mount point.

Although the total numbers may seem enormous, They can run out long before the disc is fullFor example, on a laptop with 1 TB and about 4.900.000 free inodes, you could create 4.900.000 1-byte files, using only a few MB of storage, and yet stop being able to create new files because there are no more inodes left.

How to detect and solve high inode problems

When applications start crashing or scheduled tasks fail to run, it's a good idea to suspect the inodes. The first thing to do is check usage with df -i in the relevant file systems:

df -i
df -i /ruta/que/te/preocupa

If you see that the percentage of inodes used is close to 100%, you've located the bottleneck. The next step is identify where small files are concentrated that are consuming so many inodes.

Some useful strategies are:

  • Locate directories with large number of files using combinations of find, du y wc -l.
  • Sort files by size with something like ls -laShr in a specific path to identify many tiny files.
  • Check cache, temporary, and download directorieswhich are often forgotten: for example /tmp, web application caches, session stores, etc.
  • If an application (such as a mail server) stores many individual items as files, concentrate its output in specific directories so that it can be cleaned periodically.

Once the source of the problem has been located, the most direct solution is usually delete files that are no longer needed:

  • Delete obsolete directories and files.
  • Delete old cache files that can regenerate on their own.
  • Clear email archives, sessions, or temporary files older than a certain time (for example, 14 days) using a cron job with find -mtime y -delete.

This allows for the rapid recovery of inodes without affecting the file system structure. However, caution is advised with aggressive automation because Deleting essential files can break applicationsIt's always a good idea to review and test commands before deploying them in production.

If you still run short of inodes and can't release any more, the only option left is the hard way: repartition or recreate the file system with a more generous inode ratio (adjusting bytes per inode in mkfsIt is a delicate process that involves backups and possible data loss if something goes wrong.

When is high toilet usage truly concerning?

Seeing a high percentage of inodes in use doesn't automatically mean disaster. Sometimes it simply reflects that You have a lot of files, but still enough spaceThe real problem arises when you approach 100%, especially in critical partitions such as /, /var o /homewhere files are constantly being created and deleted.

In many cases, excessive use stems from superfluous data of small sizeTemporary files, caches, or session files that haven't been cleaned in months. Implementing automated routines (cron jobs) that periodically delete files based on age is often a simple way to Keep inode consumption under control without altering the system design.

Linux file attributes
Related article:
Bottles on Linux: a practical guide to running Windows applications

It's also worth considering the differences between file systems. Some, like XFS or Btrfs, manage inodes more flexibly than ext4, which can be useful in environments where frequent inode handling is known. millions of small files.

Understanding how inodes work, what information they store, and how they are depleted allows you to see the file system in a new light: They are no longer simply “files and folders”, but internal structures with very specific boundaries.

By controlling these limits and occasionally checking inode usage with the appropriate commands, it is much easier to avoid "No space left on device" errors and maintain a healthy Linux system, even under heavy load. Share the information and more people will know about the topic..