linux 软硬链接区别

210 阅读4分钟

linux 软硬链接区别

linux软连接与window中快捷相似,可以快速通过快捷方式去查看源文件与修改;

而硬连接更多是为了文件安全,防止其他人误删除文件,即使删除源文件也可以通过硬连接去恢复;

下面通过实际对比分析二者的实质差异

linux创建源源文件test

[root@centos-master link]# touch test
[root@centos-master link]# vi test 
[root@centos-master link]# ll
total 4
-rw-r--r-- 1 root root 11 Jan  3 11:42 test
[root@centos-master link]# cat test 
hello link
[root@centos-master link]# 

创建软连接与硬连接

#创建软连接 ln -s 
[root@centos-master link]# ln -s test stest
#创建硬连接
[root@centos-master link]# ln test htest
[root@centos-master link]# ll
total 8
-rw-r--r-- 2 root root 11 Jan  3 11:42 htest
lrwxrwxrwx 1 root root  4 Jan  3 11:44 stest -> test
-rw-r--r-- 2 root root 11 Jan  3 11:42 test
[root@centos-master link]# 

通过stat命令查看文件属性

[root@centos-master link]# ls
htest  stest  test
[root@centos-master link]# stat test
  File: ‘test’
  Size: 11              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 1184277     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-01-03 11:42:27.180108474 +0800
Modify: 2022-01-03 11:42:13.310600110 +0800
Change: 2022-01-03 11:44:25.000427083 +0800
 Birth: -
[root@centos-master link]# stat stest
  File: ‘stest’ -> ‘test’
  Size: 4               Blocks: 0          IO Block: 4096   symbolic link
Device: fd01h/64769d    Inode: 1184274     Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-01-03 11:44:26.477481225 +0800
Modify: 2022-01-03 11:44:15.924094391 +0800
Change: 2022-01-03 11:44:15.924094391 +0800
 Birth: -
[root@centos-master link]# stat htest
  File: ‘htest’
  Size: 11              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 1184277     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-01-03 11:42:27.180108474 +0800
Modify: 2022-01-03 11:42:13.310600110 +0800
Change: 2022-01-03 11:44:25.000427083 +0800
 Birth: -
[root@centos-master link]# 

其中 Inode属性是指在linux系统中的唯一标识,每个文件对应唯一一个标识,通过文件名找到对应的Inode,这样同时可以保证在不同的文件夹下可以创建相同的文件名。

可以观察到软连接文件stest对应的Inode与原始文件的不同,但是硬连接创建的与原始文件相同

linux-link.png

通过上图可以发现硬连接与原始文件指向同一个node,而软连接是新创建的node,软连接文件stest 通过test查找对应的node,而不是通过node直接连接,网上有资料介绍说软连接是通过node直接关联(图中的红线),后续会验证不是通过node直接关联

检查修改test内容,其他文件内容也对应修改,同理修改stest或者htest也

[root@centos-master link]# vi test 
[root@centos-master link]# cat test
hello link
hello link
[root@centos-master link]# cat stest
hello link
hello link
[root@centos-master link]# cat htest
hello link
hello link
[root@centos-master link]# 

删除test文件对硬连接htest 和 软连接stest有什么影响?

linux-link2.png 可以看出软连接文件stest无法查找原始文件的node,因为test文件删除,删除test文件是否会删除Inode: 1184277?答案是不会

stat查看test文件属性中Links

[root@centos-master link]# stat test
  File: ‘test’
  Size: 22              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 1184277     Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-01-03 12:11:02.985015660 +0800
Modify: 2022-01-03 12:10:55.696747919 +0800
Change: 2022-01-03 12:10:55.697747955 +0800
 Birth: -
[root@centos-master link]# 

发现Links:2 有两个连接在使用,那么删除test时对应的node不会删除,所以对硬连接htest不会有影响,但是软连接无法使用

检查文件

[root@centos-master link]# ll
total 8
-rw-r--r-- 2 root root 22 Jan  3 12:10 htest
lrwxrwxrwx 1 root root  4 Jan  3 11:44 stest -> test
-rw-r--r-- 2 root root 22 Jan  3 12:10 test
[root@centos-master link]# rm -rf test
[root@centos-master link]# ll
total 4
-rw-r--r-- 1 root root 22 Jan  3 12:10 htest
lrwxrwxrwx 1 root root  4 Jan  3 11:44 stest -> test   #stest 显示为红色
[root@centos-master link]# cat htest
hello link
hello link
[root@centos-master link]# cat stest
cat: stest: No such file or directory
[root@centos-master link]# 

删除test后,软连接stest已经无法访问,也可以说明软连接不是通过node直接关联原始文件node。