1. 程式人生 > >硬鏈接與軟連接

硬鏈接與軟連接

硬鏈接與軟連接

硬鏈接與軟連接

寫在前面



Linux系統中存在兩種鏈接文件,硬鏈接(hard link)和符號鏈接(symbolic link)。符號鏈接也稱為軟連接。想了解清楚這兩種鏈接文件的區別並不容易,首先要清楚Linux文件系統的相關知識。

我們知道文件有文件名和數據。而Linux的文件系統在存儲文件時分為兩個部分,用戶數據(userdata)和元數據(metadata)。用戶數據是文件的真實數據存儲文件系統的data block中,元數據存儲在一個iNode的節點塊中,包括文件的iNode號,權限,大小,時間屬性(atime,ctime,mtime),所屬者/組等屬性信息(但並不包括文件名)。

iNode


對於文件系統來講,iNodeindex node)才是文件的唯一標識,並非文件名,文件名只是符合人類記憶習慣而實現的一種方式。這樣就可用下圖來說明計算機是如何通過文件名來訪問的文件的了。

技術分享

Linux操作系統中,可以使用stat或者ls -i命令查看文件的iNode,使用df i 命令查看整個文件系統的iNode節點使用情況。筆者說過,iNode號相對於文件系統來具有唯一性,對於不使用raid或者lvm技術的傳統文件系統的來說就是在“分區”內iNode是文件訪問的唯一標識。

為了解決文件共享的問題(筆者自認為),Linux在文件系統設計上支持鏈接文件。分為硬鏈接和符號鏈接。

硬鏈接


iNode號具有多個文件名的時候,稱這種文件為硬鏈接文件,硬鏈接文件和源文件在系統下具有相同的“地位”。為了更直觀的理解,我引用了鳥哥私房菜書中的插圖。

技術分享

由於硬鏈接的元數據與源文件的完全一致(就是同一條元數據),所以有了它的一系列特性。

1,不能跨文件系統;

2,有相同的iNodedata block

3,硬鏈接只能對已經存在的文件進行創建;

4,不能針對目錄文件進行創建,Linux文件系統在設計時對目錄默認創建了兩個硬鏈接, . .. 如果再支持目錄創建硬鏈接的話會生成目錄環可能會造成死鎖(筆者一家之言);

5,刪除文件時,必須刪除所有的硬鏈接才能真正釋放data block.

軟連接


與硬鏈接不同,軟連接是一個獨立的文件,與源文件的元數據和用戶數據都不同。軟連接的的數據塊中存儲的只是指向源文件的路徑。當然這裏也這裏也引用鳥哥圖來讓讀者直觀的理解。

技術分享

相比於硬鏈接來說,我認為軟連接打破了他的諸多限制。

  1. 軟連接有自己的元數據,所以可以對其進行權限控制(作用於原文件)

  2. 可跨越文件系統(包括網絡)創建軟連接

  3. 可對不存在的文件和目錄創建(打破不能鏈接目錄的限制)

  4. 創建軟連接,源文件鏈接數不增加

  5. 刪除軟連接時不影響源文件

小實驗


驗證文件訪問是通過iNode節點而非目錄。驗證方法很簡單,對文件進行移動,重命名後查看iNode節點號是否變化。當然還有一點很重要,對於Linux來講目錄也是文件,所以在同一個目錄下不能建立同名的文件和目錄。

[[email protected] workspace]$ touch test

[[email protected] workspace]$ ls -i test #查看test文件的iNode號為1704277

1704277 test

[[email protected] workspace]$ mkdir dir1

[[email protected] workspace]$ mv test dir1/

[[email protected] workspace]$ ls -i dir1/test #移動test文件到不同的目錄下iNode節點不變

1704277 dir1/test

[[email protected] workspace]$ mv dir1/test test.txt #test文件進行重命名,iNode節點號依舊不變,反映出iNode號是文件訪問的唯一標識

[[email protected] workspace]$ ls -i test.txt

1704277 test.txt

[[email protected] workspace]$ ls

dir1 test.txt

[[email protected] workspace]$ mkdir test.txt

mkdir: cannot create directory `test.txt‘: File exists

驗證硬鏈接的相關特性

ln source hardlink命令建立硬鏈接

[[email protected] app]# df -T #查看文件系統信息,筆者在/app分區下做實驗

Filesystem Type 1K-blocks Used Available Use% Mounted on

/dev/sda2 ext4 51475068 4751996 44101632 10% /

tmpfs tmpfs 1019172 76 1019096 1% /dev/shm

/dev/sda3 ext4 40185208 49016 38088192 1% /app

/dev/sda1 ext4 999320 40008 906884 5% /boot

[[email protected] app]# touch test;ll -i test #創建並查看test文件的iNode號,以及第三字段的鏈接數

11 -rw-r--r--. 1 root root 14 Jul 20 13:17 test

[[email protected] app]# ln test /boot/test_hardlink #在boot分區創建硬鏈接,提示錯誤,創建硬鏈接不能跨越文件系統

ln: creating hard link `/boot/test_hardlink‘ => `test‘: Invalid cross-device link

[[email protected] app]# ln test test_hardlink #創建硬鏈接成功

[[email protected] app]# ll –i

# 查看源文件和鏈接文件的屬性信息,完全一致,鏈接數增加1,硬鏈接文件顯示是普通文件,與源文件具有相同的‘地位‘

total 0

11 -rw-r--r--. 2 root root 0 Jul 20 13:15 test

11 -rw-r--r--. 2 root root 0 Jul 20 13:15 test_hardlink

[[email protected] app]# echo "I love linux." >test_hardlink

[[email protected] app]# cat test #對硬鏈接文件進行修改,源文件同步修改,

I love linux.

[[email protected] app]# ln test1 test1_hardlink #不能對未存在的文件創建鏈接

ln: accessing `test1‘: No such file or directory

[[email protected] app]# ln /app app_link #不能對目錄進行創建鏈接

ln: `/app‘: hard link not allowed for directory

[[email protected] app]# rm -f test #刪除源文件後,數據依舊存在,只有把所有的硬鏈接文件都刪除掉,磁盤空間才會被釋放

[[email protected] app]# cat test_hardlink

I love linux.

驗證軟連接特性

ln -s source symlink 命令建立軟連接

[[email protected] app]# touch test

[[email protected] app]# ll -i #關註iNode號與鏈接數

total 0

11 -rw-r--r--. 1 root root 0 Jul 20 13:31 test

[[email protected] app]# ln -s test test_symlink #創建軟連接

[[email protected] app]# ll -i #軟連接和源文件有不同的元數據,不是同一個文件

total 0

11 -rw-r--r--. 1 root root 0 Jul 20 13:31 test

12 lrwxrwxrwx. 1 root root 4 Jul 20 13:31 test_symlink -> test

[[email protected] app]# chmod 600 test_symlink #對軟連接進行權限更改後,效果作用於源文件

[[email protected] app]# ll -i

total 0

11 -rw-------. 1 root root 0 Jul 20 13:31 test

12 lrwxrwxrwx. 1 root root 4 Jul 20 13:31 test_symlink -> test

[[email protected] app]# ln -s /app/test /boot/test_symlink #可以跨越文件系統進行創建軟連接

[[email protected] app]# ll -i /boot/test_symlink

23 lrwxrwxrwx. 1 root root 9 Jul 20 13:38 /boot/test_symlink -> /app/test

[[email protected] app]# echo "I love linux." >test_symlink #對軟連接的寫操作也是寫入到原文件

[[email protected] app]# cat test

I love linux.

[[email protected] app]# rm -f test_symlink #刪除軟連接後不對源文件有影響

[[email protected] app]# cat test

I love linux.

軟連接創建過程中遇到的那些坑

在軟連接的創建過程牽扯到一個使用相對路徑和絕對路徑的問題,在使用相對路徑鏈接到源文件時的參考點是以軟連接所在的目錄為基準,而非當前工作目錄,所以,在使用相對路徑創建軟連接時,一定要尤為註意。這裏筆者有一個小經驗,對於ln -s source symlink 這個命令來說,就是為了創建軟連接,所以可以在敲命令時先敲入軟連接的路徑,這就無所謂相對路徑和絕對路徑了(建議使用絕對路徑,可以以鏈接文件所在路徑推出原文件相對於鏈接文件的路徑)。

讀者可能會想既然使用相對路徑會出現這麽多的麻煩,為什麽還有人使用呢?因為有時需要對文件以及他的軟連接進行同步遷移(相對路徑不變),這時軟連接還是能夠連接到原文件,使用絕對路徑就沒有這種方便。我用下面代碼展示一下。

[[email protected] app]# touch test

[[email protected] app]# mkdir dir

[[email protected] app]# cd dir/

[[email protected] dir]# ln -s ../test test_link1 #用相對路徑進行創建軟連接

[[email protected] dir]# ln -s /app/test test_link2#用絕對路徑進行創建軟連接

[[email protected] dir]# tree /app/

/app/

├── dir

│ ├── test_link1 -> ../test

│ └── test_link2 -> /app/test

└── test

1 directory, 3 files

技術分享

如上圖所示,使用絕對路徑在遷移時軟連接會失效。因此,使用絕對路徑和相對路徑建立軟連接需要根據具體環境而言,而有優缺點。


本文出自 “Keep simple keep stupid” 博客,請務必保留此出處http://yangzhiheng.blog.51cto.com/11586378/1966460

硬鏈接與軟連接