what is linux inode

藏色散人
藏色散人Original
2023-04-20 10:18:212764browse

Linux inode is a characteristic description used to identify files in the operating system; the Linux system assigns an inode number to each file, and this number records some meta-information related to the file. Through this meta-information Can be used to uniquely identify a file.

what is linux inode

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

What is linux inode?

Before we understand the inode on the Linux operating system, let’s first talk about the files on the Linux operating system. For the Linux operating system, [everything is a file]. Files cannot exist independently of storage media (here refers to physical disks, memory, flash memory, etc.). Files on all operating systems deal with storage media all the time.

For example, when reading a file, the file needs to be loaded from the disk into the memory. When the file operation is completed, the file will be stored in the disk. Well, since the files are to be stored on the disk, and the disk has a capacity limit, it means that the number of physical files that can be stored on the disk is limited.

If you have understood this point of view, then congratulations, you generally know what inode does.

Yes, inode is a characteristic description of the file used to identify the operating system, and the inodes on the operating system are not endless. Usually, the number of inodes on the system has been determined after you install the operating system. down (however, you can dynamically modify the number of inodes).

You can query the number of inodes on the system through sysctl -a.

[root@server ~]# sysctl -a | grep inode
fs.inode-nr = 70212 21785

As above, 70212 in fs.inode-nr identifies the number of inodes allocated by the current operating system; 21785 represents the number of remaining free inodes in the previous operating system

As I just said, inode is A feature used to identify files. Why is this?

The Linux system assigns an inode number to each file. This number records some meta-information related to the file. This meta-information can be used to uniquely identify a file.

You can view the inode number of any file through ls -i

[root@server ~]# ls -i logrotate.man
8986710 logrotate.man

To view the meta information of the file, you need to use stat {filename}

[root@server ~]# stat logrotate.man
  文件:"logrotate.man"
  大小:18033      块:40         IO 块:4096   普通文件
设备:fd00h/64768d Inode:8986710     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-11-25 03:20:39.497330998 -0500
最近更改:2021-11-24 04:38:17.781399647 -0500
最近改动:2021-11-24 04:38:17.797398907 -0500
创建时间:-

As above, That is the inode information of a file. This contains:

  • Size: 18033: The number of bytes in the file. The disk space occupied by this file is 18033 Bytes

  • Block: 40: used block (data block). This file uses 40 physical blocks

  • Permissions: (0644/-rw-r--r--) Uid: (0/ root) Gid: (0/ root): File permissions and group information. This file is an ordinary file with permissions 0644. Both the user and group are root

  • Last access: 2021-11-25 03:20:39.497330998 -0500: that is, atime (the last time The time when the file was accessed)

  • Latest change: 2021-11-24 04:38:17.781399647 -0500: that is, mtime (the time when the file was last modified)

  • Latest changes: 2021-11-24 04:38:17.797398907 -0500: i.e. ctime (the time when the file (permissions, group) was last changed)

In fact, just passed stat logrotate.man also includes a field hard link: 1.

Why do hard links appear in inode information?

Generally, one file corresponds to one inode in the operating system, but this rule does not apply to hard link files. Because on the Linux operating system, multiple files are allowed to point to the same inode number.

In the hard link scenario, you can use different file names to access the contents of the same file, and modifications to the file content, attributes, etc. will be transferred to other files. However, deleting a linked file does not affect access to other files.

For example, create the hard link file of logrotate.man

[root@server ~]# ln logrotate.man logrotate.man.1
[root@server ~]# ls -i logrotate.man*
8986710 logrotate.man  8986710 logrotate.man.1
# 可以看到,建立了硬链接的文件共用了同一个 inode 编号
[root@server ~]# stat logrotate.man
  文件:"logrotate.man"
  大小:18033      块:40         IO 块:4096   普通文件
设备:fd00h/64768d Inode:8986710     硬链接:2
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-11-25 03:20:39.497330998 -0500
最近更改:2021-11-24 04:38:17.781399647 -0500
最近改动:2021-12-05 01:22:05.716611059 -0500
创建时间:-

After establishing the hard link, you can see through stat that the number of hard links has become 2.

At this time we delete the original link file and view the content of the linked file

[root@server ~]# rm -rf logrotate.man
[root@server ~]# tail -n 1 logrotate.man.1
Linux                                 Wed Nov 5 2002                                                                                                                 LOGROTATE(8)
[root@server ~]# stat logrotate.man.1
  文件:"logrotate.man.1"
  大小:18033      块:40         IO 块:4096   普通文件
设备:fd00h/64768d Inode:8986710     硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2021-12-05 01:25:50.701384256 -0500
最近更改:2021-11-24 04:38:17.781399647 -0500
最近改动:2021-12-05 01:25:41.544800473 -0500
创建时间:-

After deleting the original file of the hard link, the content of the linked file can be successfully read. At this time, the hard link The number of links becomes 1 again.

The reason for this situation is that the hard link actually adds an index to the file, and this index points to the inode number of the file. When the number of hard links is greater than 1, it means that the file has multiple hard links in addition to itself. When the number of hard links equals 0, the operating system no longer has any files pointing to the inode, that is, the operating system will recycle the inode.

In fact, every time a file is deleted, the number of hard links to the file is reduced by one. When the number of hard links in a file reaches 0, the file will be completely cleared by the operating system.

Finally, under normal circumstances, the number of inodes allocated by the operating system is completely sufficient, but some programs or human accidents may cause the inode overflow of the operating system. You can check the system through df -ih The usage of inodes under the partition can be used to take timely response measures.

[root@server ~]# df -ih
文件系统                Inode 已用(I) 可用(I) 已用(I)% 挂载点
devtmpfs                 121K     390    121K       1% /dev
tmpfs                    124K       1    124K       1% /dev/shm
tmpfs                    124K     522    124K       1% /run
tmpfs                    124K      16    124K       1% /sys/fs/cgroup
/dev/mapper/centos-root  4.0M    129K    3.9M       4% /
/dev/vda1                512K     332    512K       1% /boot
tmpfs                    124K       1    124K       1% /run/user/0

Recommended learning: "linux video tutorial"

The above is the detailed content of what is linux inode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn