硬链接与软连接

inode

在我们开始介绍软连接与硬链接时,我们先来介绍一下inode。

索引节点,其英文为 Inode,是 Index Node 的缩写。索引节点是整个 Linux 文件系统的基础。存储于文件系统上的任何文件都可以用索引节点来表示。举一个例子来说,假设有一个老图书馆里面有一本登记簿,上面记录着馆内的书名及存放位置,比如在哪一间的第几排存放着哪一本书,以及书的作者是谁。在这里,记录着一本书的那一行就是索引节点。索引节点以同样的方式来存储对象。
在 Linux 系统中,文件系统主要分为两部分,一部分为元数据(metadata),另一部分为数据本身。元数据,换句话说,就是“包含了与数据有关信息的数据”。索引节点就管理着文件系统中元数据的部分。

关于inode更详细介绍:

LINUX/UNIX文件系统索引节点浅析

什么是软连接和硬链接呢?

硬链接:硬连接指通过索引节点来进行连接。在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号(Inode Index)。在Linux中,多个文件名指向同一索引节点是存在的。一般这种连接就是硬连接。硬连接的作用是允许一个文件拥有多个有效路径名,这样用户就可以建立硬连接到重要文件,以防止“误删”的功能。其原因如上所述,因为对应该目录的索引节点有一个以上的连接。只删除一个连接并不影响索引节点本身和其它的连接,只有当最后一个连接被删除后,文件的数据块及目录的连接才会被释放。也就是说,文件真正删除的条件是与之相关的所有硬连接文件均被删除。

软连接:软连接也称之为符号连接(Symbolic Link)。软链接文件有类似于Windows的快捷方式。它实际上是一个特殊的文件。在符号连接中,文件实际上是一个文本文件,其中包含的有另一文件的位置信息。

硬链接访问原理

我们知道文件都有文件名与数据,这在 Linux 上被分成两个部分:用户数据 (user data) 与元数据 (metadata)。用户数据,即文件数据块 (data block),数据块是记录文件真实内容的地方;而元数据则是文件的附加属性,如文件大小、创建时间、所有者等信息。在 Linux 中,元数据中的 inode 号(inode 是文件元数据的一部分但其并不包含文件名,inode 号即索引节点号)才是文件的唯一标识而非文件名。文件名仅是为了方便人们的记忆和使用,系统或程序通过 inode 号寻找正确的文件数据块。下图展示了程序通过文件名获取文件内容的过程。
这里写图片描述

软连接访问原理
软链接与硬链接不同,若文件用户数据块中存放的内容是另一文件的路径名的指向,则该文件就是软连接。软链接就是一个普通文件,只是数据块内容有点特殊。软链接有着自己的 inode 号以及用户数据块,如下图。

实例:

1.创建test.txt文本并为其创建硬链接,尝试跨分区创建硬链接,尝试对目录创建硬链接。

[root@centos7 testdir]# touch test.txt      <==创建文件test.txt
[root@centos7 testdir]# ll test.txt         <==长格式查看test.txt,链接数为1.(连接数在权限.后面,在所属主前面)
-rw-r--r--. 1 root root 0 Jul 29 15:39 test.txt 
[root@centos7 testdir]# ln test.txt /test2.txt  <==为test.txt创建硬链接到/test2.txt
[root@centos7 testdir]# ll test.txt             <==长格式查看test.txt,连接数已变为2
-rw-r--r--. 2 root root 0 Jul 29 15:39 test.txt
[root@centos7 testdir]# ll /test2.txt           <==长格式查看/test2.txt,连接数也是2
-rw-r--r--. 2 root root 0 Jul 29 15:39 /test2.txt
[root@centos7 testdir]# ls -i test.txt          <==查看test.txt的inode
68447508 test.txt
[root@centos7 testdir]# ls -i /test2.txt        <==查看/test2.txt的inode,发现和test.txt的inode是一样的
68447508 /test2.txt
[root@centos7 testdir]# echo test > test.txt    <==向test.txt文本中输入内容
[root@centos7 testdir]# cat test.txt            <==查看test.txt内容
test
[root@centos7 testdir]# cat /test2.txt          <==查看test2.txt内容,发现内容是相同的
test
[root@centos7 testdir]# rm -f test.txt          <==删除源文件
[root@centos7 testdir]# ll /test2.txt           <==查看目标文件,连接数变为1
-rw-r--r--. 1 root root 5 Jul 29 15:53 /test2.txt
[root@centos7 testdir]# cat /test2.txt            <==查看test2.txt,没有问题
test
[root@centos7 testdir]# mkdir test              <==创建目录test
[root@centos7 testdir]# ll -d test              <==长格式查看test目录
drwxr-xr-x. 2 root root 6 Jul 29 15:54 test
[root@centos7 testdir]# ln test /test2          <==为test目录创建硬链接,无法创建
ln: ‘test’: hard link not allowed for directory
[root@centos7 testdir]# ln test.txt /app/       <==跨分区创建硬链接(/app是一个独立的分区),无法创建
ln: failed to create hard link ‘/app/test.txt’ => ‘test.txt’: Invalid cross-device link
结论:
通过以上实验我们得出以下结论:
1.硬链接无法对目录创建
2.硬链接无法跨分区创建
3.硬链接的inode是相同的
4.创建硬链接连接数会增加
5.删除创建文件的文件后,文件仍能访问

2.创建test.txt文本并为其创建软链接,尝试跨分区创建软链接,尝试对目录创建软链接。

[root@centos7 testdir]# ll                      <==长格式显示目录内容,目录内有test.txt文本和test目录
total 0
drwxr-xr-x. 2 root root 6 Jul 29 16:27 test
-rw-r--r--. 1 root root 0 Jul 29 16:27 test.txt
[root@centos7 testdir]# ln -s /testdir/test.txt /test2.txt  <==为test.txt创建软连接到/test2.txt
[root@centos7 testdir]# ll test.txt                         <==长格式查看test.txt,发现链接数并没有增加
-rw-r--r--. 1 root root 0 Jul 29 16:27 test.txt
[root@centos7 testdir]# ll /test2.txt                       <==长格式查看test2.txt,发现此文件指向了/testdir/test.txt
lrwxrwxrwx. 1 root root 17 Jul 29 16:31 /test2.txt -> /testdir/test.txt
[root@centos7 testdir]# cat test.txt                        <==查看test.txt内容
test
[root@centos7 testdir]# cat /test2.txt                      <==查看test2.txt内容,没有问题
test
[root@centos7 testdir]# ls -i test.txt                      <==查看test.txt的inode
70621972 test.txt
[root@centos7 testdir]# ls -i /test2.txt                    <==查看/test2.txt的inode,发现inode并不是相同的
1632840 /test2.txt
[root@centos7 testdir]# rm -rf test.txt                     <==删除源文件test.txt
[root@centos7 testdir]# ll /test2.txt                       <==查看/test2.txt,发现有变化(因为文本原因,所以可能显示不出来)
lrwxrwxrwx. 1 root root 17 Jul 29 16:31 /test2.txt -> /testdir/test.txt
[root@centos7 testdir]# cat /test2.txt                      <==查看/test2.txt文本内容,提示不存在。链接文件是存在的,此提示是找不到源文件test.txt
cat: /test2.txt: No such file or directory
[root@centos7 testdir]# ln -s /testdir/test/ /test2         <==对/testdir/test目录创建软连接/test2
[root@centos7 testdir]# ll -d test/                         <==长格式查看/test目录
drwxr-xr-x. 2 root root 6 Jul 29 16:27 test/                
[root@centos7 testdir]# ll -d /test2                        <==长格式查看/test2目录
lrwxrwxrwx. 1 root root 14 Jul 29 16:52 /test2 -> /testdir/test/
[root@centos7 testdir]# ln -s /testdir/test/ /app/test3     <==对/testdir/test目录创建软连接/app/test3目录下(/app是一个独立的分区)
[root@centos7 testdir]# ll test/ -d                         <==长格式查看/test目录
drwxr-xr-x. 2 root root 6 Jul 29 16:27 test/                
[root@centos7 testdir]# ll /app/test3/ -d                   <==长格式查看/app/test3目录
drwxr-xr-x. 2 root root 6 Jul 29 16:27 /app/test3/
结论:
通过以上实验我们得出以下结论:
1.软连接可以对目录创建
2.软连接可以跨分区创建
3.软链接的inode是不同的
4.创建软链接链接数不会增加
5.删除创建软连接的文件后,文件不能访问
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值