硬链接定义
-
硬链接:当系统要读取一个文件时,会先读inode信息,然后再根据inode中的信息到块区域将数据取出来。而硬链接是直接再建立一个inode链接到文件放置的块区域。即建立硬链接时该文件内容没有变化,只是增加了一个指向这个文件的inode,并不会额外占用磁盘空间。
-
硬链接不支持对目录做链接,只支持对文件做链接
硬链接和软连接对比
- 硬链接
[root@localhost ~]# ln 1.txt 1_heard.txt
- 软链接
[root@localhost ~]# ln -s 1.txt 1_sorft.txt
- 对比:会发现软链接很小,硬链接很大
- ls -i 查看inode号
- 会发现硬链接文件和源文件使用了同一个inode号,大小相同,可使用find命令查找到硬链接
[root@localhost ~]# ls -l
总用量 32
drwxr-xr-x. 3 root root 45 10月 26 16:11 111
-rw-r--r--. 1 root root 0 10月 26 15:54 111.12.txt
-rw-r--r--. 2 root root 391 10月 30 15:10 1_heard.txt
lrwxrwxrwx. 1 root root 5 10月 30 15:06 1_sorft.txt -> 1.txt
[root@localhost ~]# ls -i
33584735 111 33575033 1_sorft.txt 33575034 2.txt 33574987 anaconda-ks.cfg
33584737 111.12.txt 33575035 1.txt 33584734 3.txt 33575006 anaconda-ks.cfg.1
33575035 1_heard.txt 33584733 1.txt~ 33584736 3.txt~ 33575036 windows.txt
硬链接文件特性
- 就是创建的文件和另外一个文件是相同的inode号,这两个文件相互为硬链接文件。(软链接文件是有原有的目标,它有一个真正的文件,软链接仅仅为一个快捷方式)
- 再删除源文件后,会发现==软链接==一直闪烁,表示找不到源文件了,那这个软链接文件就没有用了
- 再删除源文件后,会发现==硬链接==,会发现正常使用,但inode会变成1
[root@localhost ~]# rm 1.txt
rm:是否删除普通文件 "1.txt"?y
[root@localhost ~]# ls -l
总用量 28
drwxr-xr-x. 3 root root 45 10月 26 16:11 111
-rw-r--r--. 1 root root 0 10月 26 15:54 111.12.txt
-rw-r--r--. 1 root root 391 10月 30 15:10 1_heard.txt
lrwxrwxrwx. 1 root root 5 10月 30 15:06 1_sorft.txt -> 1.txt
- 真正存数据的地方,存这个文件信息的位置在inode上
- inode号代表着一个inode,inode是存在于文件系统中特殊的东西,这个inode会去记录一些文件的属性,如文件的时间、权限、位置等
[root@localhost ~]# ls -i 1_heard.txt
33575035 1_heard.txt
- 硬链接不会占用双份空间,因为使用的同一个inode
硬链接的限制
- 硬链接的限制:
- 目录不能做硬链接
[root@localhost ~]# ln 111 hf11 在硬链接指向目录的时候,会提示错误 ln: "111": 不允许将硬链接指向目录
- 不能跨文件系统(跨分区),因为不同的文件系统有不同的inode,不同的table目录体系。
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 18G 1.1G 17G 6% /
devtmpfs 483M 0 483M 0% /dev
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 493M 6.7M 486M 2% /run
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/sda1 197M 109M 88M 56% /boot
tmpfs 99M 0 99M 0% /run/user/0
[root@localhost ~]# ls -i /boot/
71 config-3.10.0-514.el7.x86_64
67 grub
131136 grub2
76 initramfs-0-rescue-513f8b3950084e768a99df3a6cd3d9e6.img
75 initramfs-3.10.0-514.el7.x86_64.img
189445 initramfs-3.10.0-514.el7.x86_64kdump.img
74 initrd-plymouth.img
72 symvers-3.10.0-514.el7.x86_64.gz
70 System.map-3.10.0-514.el7.x86_64
77 vmlinuz-0-rescue-513f8b3950084e768a99df3a6cd3d9e6
73 vmlinuz-3.10.0-514.el7.x86_64
[root@localhost ~]# ln /boot/config-3.10.0-514.el7.x86_64 /tmp/conifg.1
ln: 无法创建硬链接"/tmp/conifg.1" => "/boot/config-3.10.0-514.el7.x86_64": 无效的跨设备连接
硬链接总结
- 硬链接文件不能跨分区创建,因为每个分区都有各自的inode。
- 比如、:d1分区有a文件的inode号是88,而d2分区的b文件的inode号也是88.
- 正是因为各个分区的inode号各自为营,如果跨分区创建硬链接文件就会导致混乱。
- 为了避免这个问题,所以不允许跨分区创建硬链接文件。
- 硬链接可以删除,因为使用了相同的inode